Ocean
Loading...
Searching...
No Matches
FrameInterpolatorBilinear.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_INTERPOLATOR_BILINEAR_H
9#define META_OCEAN_CV_FRAME_INTERPOLATOR_BILINEAR_H
10
11#include "ocean/cv/CV.h"
13#include "ocean/cv/NEON.h"
15#include "ocean/cv/SSE.h"
16
17#include "ocean/base/DataType.h"
18#include "ocean/base/Frame.h"
19#include "ocean/base/Memory.h"
20#include "ocean/base/Worker.h"
21
23
27#include "ocean/math/Lookup2.h"
32#include "ocean/math/Vector2.h"
33
34namespace Ocean
35{
36
37namespace Test { namespace TestCV { class TestFrameInterpolatorBilinearNEON; } }
38
39namespace CV
40{
41
42/**
43 * This class implements bilinear frame interpolator functions.
44 * @ingroup cv
45 */
46class OCEAN_CV_EXPORT FrameInterpolatorBilinear
47{
49
50 public:
51
52 /**
53 * Definition of a lookup table for 2D vectors.
54 */
56
57 public:
58
59 /**
60 * The following comfort class provides comfortable functions simplifying prototyping applications but also increasing binary size of the resulting applications.
61 * Best practice is to avoid using these functions if binary size matters,<br>
62 * as for every comfort function a corresponding function exists with specialized functionality not increasing binary size significantly.<br>
63 */
64 class OCEAN_CV_EXPORT Comfort
65 {
66 public:
67
68 /**
69 * Resizes/rescales a given frame by application of a bilinear interpolation.
70 * @param source The source frame to resize, must be valid
71 * @param target Resulting target frame with identical frame pixel format and pixel origin as the source frame, must be valid
72 * @param worker Optional worker object used for load distribution
73 * @return True, if the frame could be resized
74 */
75 static bool resize(const Frame& source, Frame& target, Worker* worker = nullptr);
76
77 /**
78 * Resizes/rescales a given frame by application of a bilinear interpolation.
79 * @param frame The frame to resize, must be valid
80 * @param width The width of the resized frame in pixel, with range [1, infinity)
81 * @param height The height of the resized frame in pixel, with range [1, infinity)
82 * @param worker Optional worker object used for load distribution
83 * @return True, if the frame could be resized
84 */
85 static inline bool resize(Frame& frame, const unsigned int width, const unsigned int height, Worker* worker = nullptr);
86
87 /**
88 * Zooms into a given input frame (or zooms out) and stores the zoomed image content in an output frame.
89 * The resulting zoomed image will have the same frame type (frame resolution, pixel format, pixel origin) as the input image.<br>
90 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).
91 * @param source The source frame for which the zoomed image content will be created, must be valid
92 * @param target The resulting target frame which will receive the zoomed image, will be set to the same frame type as the source frame, can be invalid
93 * @param zoomFactor The zoom factor to be applied, a factor < 1 will zoom out, a factor > 1 will zoom in, with range (0, infinity)
94 * @param worker Optional worker object to distribute the computation to several CPU cores
95 * @return True, if succeeded
96 */
97 static bool zoom(const Frame& source, Frame& target, const Scalar zoomFactor, Worker* worker = nullptr);
98
99 /**
100 * Transforms a given input frame into an output frame (with arbitrary frame dimension) by application of a homography.
101 * The output frame must have the same pixel format and pixel origin as the input frame, however the dimension (and position) of the output frame can be arbitrary.<br>
102 * This function allows the creation of an output frame fully covering the input frame (if the position and dimension of the output frame covers the transformation of the homography.<br>
103 * The 'outputOrigin' parameter simply applies an additional translation onto the provided homography i.e., homography * create_translation_matrix3x3(outputOrigin.x(), outputOrigin.y()).<br>
104 * Information: This function is the equivalent to OpenCV's cv::warpPerspective().
105 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).
106 * @param input The input frame that will be transformed, must be valid
107 * @param output The output frame resulting by application of the given homography, with same pixel format and pixel origin as the input frame, must be valid
108 * @param input_H_output Homography used to transform the given input frame by following equation: inputPoint = input_H_output * outputPoint, must be valid
109 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels and the data type of the pixel elements, nullptr to assign 0 to each channel
110 * @param worker Optional worker object to distribute the computational load
111 * @param outputOrigin The origin of the output frame defining the global position of the output frame's pixel coordinate (0, 0), with range (-infinity, infinity)x(-infinity, infinity)
112 * @return True, if succeeded
113 */
114 static bool homography(const Frame& input, Frame& output, const SquareMatrix3& input_H_output, const void* borderColor = nullptr, Worker* worker = nullptr, const PixelPositionI& outputOrigin = PixelPositionI(0, 0));
115
116 /**
117 * Transforms a given input frame into an output frame (with arbitrary frame dimension) by application of four homographies.
118 * For each quadrant of the output frame an individual homography is applied while the final result is interpolated between the four homographies.<br>
119 * The quadrant order of the homographies is as follows: top left, top right, bottom left, bottom right.<br>
120 * The output frame must have the same pixel format and pixel origin as the input frame, however the dimension (and position) of the output frame can be arbitrary.<br>
121 * This function allows the creation of an output frame fully covering the input frame (if the position and dimension of the output frame covers the transformation of the homography.<br>
122 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).
123 * @param input The input frame that will be transformed
124 * @param output The output frame resulting by application of the given homography, with same pixel format and pixel origin as the input frame, must have a valid dimension
125 * @param homographies Four homographies used to transform the given input frame, transforming points defined in the output frame into points defined in the input frame
126 * @param outputQuadrantCenter The center position of the four quadrants in the output frame (the local center not respecting the optional outputOrigin parameter), with range [0, output.width())x[0, output.height())
127 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
128 * @param worker Optional worker object to distribute the computational load
129 * @param outputOrigin The origin of the output frame defining the global position of the output frame's pixel coordinate (0, 0), with range (-infinity, infinity)x(-infinity, infinity)
130 * @return True, if succeeded
131 */
132 static bool homographies(const Frame& input, Frame& output, const SquareMatrix3 homographies[4], const Vector2& outputQuadrantCenter, const uint8_t* borderColor = nullptr, Worker* worker = nullptr, const PixelPositionI& outputOrigin = PixelPositionI(0, 0));
133
134 /**
135 * Transforms a given input frame into an output frame (with arbitrary frame dimension) by application of a homography.
136 * The output frame must have the same pixel format and pixel origin as the input frame, however the dimension (and position) of the output frame can be arbitrary.<br>
137 * This function allows the creation of an output frame fully covering the input frame (if the position and dimension of the output frame covers the transformation of the homography).<br>
138 * Input frame pixels lying outside the frame will be masked in the resulting output mask frame, further theses pixels are untouched in the output frame.<br>
139 * The 'outputOrigin' parameter simply applies an additional translation onto the provided homography i.e., homography * create_translation_matrix3x3(outputOrigin.x(), outputOrigin.y()).
140 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).
141 * @param input The input frame that will be transformed, must be valid
142 * @param output The Output frame resulting by application of the given homography, with same pixel format and pixel origin as the input frame, must have a valid dimension
143 * @param outputMask Resulting mask frame with 8 bit per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame, must be valid and must have the same frame dimension as the output frame
144 * @param input_H_output Homography used to transform the given input frame by following equation: inputPoint = input_H_output * outputPoint, must be valid
145 * @param worker Optional worker object to distribute the computational load
146 * @param maskValue 8 bit mask values for reference pixels lying inside the given camera frame, reference pixels lying outside the camera frame will be assigned with (0xFF - maskValue)
147 * @param outputOrigin The origin of the output frame defining the global position of the output frame's pixel coordinate (0, 0), with range (-infinity, infinity)x(-infinity, infinity)
148 * @return True, if succeeded
149 * @see coversHomographyInputFrame().
150 */
151 static bool homographyMask(const Frame& input, Frame& output, Frame& outputMask, const SquareMatrix3& input_H_output, Worker* worker = nullptr, const uint8_t maskValue = 0xFF, const PixelPositionI& outputOrigin = PixelPositionI(0, 0));
152
153 /**
154 * Transforms a given input frame into an output frame (with arbitrary frame dimension) by application of four homographies.
155 * For each quadrant of the output frame an individual homography is applied while the final result is interpolated between the four homographies.<br>
156 * The quadrant order of the homographies is as follows: top left, top right, bottom left, bottom right.<br>
157 * The output frame must have the same pixel format and pixel origin as the input frame, however the dimension (and position) of the output frame can be arbitrary.<br>
158 * This function allows the creation of an output frame fully covering the input frame (if the position and dimension of the output frame covers the transformation of the homography.<br>
159 * Input frame pixels lying outside the frame will be masked in the resulting output mask frame, further theses pixels are untouched in the output frame.<br>
160 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).
161 * @param input The input frame that will be transformed, must be valid
162 * @param output The output frame resulting by application of the given homography, with same pixel format and pixel origin as the input frame, must have a valid dimension
163 * @param outputMask Mask frame with 8 bit per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame
164 * @param homographies Four homographies used to transform the given input frame, transforming points defined in the output frame into points defined in the input frame
165 * @param outputQuadrantCenter The center position of the four quadrants in the output frame (the local center not respecting the optional outputOrigin parameter), with range [0, output.width())x[0, output.height())
166 * @param worker Optional worker object to distribute the computational load
167 * @param maskValue 8 bit mask values for reference pixels lying inside the given camera frame, reference pixels lying outside the camera frame will be assigned with (0xFF - maskValue)
168 * @param outputOrigin The origin of the output frame defining the global position of the output frame's pixel coordinate (0, 0), with range (-infinity, infinity)x(-infinity, infinity)
169 * @return True, if succeeded
170 * @see coversHomographyInputFrame().
171 */
172 static bool homographiesMask(const Frame& input, Frame& output, Frame& outputMask, const SquareMatrix3* homographies, const Vector2& outputQuadrantCenter, Worker* worker = nullptr, const uint8_t maskValue = 0xFF, const PixelPositionI& outputOrigin = PixelPositionI(0, 0));
173
174 /**
175 * Transforms a given input frame into an output frame by application of a homography.
176 * This function also uses a camera profile to improve the interpolation accuracy.<br>
177 * The given homography is transformed into a homography for normalized image coordinates.<br>
178 * Thus, also distortion parameters of the camera profile can be applied.<br>
179 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).
180 * @param inputCamera The pinhole camera profile to be applied for the input frame
181 * @param outputCamera The pinhole camera profile to be applied for the output frame
182 * @param input The input frame that will be transformed, the frame dimension must match the dimension of the left camera
183 * @param output The output frame resulting by application of the given homography, the frame dimension must match the dimension of the right camera
184 * @param homography The homography used to transform the given input frame, with includes both camera profiles: H = Ki * H' * Ko^-1
185 * @param useDistortionParameters True, to apply the distortion parameters of the camera profile
186 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
187 * @param worker Optional worker object to distribute the computational load
188 * @return True, if succeeded
189 * @see homographyWithCameraMask(), homography().
190 */
191 static bool homographyWithCamera(const PinholeCamera& inputCamera, const PinholeCamera& outputCamera, const Frame& input, Frame& output, const SquareMatrix3& homography, const bool useDistortionParameters, const uint8_t* borderColor = nullptr, Worker* worker = nullptr);
192
193 /**
194 * Transforms a given input frame into an output frame by application of a homography.
195 * Input frame pixels lying outside the frame will be masked in the resulting output mask frame, further theses pixels are untouched in the output frame.<br>
196 * This function also uses a camera profile to improve the interpolation accuracy.<br>
197 * The given homography is transformed into a homography for normalized image coordinates.<br>
198 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).
199 * Thus, also distortion parameters of the camera profile can be applied.<br>
200 * @param inputCamera The pinhole camera profile to be applied for the input frame
201 * @param outputCamera The pinhole camera profile to be applied for the output frame
202 * @param input The input frame that will be transformed, the frame dimension must match the dimension of the left camera
203 * @param output The output frame resulting by application of the given homography, the frame dimension must match the dimension of the right camera
204 * @param outputMask Mask frame with 8 bit per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame
205 * @param homography The homography used to transform the given input frame, with includes both camera profiles: H = Ki * H' * Ko^-1
206 * @param worker Optional worker object to distribute the computational load
207 * @param maskValue 8 bit mask values for reference pixels lying inside the given camera frame, reference pixels lying outside the camera frame will be assigned with (0xFF - maskValue)
208 * @return True, if succeeded
209 * @see homographyWithCamera(), homography().
210 */
211 static bool homographyWithCameraMask(const AnyCamera& inputCamera, const AnyCamera& outputCamera, const Frame& input, Frame& output, Frame& outputMask, const SquareMatrix3& homography, Worker* worker = nullptr, const uint8_t maskValue = 0xFFu);
212
213 /**
214 * Transforms a given input frame into an output frame by application of an interpolation lookup table.
215 * The output frame must have the same pixel format and pixel origin as the input frame.<br>
216 * Information: This function is the equivalent to OpenCV's cv::remap().
217 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).
218 * @param input The input frame that will be transformed
219 * @param output Resulting output frame, the dimension will be set to match the size of the lookup table, pixel format and pixel origin will be set to match the given input frame
220 * @param input_LT_output The lookup table which defines the transformation from locations defined in the output frame to locations defined in the input frame (the lookup table stores the corresponding locations in the input frame), must be valid
221 * @param offset True, if the lookup table store local offsets; False, if the lookup table stores absolute positions
222 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
223 * @param worker Optional worker object to distribute the computation
224 * @return True, if succeeded
225 */
226 static bool lookup(const Frame& input, Frame& output, const LookupTable& input_LT_output, const bool offset, const void* borderColor, Worker* worker = nullptr);
227
228 /**
229 * Transforms a given input frame into an output frame by application of an interpolation lookup table and creates and additional mask as output.
230 * The output frame must have the same pixel format and pixel origin as the input frame.<br>
231 * Input frame pixels lying outside the frame will be masked in the resulting output mask frame, further theses pixels are untouched in the output frame.<br>
232 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).
233 * @param input The input frame which will be transformed
234 * @param output Resulting output frame, the frame dimension will be set to match the size of the lookup table, pixel format and pixel origin will be set to match the given input frame
235 * @param outputMask Resulting mask frame with 8 bit per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame
236 * @param input_LT_output The lookup table which defines the transformation from locations defined in the output frame to locations defined in the input frame (the lookup table stores the corresponding locations in the input frame), must be valid
237 * @param offset True, if the lookup table store local offsets; False, if the lookup table stores absolute positions
238 * @param worker Optional worker object to distribute the computation
239 * @param maskValue 8 bit mask values for pixels lying inside the input frame, pixels lying outside the input frame will be assigned with (0xFF - maskValue)
240 * @return True, if succeeded
241 */
242 static bool lookupMask(const Frame& input, Frame& output, Frame& outputMask, const LookupTable& input_LT_output, const bool offset, Worker* worker = nullptr, const uint8_t maskValue = 0xFF);
243
244 /**
245 * Applies an affine transformation to an image.
246 * The target frame must have the same pixel format and pixel origin as the source frame, however the dimension (and position) of the target frame can be arbitrary.
247 * This function allows the creation of an target frame fully covering the source frame (if the position and dimension of the target frame covers the transformation of the affine transformation.
248 * The multiplication of the affine transformation with pixel location in the target image yield their location in the source image, i.e., sourcePoint = source_A_target * targetPoint.
249 * The parameter 'targetOrigin' applies an additional translation to the provided affine transformation i.e., source_A_target * create_translation_matrix3x3(targetOrigin.x(), targetOrigin.y()).
250 * Please note that here the affine transformation is specified as a 3-by-3 matrix (in contrast to the more commonly used 2-by-3 matrix) and should take of the form:
251 * <pre>
252 * a c e
253 * b d f
254 * 0 0 1
255 * </pre>
256 * However, this function disregards the last row completely and only uses the top two rows, i.e., the elements a through f.
257 * Information: This function is the equivalent to OpenCV's cv::warpAffine().
258 * Note: For applications running on mobile devices, in order to keep the impact on binary size to a minimum please prefer a specialized transformation function (those that work on image pointers instead of Frame instances).
259 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).
260 * @param source The source frame that will be transformed, must be valid
261 * @param target The resulting frame after applying the affine transformation to the source frame; pixel format and pixel origin must be identical to source frame; memory of target frame must be allocated by the caller
262 * @param source_A_target Affine transform used to transform the given source frame, transforming points defined in the target frame into points defined in the source frame
263 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
264 * @param worker Optional worker object to distribute the computational load
265 * @param targetOrigin The origin of the target frame defining the global position of the target frame's pixel coordinate (0, 0), with range (-infinity, infinity)x(-infinity, infinity)
266 * @return True, if succeeded
267 */
268 static bool affine(const Frame& source, Frame& target, const SquareMatrix3& source_A_target, const uint8_t* borderColor = nullptr, Worker* worker = nullptr, const PixelPositionI& targetOrigin = PixelPositionI(0, 0));
269
270 /**
271 * Rotates a given frame by a bilinear interpolation.
272 * The frame will be rotated around a specified anchor position (inside or outside the frame).<br>
273 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).
274 * @param source The source frame to be rotated, must be valid
275 * @param target The target frame which will receive the rotated image, will be set to the same frame type as the source frame, can be invalid
276 * @param horizontalAnchorPosition Position of the rotation anchor in the horizontal direction, with range (-infinity, infinity)
277 * @param verticalAnchorPosition Position of the rotation anchor in the vertical direction, with range (-infinity, infinity)
278 * @param angle The counter clockwise rotation angle in radian, with range [0, 2PI)
279 * @param worker Optional worker object to distribute the computation to several CPU cores
280 * @param borderColor The color of border pixels for which now visual content exists, provide one value for each channel, nullptr to use 0x00 for each channel
281 * @return True, if succeeded
282 */
283 static bool rotate(const Frame& source, Frame& target, const Scalar horizontalAnchorPosition, const Scalar verticalAnchorPosition, const Scalar angle, Worker* worker = nullptr, const uint8_t* borderColor = nullptr);
284
285 /**
286 * Re-samples a camera image which has been captured with a camera profile as if the image would have been captured with a second camera profile.
287 * The function can be used e.g., to rectify a fisheye camera image into a pinhole camera image.
288 * @param sourceFrame The source image captured with the source camera profile, must be valid
289 * @param sourceCamera The source camera profile which has been used to capture the source image, with resolution sourceFrame.width() x sourceFrame.height(), must be valid
290 * @param source_R_target The rotation transforming 3D points defined in the coordinate system of the target camera image to 3D points defined in the coordinate system of the source camera image, must be valid
291 * @param targetCamera The camera profile of the target frame, must be valid
292 * @param targetFrame The resulting target image, with resolution targetCamera.width() x targetCamera.height(), must be valid
293 * @param source_OLT_target Optional resulting offset lookup table between target image points and source image points
294 * @param worker Optional worker object to distribute the computational load
295 * @param binSizeInPixel The size in pixel of the interpolation bins used for building the lookup table, with range [1, infinity)
296 * @param borderColor The color of border pixels for which now visual content exists, provide one value for each channel, nullptr to use ElementType(0) for each channel
297 * @return True, if succeeded
298 * @see resampleCameraImageImage8BitPerChannel().
299 */
300 static bool resampleCameraImage(const Frame& sourceFrame, const AnyCamera& sourceCamera, const SquareMatrix3& source_R_target, const AnyCamera& targetCamera, Frame& targetFrame, LookupCorner2<Vector2>* source_OLT_target = nullptr, Worker* worker = nullptr, const unsigned int binSizeInPixel = 8u, const void* borderColor = nullptr);
301
302 /**
303 * Determines the interpolated pixel values for a given pixel position in an 8 bit per channel frame.
304 * This function uses an integer interpolation with a precision of 1/128.
305 * @param frame The frame to determine the pixel values from, must be valid
306 * @param channels Number of channels of the given frame, with range [1, 8]
307 * @param width The width of the frame in pixel, with range [1, infinity)
308 * @param height The height of the frame in pixel, with range [1, infinity)
309 * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
310 * @param pixelCenter The pixel center to be used during interpolation, either 'PC_TOP_LEFT' or 'PC_CENTER'
311 * @param position The position for which the interpolated pixel will be determined, with ranges [0, width - 1]x[0, height - 1] for PC_TOP_LEFT, [0, width]x[0, height] for PC_CENTER
312 * @param result Resulting pixel values, must be valid, must be valid
313 * @return True, if succeeded
314 * @tparam TScalar The scalar data type of the sub-pixel position
315 */
316 template <typename TScalar = Scalar>
317 static bool interpolatePixel8BitPerChannel(const uint8_t* frame, const unsigned int channels, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const PixelCenter pixelCenter, const VectorT2<TScalar>& position, uint8_t* result);
318
319 /**
320 * Determines the interpolated pixel values for a given pixel position in a frame with arbitrary data type.
321 * This function uses floating point precision during interpolation.
322 * @param frame The frame to determine the pixel values from, must be valid
323 * @param channels Number of channels of the given frame, with range [1, 8]
324 * @param width The width of the frame in pixel, with range [1, infinity)
325 * @param height The height of the frame in pixel, with range [1, infinity)
326 * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
327 * @param pixelCenter The pixel center to be used during interpolation, either 'PC_TOP_LEFT' or 'PC_CENTER'
328 * @param position The position to determine the interpolated pixel values for, with range [0, width)x[0, height)
329 * @param result Resulting interpolated pixel value(s), must be valid
330 * @param resultBias Optional bias value which will be added to the interpolation result e.g. to handle rounding, with range (-infinity, infinity), default is zero
331 * @return True, if succeeded
332 * @tparam TSource The data type of the provided pixel values in the (source) frame
333 * @tparam TTarget The data type of the resulting interpolated value(s)
334 * @tparam TScalar The data type of each coordinate of the provided interpolation location, should be either Scalar, float, or double
335 * @tparam TIntermediate The data type of the intermediate interpolation result before assigning the result
336 */
337 template <typename TSource, typename TTarget, typename TScalar = Scalar, typename TIntermediate = TScalar>
338 static bool interpolatePixel(const TSource* frame, const unsigned int channels, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const PixelCenter pixelCenter, const VectorT2<TScalar>& position, TTarget* result, const TIntermediate& resultBias = TIntermediate(0));
339 };
340
341 /**
342 * This class implements highly optimized interpolation functions with fixed properties.
343 * The functions can be significantly faster as these functions are tailored to the specific properties.
344 */
345 class OCEAN_CV_EXPORT SpecialCases
346 {
347 public:
348
349 /**
350 * Resizes a given FORMAT_Y8 frame with resolution 400x400 to a FORMAT_Y8 frame with resolution 224x224 by using a bilinear interpolation.
351 * This function exploits the fact that lookup locations and interpolation factors repeat after 25 pixels (16 pixels in the target resolution).
352 * @param source The source frame buffer with resolution 400x400, must be valid
353 * @param target The target frame buffer receiving the resized image information, with resolution 224x224, must be valid
354 * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
355 * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
356 * @see FrameInterpolatorBilinear::resize<T, tChannels>().
357 */
358 static void resize400x400To224x224_8BitPerChannel(const uint8_t* const source, uint8_t* const target, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements);
359
360 /**
361 * Resizes a given FORMAT_Y8 frame with resolution 400x400 to a FORMAT_Y8 frame with resolution 256x256 by using a bilinear interpolation.
362 * This function exploits the fact that lookup locations and interpolation factors repeat after 25 pixels (16 pixels in the target resolution).
363 * @param source The source frame buffer with resolution 400x400, must be valid
364 * @param target The target frame buffer receiving the resized image information, with resolution 256x256, must be valid
365 * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
366 * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
367 * @see FrameInterpolatorBilinear::resize<T, tChannels>().
368 */
369 static void resize400x400To256x256_8BitPerChannel(const uint8_t* const source, uint8_t* const target, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements);
370 };
371
372 /**
373 * Resizes a given frame with (almost) arbitrary data type (e.g., float, double, int) by using a bilinear interpolation.
374 * This function is actually a wrapper for scale().
375 * @param source The source frame buffer providing the image information to be resized, must be valid
376 * @param target The target frame buffer receiving the resized image information, must be valid
377 * @param sourceWidth Width of the source frame in pixel, with range [1, infinity)
378 * @param sourceHeight Height of the source frame in pixel, with range [1, infinity)
379 * @param targetWidth Width of the target frame in pixel, with range [1, infinity)
380 * @param targetHeight Height of the target frame in pixel, with range [1, infinity)
381 * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
382 * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
383 * @param worker Optional worker object to distribute the computation to several CPU cores
384 * @tparam T Data type of each pixel channel, e.g., float, double, int
385 * @tparam tChannels Number of channels of the frame, with range [1, infinity)
386 * @see scale<T, tChannels>().
387 */
388 template <typename T, unsigned int tChannels>
389 static inline void resize(const T* source, T* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
390
391 /**
392 * Rescales a given frame with arbitrary data type (e.g., float, double, int) by using a bilinear interpolation with user-defined scaling factors.
393 * Beware: This function is not optimized for performance but supports arbitrary data types.<br>
394 * Try to use scale8BitPerChannel() if possible.
395 * @param source The source frame buffer providing the image information to be resized, must be valid
396 * @param target The target frame buffer receiving the rescaled image information, must be valid
397 * @param sourceWidth Width of the source frame in pixel, with range [1, infinity)
398 * @param sourceHeight Height of the source frame in pixel, with range [1, infinity)
399 * @param targetWidth Width of the target frame in pixel, with range [1, infinity)
400 * @param targetHeight Height of the target frame in pixel, with range [1, infinity)
401 * @param sourceX_s_targetX The horizontal scale factor converting a location in the target frame to a location in the source frame (xSource = sourceX_s_targetX * xTarget), with range (0, sourceWidth/targetWidth]
402 * @param sourceY_s_targetY The vertical scale factor converting a location in the target frame to a location in the source frame (ySource = sourceY_s_targetY * yTarget), with range (0, sourceHeight/targetHeight]
403 * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
404 * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
405 * @param worker Optional worker object to distribute the computation to several CPU cores
406 * @tparam T Data type of each pixel channel, e.g., float, double, int
407 * @tparam tChannels Number of channels of the frame, with range [1, infinity)
408 * @see resize<T, tChannels>().
409 */
410 template <typename T, unsigned int tChannels>
411 static inline void scale(const T* source, T* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
412
413 /**
414 * Rotates a given frame by a bilinear interpolation.
415 * The frame will be rotated around a specified anchor position (inside or outside the frame).
416 * @param source The source frame to be rotated, must be valid
417 * @param target The target frame which will receive the rotated image, with same frame type as the source frame, must be valid
418 * @param width The width of the source and target frame in pixel, with range [1, infinity)
419 * @param height The height of the source and target frame in pixel, with range [1, infinity)
420 * @param horizontalAnchorPosition Position of the rotation anchor in the horizontal direction, with range (-infinity, infinity)
421 * @param verticalAnchorPosition Position of the rotation anchor in the vertical direction, with range (-infinity, infinity)
422 * @param angle The counter clockwise rotation angle in radian, with range [0, 2PI)
423 * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
424 * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
425 * @param worker Optional worker object to distribute the computation to several CPU cores
426 * @param borderColor The color of border pixels for which now visual content exists, provide one value for each channel, nullptr to use 0x00 for each channel
427 * @tparam tChannels The number of channels both frames have, with range [1, infinity)
428 */
429 template <unsigned int tChannels>
430 static inline void rotate8BitPerChannel(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const Scalar horizontalAnchorPosition, const Scalar verticalAnchorPosition, const Scalar angle, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr, const uint8_t* borderColor = nullptr);
431
432 /**
433 * Apply an affine transforms to a N-channel, 8-bit frame
434 * The target frame must have the same pixel format and pixel origin as the source frame, however the dimension (and position) of the target frame can be arbitrary.<br>
435 * This function allows the creation of an target frame fully covering the source frame (if the position and dimension of the target frame covers the transformation of the affine transformation).<br>
436 * The 'targetOrigin' parameter simply applies an additional translation onto the provided affine transformation i.e., affine * create_translation_matrix3x3(targetOrigin.x(), targetOrigin.y()).
437 * Please note that here the affine transformation is specified as a 3-by-3 matrix (in contrast to the more commonly used 2-by-3 matrix) and should take of the form:
438 * <pre>
439 * a c e
440 * b d f
441 * 0 0 1
442 * </pre>
443 * However, this function disregards the last row completely and only uses the top two rows, i.e., the elements a through f.
444 * @param source Input frame that will be transformed, must be valid
445 * @param sourceWidth Width of both images in pixel, with range [1, infinity)
446 * @param sourceHeight Height of both images pixel, with range [1, infinity)
447 * @param source_A_target Affine transformation, such that: sourcePoint = source_A_target * targetPoint
448 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
449 * @param target The target frame using the given affine transform, must be valid
450 * @param targetOrigin The origin of the target frame defining the global position of the target frame's pixel coordinate (0, 0), with range (-infinity, infinity)x(-infinity, infinity)
451 * @param targetWidth The width of the target image in pixel, with range [1, infinity)
452 * @param targetHeight The height of the target image in pixel, with range [1, infinity)
453 * @param sourcePaddingElements The number of padding elements at the end of each source frame row, in elements, with range [0, infinity)
454 * @param targetPaddingElements The number of padding elements at the end of each target frame row, in elements, with range [0, infinity)
455 * @param worker Optional worker object to distribute the computational load
456 * @tparam tChannels Number of channels of the frame
457 * @see homographyMask8BitPerChannel(), homographyWithCamera8BitPerChannel(), homography().
458 */
459 template <unsigned int tChannels>
460 static inline void affine8BitPerChannel(const uint8_t* source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3& source_A_target, const uint8_t* borderColor, uint8_t* target, const PixelPositionI& targetOrigin, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
461
462 /**
463 * Transforms a input frame with (almost) arbitrary pixel format into an output frame by application of a homography.
464 * The output frame must have the same pixel format and pixel origin as the input frame, however the dimension (and position) of the output frame can be arbitrary.<br>
465 * This function allows the creation of an output frame fully covering the input frame (if the position and dimension of the output frame covers the transformation of the homography.<br>
466 * The 'outputOrigin' parameter simply applies an additional translation onto the provided homography i.e., homography * create_translation_matrix3x3(outputOrigin.x(), outputOrigin.y()).
467 * @param input The input frame that will be transformed, must be valid
468 * @param inputWidth Width of both images in pixel, with range [1, infinity)
469 * @param inputHeight Height of both images pixel, with range [1, infinity)
470 * @param input_H_output Homography used to transform the given input frame by following equation: inputPoint = input_H_output * outputPoint, must be valid
471 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
472 * @param output The output frame using the given homography, must be valid
473 * @param outputOrigin The origin of the output frame defining the global position of the output frame's pixel coordinate (0, 0), with range (-infinity, infinity)x(-infinity, infinity)
474 * @param outputWidth The width of the output image in pixel, with range [1, infinity)
475 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
476 * @param inputPaddingElements The number of padding elements at the end of each input frame row, in elements, with range [0, infinity)
477 * @param outputPaddingElements The number of padding elements at the end of each output frame row, in elements, with range [0, infinity)
478 * @param worker Optional worker object to distribute the computational load
479 * @tparam T Data type of each pixel channel, e.g., float, double, int
480 * @tparam tChannels Number of channels of the frame, with range [1, infinity)
481 * @see homographyMask8BitPerChannel(), homographyWithCamera8BitPerChannel().
482 */
483 template <typename T, unsigned int tChannels>
484 static inline void homography(const T* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3& input_H_output, const T* borderColor, T* output, const PixelPositionI& outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker* worker = nullptr);
485
486 /**
487 * Transforms a given 8 bit per channel input frame into an output frame by application of four homographies.
488 * For each quadrant of the output frame an individual homography is applied while the final result is interpolated between the four homographies.<br>
489 * The quadrant order of the homographies is as follows: top left, top right, bottom left, bottom right.<br>
490 * The output frame must have the same pixel format and pixel origin as the input frame, however the dimension (and position) of the output frame can be arbitrary.<br>
491 * This function allows the creation of an output frame fully covering the input frame (if the position and dimension of the output frame covers the transformation of the homography.<br>
492 * @param input The input frame that will be transformed
493 * @param inputWidth Width of both images in pixel, with range [1, infinity)
494 * @param inputHeight Height of both images pixel, with range [1, infinity)
495 * @param homographies Four homographies used to transform the given input frame, transforming points defined in the output frame into points defined in the input frame
496 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
497 * @param output The output frame using the given homography
498 * @param outputQuadrantCenter The center position of the four quadrants in the output frame (the local center not respecting the optional outputOrigin parameter), with range [0, outputWidth)x[0, outputHeight)
499 * @param outputOrigin The origin of the output frame defining the global position of the output frame's pixel coordinate (0, 0), with range (-infinity, infinity)x(-infinity, infinity)
500 * @param outputWidth The width of the output image in pixel, with range [1, infinity)
501 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
502 * @param inputPaddingElements The number of padding elements at the end of each input frame row, in elements, with range [0, infinity)
503 * @param outputPaddingElements The number of padding elements at the end of each output frame row, in elements, with range [0, infinity)
504 * @param worker Optional worker object to distribute the computational load
505 * @tparam tChannels Number of channels of the frame
506 * @see homographyMask8BitPerChannel(), homographyWithCamera8BitPerChannel().
507 */
508 template <unsigned int tChannels>
509 static inline void homographies8BitPerChannel(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 homographies[4], const uint8_t* borderColor, uint8_t* output, const Vector2& outputQuadrantCenter, const PixelPositionI& outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker* worker = nullptr);
510
511 /**
512 * Transforms a given 8 bit per channel input frame into an output frame by application of a homography.
513 * The output frame must have the same pixel format and pixel origin as the input frame, however the dimension (and position) of the output frame can be arbitrary.<br>
514 * This function allows the creation of an output frame fully covering the input frame (if the position and dimension of the output frame covers the transformation of the homography.<br>
515 * The 'outputOrigin' parameter simply applies an additional translation onto the provided homography i.e., homography * create_translation_matrix3x3(outputOrigin.x(), outputOrigin.y()).
516 * @param input The input frame that will be transformed, must be valid
517 * @param inputWidth Width of both images in pixel, with range [1, infinity)
518 * @param inputHeight Height of both images pixel, with range [1, infinity)
519 * @param input_H_output Homography used to transform the given input frame by following equation: inputPoint = input_H_output * outputPoint, must be valid
520 * @param output The output frame using the given homography, must be valid
521 * @param outputMask Mask frame with 8 bit per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame, must be valid
522 * @param outputOrigin The origin of the output frame defining the global position of the output frame's pixel coordinate (0, 0), with range (-infinity, infinity)x(-infinity, infinity)
523 * @param outputWidth The width of the output image in pixel, with range [1, infinity)
524 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
525 * @param maskValue 8 bit mask values for reference pixels lying inside the given camera frame, reference pixels lying outside the camera frame will be assigned with (0xFF - maskValue)
526 * @param inputPaddingElements The number of padding elements at the end of each input row, in elements, with range [0, infinity)
527 * @param outputPaddingElements The number of padding elements at the end of each output row, in elements, with range [0, infinity)
528 * @param outputMaskPaddingElements The number of padding elements at the end of output mask input row, in elements, with range [0, infinity)
529 * @param worker Optional worker object to distribute the computational load
530 * @tparam tChannels Number of channels of the frame, with range [1, infinity)
531 * @see homography(), homographyWithCamera8BitPerChannel().
532 */
533 template <unsigned int tChannels>
534 static inline void homographyMask8BitPerChannel(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3& input_H_output, uint8_t* output, uint8_t* outputMask, const PixelPositionI& outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const uint8_t maskValue /* = 0xFF*/, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker* worker = nullptr);
535
536 /**
537 * Transforms a given 8 bit per channel input frame into an output frame by application of four homographies.
538 * For each quadrant of the output frame an individual homography is applied while the final result is interpolated between the four homographies.<br>
539 * The quadrant order of the homographies is as follows: top left, top right, bottom left, bottom right.<br>
540 * The output frame must have the same pixel format and pixel origin as the input frame, however the dimension (and position) of the output frame can be arbitrary.<br>
541 * This function allows the creation of an output frame fully covering the input frame (if the position and dimension of the output frame covers the transformation of the homography.<br>
542 * @param input The input frame that will be transformed
543 * @param inputWidth Width of both images in pixel, with range [1, infinity)
544 * @param inputHeight Height of both images pixel, with range [1, infinity)
545 * @param homographies Four homographies used to transform the given input frame, transforming points defined in the output frame into points defined in the input frame
546 * @param output The output frame using the given homography
547 * @param outputMask Mask frame with 8 bit per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame
548 * @param outputQuadrantCenter The center position of the four quadrants in the output frame (the local center not respecting the optional outputOrigin parameter), with range [0, outputWidth)x[0, outputHeight)
549 * @param outputOrigin The origin of the output frame defining the global position of the output frame's pixel coordinate (0, 0), with range (-infinity, infinity)x(-infinity, infinity)
550 * @param outputWidth The width of the output image in pixel, with range [1, infinity)
551 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
552 * @param inputPaddingElements The number of padding elements at the end of each input row, in elements, with range [0, infinity)
553 * @param outputPaddingElements The number of padding elements at the end of each output row, in elements, with range [0, infinity)
554 * @param outputMaskPaddingElements The number of padding elements at the end of each row of the output mask, in elements, with range [0, infinity)
555 * @param worker Optional worker object to distribute the computational load
556 * @param maskValue 8 bit mask values for reference pixels lying inside the given camera frame, reference pixels lying outside the camera frame will be assigned with (0xFF - maskValue)
557 * @tparam tChannels Number of channels of the frame
558 * @see homography(), homographyWithCamera8BitPerChannel().
559 */
560 template <unsigned int tChannels>
561 static inline void homographiesMask8BitPerChannel(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 homographies[4], uint8_t* output, uint8_t* outputMask, const Vector2& outputQuadrantCenter, const PixelPositionI& outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker* worker = nullptr, const uint8_t maskValue = 0xFF);
562
563 /**
564 * Transforms a given 8 bit per channel input frame into an output frame by application of a homography.
565 * This function also uses a camera profile to improve the interpolation accuracy.<br>
566 * The given homography is transformed into a homography for normalized image coordinates.<br>
567 * Thus, also distortion parameters of the camera profile can be applied.<br>
568 * @param inputCamera The pinhole camera profile to be applied for the input frame
569 * @param outputCamera The pinhole camera profile to be applied for the output frame
570 * @param input The input frame that will be transformed
571 * @param homography The homography used to transform the given input frame by following equation: inputPoint = homography * outputPoint
572 * @param useDistortionParameters True, to apply the distortion parameters of the camera profile
573 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
574 * @param output The output frame using the given homography
575 * @param inputPaddingElements The number of padding elements at the end of each input row, in elements, with range [0, infinity)
576 * @param outputPaddingElements The number of padding elements at the end of each output row, in elements, with range [0, infinity)
577 * @param worker Optional worker object to distribute the computational load
578 * @tparam tChannels Number of channels of the frame
579 * @see homography().
580 */
581 template <unsigned int tChannels>
582 static inline void homographyWithCamera8BitPerChannel(const PinholeCamera& inputCamera, const PinholeCamera& outputCamera, const uint8_t* input, const SquareMatrix3& homography, const bool useDistortionParameters, const uint8_t* borderColor, uint8_t* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker* worker = nullptr);
583
584 /**
585 * Transforms a given 8 bit per channel input frame into an output frame by application of a homography.
586 * Input frame pixels lying outside the frame will be masked in the resulting output mask frame.<br>
587 * This function also uses a camera profile to improve the interpolation accuracy.<br>
588 * The given homography is transformed into a homography for normalized image coordinates.<br>
589 * Thus, also distortion parameters of the camera profile can be applied.
590 * @param inputCamera The pinhole camera profile to be applied for the input frame, must be valid
591 * @param outputCamera The pinhole camera profile to be applied for the output frame, must be valid
592 * @param input The input frame that will be transformed, must be valid
593 * @param inputPaddingElements The number of padding elements at the end of each input row, in elements, with range [0, infinity)
594 * @param homography The homography used to transform the given input frame by following equation: inputPoint = homography * outputPoint
595 * @param output The output frame using the given homography
596 * @param outputMask Mask frame with 8 bit per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame
597 * @param outputPaddingElements The number of padding elements at the end of each output row, in elements, with range [0, infinity)
598 * @param outputMaskPaddingElements The number of padding elements at the end of each output mask row, in elements, with range [0, infinity)
599 * @param worker Optional worker object to distribute the computational load
600 * @param maskValue 8 bit mask values for reference pixels lying inside the given camera frame, reference pixels lying outside the camera frame will be assigned with (0xFF - maskValue)
601 * @tparam tChannels Number of channels of the frame
602 */
603 template <unsigned int tChannels>
604 static inline void homographyWithCameraMask8BitPerChannel(const PinholeCamera& inputCamera, const PinholeCamera& outputCamera, const uint8_t* input, const unsigned int inputPaddingElements, const SquareMatrix3& homography, uint8_t* output, uint8_t* outputMask, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker* worker = nullptr, const uint8_t maskValue = 0xFF);
605
606 /**
607 * Transforms a given input frame into an output frame by application of an interpolation lookup table.
608 * The frame must have a 1-plane pixel format.<br>
609 * The output frame must have the same pixel format and pixel origin as the input frame.
610 * @param input The input frame which will be transformed, must be valid
611 * @param inputWidth The width of the given input frame in pixel, with range [1, infinity)
612 * @param inputHeight The height of the given input frame in pixel, with range [1, infinity)
613 * @param input_LT_output The lookup table which defines the transformation from locations defined in the output frame to locations defined in the input frame (the lookup table stores the corresponding locations in the input frame), must be valid
614 * @param offset True, if the lookup table store local offsets; False, if the lookup table stores absolute positions
615 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign T(0) to each channel
616 * @param output Resulting output frame with frame dimension equal to the size of the given lookup table, must be valid
617 * @param inputPaddingElements The number of padding elements at the end of each input row, in elements, with range [0, infinity)
618 * @param outputPaddingElements The number of padding elements at the end of each output row, in elements, with range [0, infinity)
619 * @param worker Optional worker object to distribute the computation
620 * @param use4ChannelSamplingFastPaths True to enable additional Apple ARM64 four-channel paths when useOptimizedNEON is true
621 * @tparam T Data type of each pixel channel, e.g., float, double, int
622 * @tparam tChannels Number of channels of the frame, with range [1, infinity)
623 */
624 template <typename T, unsigned int tChannels>
625 static inline void lookup(const T* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable& input_LT_output, const bool offset, const T* borderColor, T* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker* worker = nullptr, const bool useOptimizedNEON = false, const bool useOptimizedBilinearValuesAndFactorCalculation = false, const bool useOptimizedNEONFactorReplication = false, const bool use4ChannelSamplingFastPaths = false);
626
627 /**
628 * Transforms a given input frame into an output frame by application of an interpolation lookup table.
629 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).<br>
630 * The output frame must have the same pixel format and pixel origin as the input frame.<br>
631 * Input frame pixels lying outside the frame will be masked in the resulting output mask frame, further theses pixels are untouched in the output frame.<br>
632 * @param input The input frame which will be transformed
633 * @param inputWidth The width of the given input frame in pixel, with range [1, infinity)
634 * @param inputHeight The height of the given input frame in pixel, with range [1, infinity)
635 * @param input_LT_output The lookup table which defines the transformation from locations defined in the output frame to locations defined in the input frame (the lookup table stores the corresponding locations in the input frame), must be valid
636 * @param offset True, if the lookup table store local offsets; False, if the lookup table stores absolute positions
637 * @param output Resulting output frame with frame dimension equal to the size of the given lookup table
638 * @param outputMask Resulting mask frame with 8 bits per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame
639 * @param inputPaddingElements The number of padding elements at the end of each row of `input`, in elements, with range [0, infinity)
640 * @param outputPaddingElements The number of padding elements at the end of each row of `output`, in elements, with range [0, infinity)
641 * @param outputMaskPaddingElements The number of padding elements at the end of each row of `outputMask`, in elements, with range [0, infinity)
642 * @param worker Optional worker object to distribute the computation
643 * @param maskValue 8 bit mask values for pixels lying inside the input frame, pixels lying outside the input frame will be assigned with (0xFF - maskValue)
644 * @tparam tChannels Number of channels of the frame
645 */
646 template <unsigned int tChannels>
647 static inline void lookupMask8BitPerChannel(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable& input_LT_output, const bool offset, uint8_t* output, uint8_t* outputMask, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker* worker = nullptr, const uint8_t maskValue = 0xFF);
648
649 /**
650 * Re-samples a camera image which has been captured with a camera profile as if the image would have been captured with a second camera profile.
651 * The function can be used e.g., to rectify a fisheye camera image into a pinhole camera image.
652 * @param sourceFrame The source image captured with the source camera profile, must be valid
653 * @param sourceCamera The source camera profile which has been used to capture the source image, with resolution sourceFrame.width() x sourceFrame.height(), must be valid
654 * @param source_R_target The rotation transforming 3D points defined in the coordinate system of the target camera image to 3D points defined in the coordinate system of the source camera image, must be valid
655 * @param targetCamera The camera profile of the target frame, must be valid
656 * @param targetFrame The resulting target image, with resolution targetCamera.width() x targetCamera.height(), must be valid
657 * @param sourceFramePaddingElements The number of padding elements at the end of each source frame row, in elements, with range [0, infinity)
658 * @param targetFramePaddingElements The number of padding elements at the end of each target frame row, in elements, with range [0, infinity)
659 * @param source_OLT_target Optional resulting offset lookup table between target image points and source image points
660 * @param worker Optional worker object to distribute the computational load
661 * @param binSizeInPixel The size in pixel of the interpolation bins used for building the lookup table, with range [1, infinity)
662 * @param borderColor The color of border pixels for which now visual content exists, provide one value for each channel, nullptr to use T(0) for each channel
663 * @tparam T Data type of each pixel channel, e.g., uint8_t, int16_t, float, double
664 * @tparam tChannels The number of frame channels, with range [1, infinity)
665 * @see Comfort::resampleCameraImage().
666 */
667 template <typename T, unsigned int tChannels>
668 static void resampleCameraImage(const T* sourceFrame, const AnyCamera& sourceCamera, const SquareMatrix3& source_R_target, const AnyCamera& targetCamera, T* targetFrame, const unsigned int sourceFramePaddingElements, const unsigned int targetFramePaddingElements, LookupCorner2<Vector2>* source_OLT_target = nullptr, Worker* worker = nullptr, const unsigned int binSizeInPixel = 8u, const T* borderColor = nullptr);
669
670 /**
671 * Determines the interpolated pixel values for a given pixel position in an 8 bit per channel frame.
672 * This function uses an integer interpolation with a precision of 1/128.
673 * @param frame The frame to determine the pixel values from, must be valid
674 * @param width The width of the frame in pixel, with range [1, infinity)
675 * @param height The height of the frame in pixel, with range [1, infinity)
676 * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
677 * @param position The position for which the interpolated pixel will be determined, with ranges [0, width - 1]x[0, height - 1] for PC_TOP_LEFT, [0, width]x[0, height] for PC_CENTER
678 * @param result Resulting pixel values, must be valid, must be valid
679 * @tparam tChannels Number of channels of the given frame, with range [1, infinity)
680 * @tparam tPixelCenter The pixel center to be used during interpolation, either 'PC_TOP_LEFT' or 'PC_CENTER'
681 * @tparam TScalar The scalar data type of the sub-pixel position
682 * @see interpolatePixel().
683 */
684 template <unsigned int tChannels, PixelCenter tPixelCenter = PC_TOP_LEFT, typename TScalar = Scalar>
685 static inline void interpolatePixel8BitPerChannel(const uint8_t* frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const VectorT2<TScalar>& position, uint8_t* result);
686
687 /**
688 * Determines the interpolated pixel values for a given pixel position in a frame with arbitrary data type.
689 * This function uses floating point precision during interpolation.
690 * @param frame The frame to determine the pixel values from, must be valid
691 * @param width The width of the frame in pixel, with range [1, infinity)
692 * @param height The height of the frame in pixel, with range [1, infinity)
693 * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
694 * @param position The position for which the interpolated pixel will be determined, with ranges [0, width - 1]x[0, height - 1] for PC_TOP_LEFT, [0, width]x[0, height] for PC_CENTER
695 * @param result Resulting interpolated pixel value(s), must be valid
696 * @param resultBias Optional bias value which will be added to the interpolation result e.g. to handle rounding, with range (-infinity, infinity), default is zero
697 * @tparam TSource The data type of the provided pixel values in the (source) frame
698 * @tparam TTarget The data type of the resulting interpolated value(s)
699 * @tparam tChannels Number of channels of the given frame, with range [1, infinity)
700 * @tparam tPixelCenter The pixel center to be used during interpolation, either 'PC_TOP_LEFT' or 'PC_CENTER'
701 * @tparam TScalar The data type of each coordinate of the provided interpolation location, should be either Scalar, float, or double
702 * @tparam TIntermediate The data type of the intermediate interpolation result before assigning the result
703 * @see interpolatePixel8BitPerChannel().
704 */
705 template <typename TSource, typename TTarget, unsigned int tChannels, PixelCenter tPixelCenter = PC_TOP_LEFT, typename TScalar = Scalar, typename TIntermediate = TScalar>
706 static inline void interpolatePixel(const TSource* frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const VectorT2<TScalar>& position, TTarget* result, const TIntermediate& resultBias = TIntermediate(0));
707
708 /**
709 * Determines the interpolated pixel values for a given pixel position in an 8 bit per channel frame with alpha channel.
710 * The center of each pixel is located with an offset of (0.5 x 0.5) in relation to the real pixel position.<br>
711 * The given frame is virtually extended by a fully transparent border so that this functions supports arbitrary interpolation positions.<br>
712 * If the given position lies inside the frame area of (-0.5, -0.5) -> (width + 0.5, height + 0.5) the resulting interpolation result will contain color information of the frame, otherwise a fully transparent interpolation result is provided.<br>
713 * @param frame The frame to determine the pixel values from, must be valid
714 * @param width The width of the frame in pixel, with range [1, infinity)
715 * @param height The height of the frame in pixel, with range [1, infinity)
716 * @param position The position to determine the interpolated pixel values for, with range (-infinity, infinity)x(-infinity, infinity)
717 * @param result Resulting pixel values, must be valid
718 * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
719 * @tparam tChannels Number of channels of the given frame, with range [1, infinity)
720 * @tparam tAlphaAtFront True, if the alpha channel is in the front of the data channels
721 * @tparam tTransparentIs0xFF True, if 0xFF is interpreted as fully transparent
722 */
723 template <unsigned int tChannels, bool tAlphaAtFront, bool tTransparentIs0xFF>
724 static inline void interpolate1PixelFullAlphaBorder8BitPerChannel(const uint8_t* frame, const unsigned int width, const unsigned int height, const Vector2& position, uint8_t* result, const unsigned int framePaddingElements);
725
726 /**
727 * Interpolate the sum of intensity values of an image patch in a frame, while the frame is provided as lined integral frame.
728 * @param linedIntegralFrame The lined integral image created from the actual gray-scale image for which the patch intensity sum will be determined, must be valid
729 * @param frameWidth Width of the original frame in pixel (not the width of the lined-integral frame), with range [1, infinity)
730 * @param frameHeight Height of the original frame in pixel (not the height of the lined-integral frame), with range [1, infinity)
731 * @param lineIntegralFramePaddingElements The number of padding elements at the end of each integral image row, in elements, with range [0, infinity)
732 * @param center 2D coordinates of the center point of the patch, with range [patchWidth/2, frameWidth - patchWidth/2)x[patchHeight/2, frameHeight - patchHeight/2) for PC_CENTER
733 * @param pixelCenter The pixel center to be used during interpolation, either 'PC_TOP_LEFT' or 'PC_CENTER'
734 * @param patchWidth Width of the calculated patch in pixel with range [1, frameWidth - 1]
735 * @param patchHeight Height of the calculated patch in pixel with range [1, frameHeight - 1]
736 * @return The resulting sum of the pixel intensities
737 */
738 static Scalar patchIntensitySum1Channel(const uint32_t* linedIntegralFrame, const unsigned int frameWidth, const unsigned int frameHeight, const unsigned int lineIntegralFramePaddingElements, const Vector2& center, const CV::PixelCenter pixelCenter, const unsigned int patchWidth, const unsigned int patchHeight);
739
740 /**
741 * Checks whether the application of a given homography for a specified input frame and output frame covers the entire image information (which is necessary for the bilinear interpolation) or whether the homography relies on missing image information.
742 * @param inputWidth The width of the input frame in pixel, with range [1, infinity)
743 * @param inputHeight The height of the input frame in pixel, with range [1, infinity)
744 * @param outputWidth The width of the output frame in pixel, with range [1, infinity)
745 * @param outputHeight The height of the output frame in pixel, with range [1, infinity)
746 * @param input_H_output Homography used to transform the given input frame by following equation: inputPoint = input_H_output * outputPoint, must be valid
747 * @param outputOriginX The horizontal origin of the output frame defining the global position of the output frame's pixel coordinate (0, 0), with range (-infinity, infinity)
748 * @param outputOriginY The vertical origin of the output frame defining the global position of the output frame's pixel coordinate (0, 0), with range (-infinity, infinity)
749 * @return True, if the homography covers the entire input image information (if all output pixels will receive valid data from the input frame); False, otherwise
750 */
751 static bool coversHomographyInputFrame(const unsigned int inputWidth, const unsigned int inputHeight, const unsigned int outputWidth, const unsigned int outputHeight, const SquareMatrix3& input_H_output, const int outputOriginX = 0, const int outputOriginY = 0);
752
753 private:
754
755 /**
756 * Transforms a given 8 bit per channel input frame into an output frame by application of a homography.
757 * The output frame must have the same pixel format and pixel origin as the input frame, however the dimension (and position) of the output frame can be arbitrary.<br>
758 * This function allows the creation of an output frame fully covering the input frame (if the position and dimension of the output frame covers the transformation of the homography).<br>
759 * The 'outputOrigin' parameter simply applies an additional translation onto the provided homography i.e., homography * create_translation_matrix3x3(outputOrigin.x(), outputOrigin.y()).
760 * @param input The input frame that will be transformed, must be valid
761 * @param inputWidth Width of both images in pixel, with range [1, infinity)
762 * @param inputHeight Height of both images pixel, with range [1, infinity)
763 * @param input_H_output Homography used to transform the given input frame by following equation: inputPoint = input_H_output * outputPoint, must be valid
764 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
765 * @param output The output frame using the given homography, must be valid
766 * @param outputOrigin The origin of the output frame defining the global position of the output frame's pixel coordinate (0, 0), with range (-infinity, infinity)x(-infinity, infinity)
767 * @param outputWidth The width of the output image in pixel, with range [1, infinity)
768 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
769 * @param inputPaddingElements The number of padding elements at the end of each input frame row, in elements, with range [0, infinity)
770 * @param outputPaddingElements The number of padding elements at the end of each output frame row, in elements, with range [0, infinity)
771 * @param worker Optional worker object to distribute the computational load
772 * @tparam tChannels Number of channels of the frame
773 * @see homographyMask8BitPerChannel(), homographyWithCamera8BitPerChannel(), homography().
774 */
775 template <unsigned int tChannels>
776 static inline void homography8BitPerChannel(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3& input_H_output, const uint8_t* borderColor, uint8_t* output, const PixelPositionI& outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker* worker = nullptr);
777
778 /**
779 * Rescales a given frame with 8 bit per data channel by using a bilinear interpolation with user-defined scaling factors.
780 * The frame must have a 1-plane pixel format with DT_UNSIGNED_INTEGER_8 as data type (e.g., FORMAT_Y8, FORMAT_RGB24, FORMAT_RGBA32, ...).<br>
781 * Information: This function is the equivalent to OpenCV's cv::resize().
782 * @param source The source frame buffer providing the image information to be resized, must be valid
783 * @param target The target frame buffer receiving the rescaled image information, must be valid
784 * @param sourceWidth Width of the source frame in pixel, with range [1, infinity)
785 * @param sourceHeight Height of the source frame in pixel, with range [1, infinity)
786 * @param targetWidth Width of the target frame in pixel, with range [1, infinity)
787 * @param targetHeight Height of the target frame in pixel, with range [1, infinity)
788 * @param sourceX_s_targetX The horizontal scale factor converting a location in the target frame to a location in the source frame (xSource = sourceX_s_targetX * xTarget), with range (0, sourceWidth/targetWidth]
789 * @param sourceY_s_targetY The vertical scale factor converting a location in the target frame to a location in the source frame (ySource = sourceY_s_targetY * yTarget), with range (0, sourceHeight/targetHeight]
790 * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
791 * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
792 * @param worker Optional worker object to distribute the computation to several CPU cores
793 * @tparam tChannels Number of channels of the frame, with range [1, infinity)
794 */
795 template <unsigned int tChannels>
796 static inline void scale8BitPerChannel(const uint8_t* source, uint8_t* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
797
798 /**
799 * Resizes a subset of a given frame with 8 bit per channel by a bilinear interpolation.
800 * @param source The image data of the source frame to be resized, must be valid
801 * @param target The target frame buffer receiving the interpolated (resized) source frame, must be valid
802 * @param sourceWidth Width of the source frame in pixel, with range [1, infinity)
803 * @param sourceHeight Height of the source frame in pixel, with range [1, infinity)
804 * @param targetWidth Width of the target frame in pixel, with range [1, infinity)
805 * @param targetHeight Height of the target frame in pixel, with range [1, infinity)
806 * @param sourceX_s_targetX The horizontal scale factor converting a location in the target frame to a location in the source frame (xSource = sourceX_s_targetX * xTarget), with range (0, sourceWidth/targetWidth]
807 * @param sourceY_s_targetY The vertical scale factor converting a location in the target frame to a location in the source frame (ySource = sourceY_s_targetY * yTarget), with range (0, sourceHeight/targetHeight]
808 * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
809 * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
810 * @param firstTargetRow The first target row to be handled, with range [0, targetHeight)
811 * @param numberTargetRows The number of target row to be handled, with range [1, targetHeight - firstTargetRow]
812 * @tparam tChannels Number of frame channels, with range [0, infinity)
813 */
814 template <unsigned int tChannels>
815 static void scale8BitPerChannelSubset(const uint8_t* source, uint8_t* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows);
816
817 /**
818 * Applies a (horizontal) linear interpolation for one row with 8 bit per channel.
819 * This function uses interpolation factors with 7 bit precision and does not apply any SIMD instructions.
820 * @param extendedSourceRow The source row extended with a copy of the last pixel for which the interpolation will be applied, must be valid
821 * @param targetRow The target row receiving the interpolation result, must be valid
822 * @param targetWidth The with of the target row in pixel, with range [8, infinity)
823 * @param channels The number of frame channels, must be identical with 'tChannels', possible values are 1, 4
824 * @param interpolationLocations The successive locations within the source row defining the location of the left pixels to be interpolated (specified in elements !not! in pixels - e.g., interpolationLocations[0] = firstInterpolationPixel * channels), one for each target pixel, with range [0, (targetWidth - 1) * channels]
825 * @param interpolationFactors The two successive (left !and! right) interpolation factors for each left and right source pixel, with range [0, 128]
826 * @see interpolateRowHorizontal8BitPerChannel7BitPrecisionNEON<tChannels>().
827 */
828 static void interpolateRowHorizontal8BitPerChannel7BitPrecision(const uint8_t* extendedSourceRow, uint8_t* targetRow, const unsigned int targetWidth, const unsigned int channels, const unsigned int* interpolationLocations, const uint8_t* interpolationFactors);
829
830 /**
831 * Applies a (vertical) linear interpolation between two rows with arbitrary data types.
832 * This function does not apply any SIMD instructions.<br>
833 * The length of both source rows is identical with the length of the target row.
834 * @param sourceRowTop The top source row to be used for interpolation, must be valid
835 * @param sourceRowBottom The bottom source row to be used for interpolation, must be valid
836 * @param targetRow The target row receiving the interpolation result, must be valid
837 * @param elements The number of elements in the row to (width * channels), with range [1, infinity)
838 * @param factorBottom The interpolation factor for all elements of the bottom row, with factorTop = 1 - factorBottom, with range [0, 1]
839 * @tparam T The data type of each element, should be 'float'
840 */
841 template <typename T>
842 static void interpolateRowVertical(const T* sourceRowTop, const T* sourceRowBottom, T* targetRow, const unsigned int elements, const float factorBottom);
843
844 /**
845 * Applies a (horizontal) linear interpolation for one row with arbitrary data type.
846 * This function does not apply any SIMD instructions.
847 * @param extendedSourceRow The source row extended with a copy of the last pixel for which the interpolation will be applied, must be valid
848 * @param targetRow The target row receiving the interpolation result, must be valid
849 * @param targetWidth The with of the target row in pixel, with range [8, infinity)
850 * @param channels The number of frame channels, must be identical with 'tChannels', possible values are 1, 4
851 * @param interpolationLocations The successive locations within the source row defining the location of the left pixels to be interpolated (specified in elements !not! in pixels - e.g., interpolationLocations[0] = firstInterpolationPixel * channels), one for each target pixel, with range [0, (targetWidth - 1) * channels]
852 * @param interpolationFactorsRight The right interpolation factors for each right source pixel, with range [0, 1]
853 * @tparam T The data type of each element, should be 'float'
854 * @tparam tChannels The number of frame channels this function can handle, should be 1
855 * @see interpolateRowHorizontal8BitPerChannel7BitPrecision().
856 */
857 template <typename T, unsigned int tChannels>
858 static void interpolateRowHorizontal(const T* extendedSourceRow, T* targetRow, const unsigned int targetWidth, const unsigned int channels, const unsigned int* interpolationLocations, const float* interpolationFactorsRight);
859
860#if defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
861
862 /**
863 * Applies a (vertical) linear interpolation between two rows with 8 bit per channel.
864 * This function applies NEON instructions and uses interpolation factors with 7 bit precision.<br>
865 * The length of both source rows is identical with the length of the target row.
866 * @param sourceRowTop The top source row to be used for interpolation, must be valid
867 * @param sourceRowBottom The bottom source row to be used for interpolation, must be valid
868 * @param targetRow The target row receiving the interpolation result, must be valid
869 * @param elements The number of elements in the row to (width * channels), with range [16, infinity)
870 * @param factorBottom The interpolation factor for all elements of the bottom row, with factorTop = 128 - factorBottom, with range [0, 128]
871 */
872 static void interpolateRowVertical8BitPerChannel7BitPrecisionNEON(const uint8_t* sourceRowTop, const uint8_t* sourceRowBottom, uint8_t* targetRow, const unsigned int elements, const unsigned int factorBottom);
873
874 /**
875 * Applies a (vertical) linear interpolation between two rows with arbitrary data types.
876 * This function applies NEON instructions.<br>
877 * The length of both source rows is identical with the length of the target row.
878 * @param sourceRowTop The top source row to be used for interpolation, must be valid
879 * @param sourceRowBottom The bottom source row to be used for interpolation, must be valid
880 * @param targetRow The target row receiving the interpolation result, must be valid
881 * @param elements The number of elements in the row to (width * channels), with range [16, infinity)
882 * @param factorBottom The interpolation factor for all elements of the bottom row, with factorTop = 1 - factorBottom, with range [0, 1]
883 * @tparam T The data type of each element, should be 'float'
884 */
885 template <typename T>
886 static void interpolateRowVerticalNEON(const T* sourceRowTop, const T* sourceRowBottom, T* targetRow, const unsigned int elements, const float factorBottom);
887
888 /**
889 * Applies a (horizontal) linear interpolation for one row with 8 bit per channel.
890 * This function applies NEON instructions and uses interpolation factors with 7 bit precision.
891 * @param extendedSourceRow The source row extended with a copy of the last pixel for which the interpolation will be applied, must be valid
892 * @param targetRow The target row receiving the interpolation result, must be valid
893 * @param targetWidth The with of the target row in pixel, with range [8, infinity)
894 * @param channels The number of frame channels, must be identical with 'tChannels', possible values are 1, 4
895 * @param interpolationLocations The successive locations within the source row defining the location of the left pixels to be interpolated (specified in elements !not! in pixels - e.g., interpolationLocations[0] = firstInterpolationPixel * channels), one for each target pixel, with range [0, (targetWidth - 1) * channels]
896 * @param interpolationFactors The two successive (left !and! right) interpolation factors for each left and right source pixel, with range [0, 128]
897 * @tparam tChannels The number of frame channels this function can handle, possible values are 1, 4
898 * @see interpolateRowHorizontal8BitPerChannel7BitPrecision().
899 */
900 template <unsigned int tChannels>
901 static void interpolateRowHorizontal8BitPerChannel7BitPrecisionNEON(const uint8_t* extendedSourceRow, uint8_t* targetRow, const unsigned int targetWidth, const unsigned int channels, const unsigned int* interpolationLocations, const uint8_t* interpolationFactors);
902
903 /**
904 * Applies a (horizontal) linear interpolation for one row with arbitrary data type.
905 * This function applies NEON instructions.
906 * @param extendedSourceRow The source row extended with a copy of the last pixel for which the interpolation will be applied, must be valid
907 * @param targetRow The target row receiving the interpolation result, must be valid
908 * @param targetWidth The with of the target row in pixel, with range [8, infinity)
909 * @param channels The number of frame channels, must be identical with 'tChannels', possible values are 1, 4
910 * @param interpolationLocations The successive locations within the source row defining the location of the left pixels to be interpolated (specified in elements !not! in pixels - e.g., interpolationLocations[0] = firstInterpolationPixel * channels), one for each target pixel, with range [0, (targetWidth - 1) * channels]
911 * @param interpolationFactorsRight The right interpolation factors for each right source pixel, with range [0, 1]
912 * @tparam T The data type of each element, should be 'float'
913 * @tparam tChannels The number of frame channels this function can handle, should be 1
914 * @see interpolateRowHorizontal8BitPerChannel7BitPrecision().
915 */
916 template <typename T, unsigned int tChannels>
917 static void interpolateRowHorizontalNEON(const T* extendedSourceRow, T* targetRow, const unsigned int targetWidth, const unsigned int channels, const unsigned int* interpolationLocations, const float* interpolationFactorsRight);
918
919 /**
920 * Rescales a subset of a given frame with 8 bit per channel by a bilinear interpolation.
921 * This function applies NEON instructions and uses interpolation factors with 7 bit precision.
922 * @param source The image data of the source frame to be resized, must be valid
923 * @param target The target frame buffer receiving the interpolated (resized) source frame, must be valid
924 * @param sourceWidth Width of the source frame in pixel, with range [2, 65.535]
925 * @param sourceHeight Height of the source frame in pixel, with range [1, 65.535]
926 * @param targetWidth Width of the target frame in pixel, with range [tMinimalTargetWidth, 65.535]
927 * @param targetHeight Height of the target frame in pixel, with range [1, infinity)
928 * @param channels The number of channels both frames have, with range [1, infinity)
929 * @param sourceX_s_targetX The horizontal scale factor converting a location in the target frame to a location in the source frame (xSource = sourceX_s_targetX * xTarget), with range (0, sourceWidth/targetWidth]
930 * @param sourceY_s_targetY The vertical scale factor converting a location in the target frame to a location in the source frame (ySource = sourceY_s_targetY * yTarget), with range (0, sourceHeight/targetHeight]
931 * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
932 * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
933 * @param firstTargetRow The first target row to be handled, with range [0, targetHeight)
934 * @param numberTargetRows The number of target row to be handled, with range [1, targetHeight - firstTargetRow]
935 * @see interpolateRowVertical8BitPerChannel7BitPrecisionNEON(), interpolateRowHorizontal8BitPerChannel7BitPrecisionNEON().
936 */
937 static void scale8BitPerChannelSubset7BitPrecisionNEON(const uint8_t* source, uint8_t* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int channels, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows);
938
939#endif // OCEAN_HARDWARE_NEON_VERSION >= 10
940
941 /**
942 * Resizes a subset of a given frame with arbitrary data type by a bilinear interpolation.
943 * @param source The image data of the source frame to be resized, must be valid
944 * @param target The target frame buffer receiving the interpolated (resized) source frame, must be valid
945 * @param sourceWidth Width of the source frame in pixel, with range [1, infinity)
946 * @param sourceHeight Height of the source frame in pixel, with range [1, infinity)
947 * @param targetWidth Width of the target frame in pixel, with range [1, infinity)
948 * @param targetHeight Height of the target frame in pixel, with range [1, infinity)
949 * @param sourceX_s_targetX The horizontal scale factor converting a location in the target frame to a location in the source frame (xSource = sourceX_s_targetX * xTarget), with range (0, sourceWidth/targetWidth]
950 * @param sourceY_s_targetY The vertical scale factor converting a location in the target frame to a location in the source frame (ySource = sourceY_s_targetY * yTarget), with range (0, sourceHeight/targetHeight]
951 * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
952 * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
953 * @param firstTargetRow The first target row to be handled, with range [0, targetHeight)
954 * @param numberTargetRows The number of target row to be handled, with range [1, targetHeight - firstTargetRow]
955 * @tparam T The data type of each pixel channel, e.g., float, double, int, short, ...
956 * @tparam TScale The data type of the internal scaling factors to be used, should be 'float' or 'double'
957 * @tparam tChannels Number of frame channels, with range [0, infinity)
958 */
959 template <typename T, typename TScale, unsigned int tChannels>
960 static void scaleSubset(const T* source, T* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows);
961
962 /**
963 * Rotates a subset of a given frame by a bilinear interpolation.
964 * @param source The source frame to be rotated, must be valid
965 * @param target The target frame which will receive the rotated image, with same frame type as the source frame, must be valid
966 * @param width The width of the source and target frame in pixel, with range [1, infinity)
967 * @param height The height of the source and target frame in pixel, with range [1, infinity)
968 * @param horizontalAnchorPosition Position of the rotation anchor in the horizontal direction, with range (-infinity, infinity)
969 * @param verticalAnchorPosition Position of the rotation anchor in the vertical direction, with range (-infinity, infinity)
970 * @param angle The counter clockwise rotation angle in radian, with range [0, 2PI)
971 * @param borderColor The color of border pixels for which now visual content exists, provide one value for each channel, nullptr to use 0x00 for each channel
972 * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
973 * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
974 * @param firstTargetRow The first row of the target frame to be handled, with range [0, height)
975 * @param numberTargetRows The number of rows in the target frame to be handled, with range [1, height - firstTargetRow]
976 * @tparam tChannels Number of frame channels, with range [1, infinity)
977 */
978 template <unsigned int tChannels>
979 static void rotate8BitPerChannelSubset(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const Scalar horizontalAnchorPosition, const Scalar verticalAnchorPosition, const Scalar angle, const uint8_t* borderColor, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows);
980
981 /**
982 * Subset function to apply an affine transform to an N-channel, 8-bit unsigned image.
983 * The affine transform must be provided in the following form: `sourcePoint = source_A_target * targetPoint`
984 * This function does not apply SIMD instructions and can be used for any frame dimensions.
985 * Please note that here the affine transformation is specified as a 3-by-3 matrix (in contrast to the more commonly used 2-by-3 matrix) and should take of the form:
986 * <pre>
987 * a c e
988 * b d f
989 * 0 0 1
990 * </pre>
991 * However, this function disregards the last row completely and only uses the top two rows, i.e., the elements a through f.
992 * @param source Input frame that will be transformed
993 * @param sourceWidth Width of both source images in pixel, with range [1, infinity)
994 * @param sourceHeight Height of both source images pixel, with range [1, infinity)
995 * @param source_A_target Affine transformation which is applied to the source frame.
996 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
997 * @param target Output frame using the given affine transform
998 * @param targetWidth The width of the target image in pixel, with range [1, infinity)
999 * @param targetHeight The height of the target image in pixel, with range [1, infinity)
1000 * @param firstTargetRow The first target row to be handled
1001 * @param numberTargetRows Number of target rows to be handled
1002 * @param sourcePaddingElements The number of padding elements at the end of each source frame, in elements, with range [0, infinity)
1003 * @param targetPaddingElements The number of padding elements at the end of each target frame, in elements, with range [0, infinity)
1004 * @tparam tChannels Number of frame channels, with range [1, infinity)
1005 * @see affine8BitPerChannelSSESubset(), affine8BitPerChannelNEONSubset()
1006 */
1007 template <unsigned int tChannels>
1008 static inline void affine8BitPerChannelSubset(const uint8_t* source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3* source_A_target, const uint8_t* borderColor, uint8_t* target, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int firstTargetRow, const unsigned int numberTargetRows, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements);
1009
1010 /**
1011 * Transforms an 8 bit per channel frame using the given homography.
1012 * The homography must provide the following transformation: inputPoint = homography * outputPoint
1013 * This function does not apply SIMD instructions and can be used for any frame dimensions.
1014 * @param input The input frame that will be transformed
1015 * @param inputWidth Width of both input images in pixel, with range [1, infinity)
1016 * @param inputHeight Height of both input images pixel, with range [1, infinity)
1017 * @param input_H_output Homography used to transform the given input frame by following equation: inputPoint = input_H_output * outputPoint, must be valid
1018 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
1019 * @param output The output frame using the given homography
1020 * @param outputWidth The width of the output image in pixel, with range [1, infinity)
1021 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
1022 * @param inputPaddingElements The number of padding elements at the end of each input frame row, in elements, with range [0, infinity)
1023 * @param outputPaddingElements The number of padding elements at the end of each output frame row, in elements, with range [0, infinity)
1024 * @param firstOutputRow The first output row to be handled, with range [0, height - 1]
1025 * @param numberOutputRows Number of output rows to be handled, with range [1, height - firstOutputRow]
1026 * @tparam tChannels Number of frame channels, with range [1, infinity)
1027 * @see homography8BitPerChannelSSESubset(), homography8BitPerChannelNEONSubset()
1028 */
1029 template <unsigned int tChannels>
1030 static inline void homography8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* input_H_output, const uint8_t* borderColor, uint8_t* output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows);
1031
1032 /**
1033 * Transforms a frame with (almost) arbitrary pixel format using the given homography.
1034 * This function does not apply SIMD instructions and can be used for any frame dimensions.
1035 * @param input The input frame that will be transformed
1036 * @param inputWidth Width of both input images in pixel, with range [1, infinity)
1037 * @param inputHeight Height of both input images pixel, with range [1, infinity)
1038 * @param input_H_output Homography used to transform the given input frame by following equation: inputPoint = input_H_output * outputPoint, must be valid
1039 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
1040 * @param output The output frame using the given homography
1041 * @param outputWidth The width of the output image in pixel, with range [1, infinity)
1042 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
1043 * @param inputPaddingElements The number of padding elements at the end of each input frame row, in elements, with range [0, infinity)
1044 * @param outputPaddingElements The number of padding elements at the end of each output frame row, in elements, with range [0, infinity)
1045 * @param firstOutputRow The first output row to be handled, with range [0, height - 1]
1046 * @param numberOutputRows Number of output rows to be handled, with range [1, height - firstOutputRow]
1047 * @tparam T Data type of each pixel channel, e.g., float, double, int
1048 * @tparam tChannels Number of frame channels, with range [1, infinity)
1049 * @see homography8BitPerChannelSSESubset().
1050 */
1051 template <typename T, unsigned int tChannels>
1052 static inline void homographySubset(const T* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* input_H_output, const T* borderColor, T* output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows);
1053
1054#if defined(OCEAN_HARDWARE_SSE_VERSION) && OCEAN_HARDWARE_SSE_VERSION >= 41
1055
1056 /**
1057 * Subset function to apply an affine transform to an N-channel, 8-bit unsigned image (using SSE).
1058 * This function applies SSE instructions.<br>
1059 * Beware: The target width 'targetWidth' must be >= 4, use affine8BitPerChannelSubset for small target frames
1060 * This function has the property: sourcePoint = source_A_target * targetPoint
1061 * Please note that here the affine transformation is specified as a 3-by-3 matrix (in contrast to the more commonly used 2-by-3 matrix) and should take of the form:
1062 * <pre>
1063 * a c e
1064 * b d f
1065 * 0 0 1
1066 * </pre>
1067 * However, this function disregards the last row completely and only uses the top two rows, i.e., the elements a through f.
1068 * @param source Input frame that will be transformed
1069 * @param sourceWidth Width of both source images in pixel, with range [1, infinity)
1070 * @param sourceHeight Height of both source images pixel, with range [1, infinity)
1071 * @param source_A_target Affine transformation which is applied to source frame.
1072 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
1073 * @param target The target frame where the result of the transformation will be stored
1074 * @param targetWidth The width of the target image in pixel, with range [4, infinity)
1075 * @param targetHeight The height of the target image in pixel, with range [1, infinity)
1076 * @param firstTargetRow The first target row to be handled
1077 * @param numberTargetRows Number of target rows to be handled
1078 * @param sourcePaddingElements The number of padding elements at the end of each source frame, in elements, with range [0, infinity)
1079 * @param targetPaddingElements The number of padding elements at the end of each target frame, in elements, with range [0, infinity)
1080 * @tparam tChannels Number of frame channels
1081 * @see affine8BitPerChannelSubset(), affine8BitPerChannelSubsetNEON().
1082 */
1083 template <unsigned int tChannels>
1084 static inline void affine8BitPerChannelSSESubset(const uint8_t* source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3* source_A_target, const uint8_t* borderColor, uint8_t* target, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int firstTargetRow, const unsigned int numberTargetRows, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements);
1085
1086 /**
1087 * Transforms an 8 bit per channel frame using the given homography.
1088 * This function applies SSE instructions.<br>
1089 * Beware: The output width 'outputWidth' must be >= 4, use homography8BitPerChannelSubset for small output frames
1090 * @param input The input frame that will be transformed, must be valid
1091 * @param inputWidth Width of both input images in pixel, with range [1, infinity)
1092 * @param inputHeight Height of both input images pixel, with range [1, infinity)
1093 * @param input_H_output Homography used to transform the given input frame by following equation: inputPoint = input_H_output * outputPoint, must be valid
1094 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
1095 * @param output The output frame using the given homography, must be valid
1096 * @param outputWidth The width of the output image in pixel, with range [4, infinity)
1097 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
1098 * @param inputPaddingElements The number of padding elements at the end of each input frame row, in elements, with range [0, infinity)
1099 * @param outputPaddingElements The number of padding elements at the end of each output frame row, in elements, with range [0, infinity)
1100 * @param firstOutputRow The first output row to be handled, with range [0, outputHeight - 1]
1101 * @param numberOutputRows Number of output rows to be handled, with range [1, outputHeight - firstOutputRow]
1102 * @tparam tChannels Number of frame channels, with range [1, infinity)
1103 * @see homography8BitPerChannelSubset().
1104 */
1105 template <unsigned int tChannels>
1106 static inline void homography8BitPerChannelSSESubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* input_H_output, const uint8_t* borderColor, uint8_t* output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows);
1107
1108 /**
1109 * Interpolates 4 independent pixels concurrently based on already known locations (top-left, top-right, bottom-left, and bottom-right) and interpolation factors for the source pixels.
1110 * This function also supports to interpolate the pixel values for a subset of the four pixels only, valid pixels will be interpolated, invalid pixels receive a defined border color.
1111 * @param source The source image in which the four independent pixels are located, must be valid
1112 * @param offsetsTopLeft The four offsets within the source image for the four top-left pixels used for the interpolation, with range [0, (width * tChannels + sourcePaddingElements) * (height - 1) + width * tChannels), or the corresponding validPixel information is 0x00000000
1113 * @param offsetsTopRight The four offsets within the source image for the four top-right pixels used for the interpolation, with range [0, (width * tChannels + sourcePaddingElements) * (height - 1) + width * tChannels), or the corresponding validPixel information is 0x00000000
1114 * @param offsetsBottomLeft The four offsets within the source image for the four bottom-right pixels used for the interpolation, with range [0, (width * tChannels + sourcePaddingElements) * (height - 1) + width * tChannels), or the corresponding validPixel information is 0x00000000
1115 * @param offsetsBottomRight The four offsets within the source image for the four bottom-right pixels used for the interpolation, with range [0, (width * tChannels + sourcePaddingElements) * (height - 1) + width * tChannels), or the corresponding validPixel information is 0x00000000
1116 * @param validPixels For boolean states specifying whether which of the given four pixels will be interpolated and which one will receive the defined border color, 0x00000000 for invalid pixels everything else for valid pixels (e.g., 0xFFFFFFFF)
1117 * @param borderColor The border color that will be assigned to each resulting pixel that is invalid (for which the corresponding validPixel information is 0x00000000
1118 * @param m128_factorsRight The horizontal interpolation factors for right pixels, with range [0, 128], 128 to use the color information of the right pixels only, 0 to use the color information of the left pixels only
1119 * @param m128_factorsBottom The vertical interpolation factors for bottom pixels, with range [0, 128], 128 to use the color information of the bottom pixels only, 0 to use the color information of the top pixels only
1120 * @param targetPositionPixels The buffer that will receive the interpolated color values, must be valid
1121 * @tparam tChannels The number of frame channels, with range [1, infinity)
1122 */
1123 template <unsigned int tChannels>
1124 static OCEAN_FORCE_INLINE void interpolate4Pixels8BitPerChannelSSE(const uint8_t* source, const unsigned int offsetsTopLeft[4], const unsigned int offsetsTopRight[4], const unsigned int offsetsBottomLeft[4], const unsigned int offsetsBottomRight[4], const unsigned int validPixels[4], const typename DataType<uint8_t, tChannels>::Type& borderColor, const __m128i& m128_factorsRight, const __m128i& m128_factorsBottom, typename DataType<uint8_t, tChannels>::Type* targetPositionPixels);
1125
1126 /**
1127 * Interpolates 4 independent pixels concurrently based on already known locations (top-left, top-right, bottom-left, and bottom-right) and interpolation factors for the source pixels.
1128 * This function also supports to interpolate the pixel values for a subset of the four pixels only, valid pixels will be interpolated, invalid pixels receive a defined border color.
1129 * @param m128_sourcesTopLeft The pixel values of the four top left pixels, starting at the first byte may contain unused bytes at the end, e.g., RGBARGBARGBARGBA or YUVYUVYUVYUV----
1130 * @param m128_sourcesTopRight The pixel values of the four top right pixels, starting at the first byte may contain unused bytes at the end
1131 * @param m128_sourcesBottomLeft The pixel values of the four bottom left pixels, starting at the first byte may contain unused bytes at the end
1132 * @param m128_sourcesBottomRight The pixel values of the four bottom right pixels, starting at the first byte may contain unused bytes at the end
1133 * @param m128_factorsTopLeft The four interpolation factors of the four top left pixels, with ranges [0, 128 * 128], so that (m128_factorsTopLeft + m128_factorsTopRight + m128_factorsBottomLeft + m128_factorsBottomRight) == (128 * 128)
1134 * @param m128_factorsTopRight The four interpolation factors of the four top right pixels, with ranges [0, 128 * 128], so that (m128_factorsTopLeft + m128_factorsTopRight + m128_factorsBottomLeft + m128_factorsBottomRight) == (128 * 128)
1135 * @param m128_factorsBottomLeft The four interpolation factors of the four bottom left pixels, with ranges [0, 128 * 128], so that (m128_factorsTopLeft + m128_factorsTopRight + m128_factorsBottomLeft + m128_factorsBottomRight) == (128 * 128)
1136 * @param m128_factorsBottomRight The four interpolation factors of the four bottom right pixels, with ranges [0, 128 * 128], so that (m128_factorsTopLeft + m128_factorsTopRight + m128_factorsBottomLeft + m128_factorsBottomRight) == (128 * 128)
1137 * @return The resulting interpolated pixel values, starting at the first byte may contain unused bytes at the end, e.g., RGBARGBARGBARGBA or YUVYUVYUVYUV----
1138 * @tparam tChannels The number of frame channels, with range [3, 4]
1139 */
1140 template <unsigned int tChannels>
1141 static OCEAN_FORCE_INLINE __m128i interpolate4Pixels8BitPerChannelSSE(const __m128i& m128_sourcesTopLeft, const __m128i& m128_sourcesTopRight, const __m128i& m128_sourcesBottomLeft, const __m128i& m128_sourcesBottomRight, const __m128i& m128_factorsTopLeft, const __m128i& m128_factorsTopRight, const __m128i& m128_factorsBottomLeft, const __m128i& m128_factorsBottomRight);
1142
1143#endif // OCEAN_HARDWARE_SSE_VERSION
1144
1145#if defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
1146
1147 /**
1148 * Subset function to apply an affine transform to an N-channel, 8-bit unsigned image (using NEON).
1149 * This function applies NEON instructions.<br>
1150 * This one has the property: sourcePoint = source_A_target * targetPoint
1151 * Beware: The target width 'targetWidth' must be >= 4, use affine8BitPerChannelSubset for small target frames
1152 * Please note that here the affine transformation is specified as a 3-by-3 matrix (in contrast to the more commonly used 2-by-3 matrix) and should take of the form:
1153 * <pre>
1154 * a c e
1155 * b d f
1156 * 0 0 1
1157 * </pre>
1158 * However, this function disregards the last row completely and only uses the top two rows, i.e., the elements a through f.
1159 * @param source The source frame that will be transformed
1160 * @param sourceWidth Width of both source images in pixel, with range [1, infinity)
1161 * @param sourceHeight Height of both source images pixel, with range [1, infinity)
1162 * @param source_A_target Affine transform used to transform the given source frame.
1163 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
1164 * @param target The target frame using the given affine transform
1165 * @param targetWidth The width of the target image in pixel, with range [4, infinity)
1166 * @param targetHeight The height of the target image in pixel, with range [1, infinity)
1167 * @param firstTargetRow The first target row to be handled
1168 * @param numberTargetRows Number of target rows to be handled
1169 * @param sourcePaddingElements The number of padding elements at the end of each source frame, in elements, with range [0, infinity)
1170 * @param targetPaddingElements The number of padding elements at the end of each target frame, in elements, with range [0, infinity)
1171 * @tparam tChannels Number of frame channels, with range [1, infinity)
1172 * @see homography8BitPerChannelSubset().
1173 */
1174 template <unsigned int tChannels>
1175 static inline void affine8BitPerChannelNEONSubset(const uint8_t* source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3* source_A_target, const uint8_t* borderColor, uint8_t* target, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int firstTargetRow, const unsigned int numberTargetRows, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements);
1176
1177 /**
1178 * Transforms an 8 bit per channel frame using the given homography.
1179 * This function applies NEON instructions.<br>
1180 * Beware: The output width 'outputWidth' must be >= 4, use homography8BitPerChannelSubset for small output frames.
1181 * @param input The input frame that will be transformed
1182 * @param inputWidth Width of both input images in pixel, with range [1, infinity)
1183 * @param inputHeight Height of both input images pixel, with range [1, infinity)
1184 * @param input_H_output Homography used to transform the given input frame by following equation: inputPoint = input_H_output * outputPoint, must be valid
1185 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
1186 * @param output The output frame using the given homography
1187 * @param outputWidth The width of the output image in pixel, with range [4, infinity)
1188 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
1189 * @param inputPaddingElements The number of padding elements at the end of each input frame row, in elements, with range [0, infinity)
1190 * @param outputPaddingElements The number of padding elements at the end of each output frame row, in elements, with range [0, infinity)
1191 * @param firstOutputRow The first output row to be handled, with range [0, outputHeight - 1]
1192 * @param numberOutputRows Number of output rows to be handled, with range [1, outputHeight - firstOutputRow]
1193 * @tparam tChannels Number of frame channels, with range [1, infinity)
1194 * @see homography8BitPerChannelSubset().
1195 */
1196 template <unsigned int tChannels>
1197 static inline void homography8BitPerChannelNEONSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* input_H_output, const uint8_t* borderColor, uint8_t* output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows);
1198
1199 /**
1200 * Interpolates 4 independent pixels concurrently based on already known locations (top-left, top-right, bottom-left, and bottom-right) and interpolation factors for the source pixels.
1201 * This function also supports to interpolate the pixel values for a subset of the four pixels only, valid pixels will be interpolated, invalid pixels receive a defined border color.
1202 * @param source The source image in which the four independent pixels are located, must be valid
1203 * @param offsetsTopLeftElements The four offsets within the source image for the four top-left pixels used for the interpolation, in elements, with ranges [0, strideElements * height), or the corresponding validPixel information is 0x00000000
1204 * @param offsetsTopRightElements The four offsets within the source image for the four top-right pixels used for the interpolation, in elements, with ranges [0, strideElements * height), or the corresponding validPixel information is 0x00000000
1205 * @param offsetsBottomLeftElements The four offsets within the source image for the four bottom-right pixels used for the interpolation, in elements, with ranges [0, strideElements * height), or the corresponding validPixel information is 0x00000000
1206 * @param offsetsBottomRightElements The four offsets within the source image for the four bottom-right pixels used for the interpolation, in elements, with ranges [0, strideElements * height), or the corresponding validPixel information is 0x00000000
1207 * @param validPixels For boolean states specifying whether which of the given four pixels will be interpolated and which one will receive the defined border color, 0x00000000 for invalid pixels everything else for valid pixels (e.g., 0xFFFFFFFF)
1208 * @param borderColor The border color that will be assigned to each resulting pixel that is invalid (for which the corresponding validPixel information is 0x00000000
1209 * @param m128_factorsRight The horizontal interpolation factors for right pixels, with range [0, 128], 128 to use the color information of the right pixels only, 0 to use the color information of the left pixels only
1210 * @param m128_factorsBottom The vertical interpolation factors for bottom pixels, with range [0, 128], 128 to use the color information of the bottom pixels only, 0 to use the color information of the top pixels only
1211 * @param targetPositionPixels The buffer that will receive the interpolated color values, must be valid
1212 * @tparam tChannels The number of frame channels, with range [1, infinity)
1213 */
1214 template <unsigned int tChannels>
1215 static OCEAN_FORCE_INLINE void interpolate4Pixels8BitPerChannelNEON(const uint8_t* source, const unsigned int offsetsTopLeftElements[4], const unsigned int offsetsTopRightElements[4], const unsigned int offsetsBottomLeftElements[4], const unsigned int offsetsBottomRightElements[4], const unsigned int validPixels[4], const typename DataType<uint8_t, tChannels>::Type& borderColor, const uint32x4_t& m128_factorsRight, const uint32x4_t& m128_factorsBottom, typename DataType<uint8_t, tChannels>::Type* targetPositionPixels);
1216
1217 /**
1218 * Interpolates 8 independent pixels concurrently of a 1 channel frame, the source pixel locations must be known already (top-left, top-right, bottom-left, and bottom-right), further the interpolation factors must be known already.
1219 * @param topLeft_u_8x8 The 8 top left pixel values to be used for interpolation
1220 * @param topRight_u_8x8 The 8 top right pixel values to be used for interpolation
1221 * @param bottomLeft_u_8x8 The 8 bottom left pixel values to be used for interpolation
1222 * @param bottomRight_u_8x8 The 8 bottom right pixel values to be used for interpolation
1223 * @param factorsRight_factorsBottom_128_u_8x16 The eight horizontal interpolation factors for right pixels, and the eight vertical interpolation factors for the bottom pixels, with range [0, 128], 128 to use the color information of the right pixels only, 0 to use the color information of the left pixels only
1224 * @param targetPositionPixels The buffer that will receive the interpolated color values, must be valid
1225 */
1226 static OCEAN_FORCE_INLINE void interpolate8Pixels1Channel8BitNEON(const uint8x8_t& topLeft_u_8x8, const uint8x8_t& topRight_u_8x8, const uint8x8_t& bottomLeft_u_8x8, const uint8x8_t& bottomRight_u_8x8, const uint8x16_t& factorsRight_factorsBottom_128_u_8x16, uint8_t* targetPositionPixels);
1227
1228 /**
1229 * Interpolates 4 independent 4-channel pixels using widening byte multiply.
1230 * Accepts pre-gathered pixel data in NEON registers, eliminating the
1231 * NEON->stack->scalar->stack->NEON roundtrip of the offset-based overload.
1232 * Uses vmull_u8/vmlal_u8 widening pattern (~22 NEON ops vs ~56 in the u32 decomposition path).
1233 * @param topLeftPixels_u8x16 The 4 top-left pixels packed as 16 bytes [R0G0B0A0 R1G1B1A1 R2G2B2A2 R3G3B3A3]
1234 * @param topRightPixels_u8x16 The 4 top-right pixels packed identically
1235 * @param bottomLeftPixels_u8x16 The 4 bottom-left pixels packed identically
1236 * @param bottomRightPixels_u8x16 The 4 bottom-right pixels packed identically
1237 * @param m128_factorsRight The horizontal interpolation factors, one per pixel, with range [0, 128]
1238 * @param m128_factorsBottom The vertical interpolation factors, one per pixel, with range [0, 128]
1239 * @param targetPositionPixels The buffer that will receive the interpolated color values, must be valid
1240 * @param useOptimizedNEONFactorReplication True to use the optimized TBL-based factor replication (AArch64 only); False to use the original narrow+zip factor replication
1241 */
1242 static OCEAN_FORCE_INLINE void interpolate4Pixels4Channel8BitPerChannelNEON(const uint8x16_t& topLeftPixels_u8x16, const uint8x16_t& topRightPixels_u8x16, const uint8x16_t& bottomLeftPixels_u8x16, const uint8x16_t& bottomRightPixels_u8x16, const uint32x4_t& m128_factorsRight, const uint32x4_t& m128_factorsBottom, typename DataType<uint8_t, 4u>::Type* targetPositionPixels, const bool useOptimizedNEONFactorReplication = false);
1243
1244#endif // OCEAN_HARDWARE_SSE_VERSION
1245
1246 /**
1247 * Transforms an 8 bit per channel frame using the given homographies.
1248 * @param input The input frame that will be transformed
1249 * @param inputWidth Width of both input images in pixel, with range [1, infinity)
1250 * @param inputHeight Height of both input images pixel, with range [1, infinity)
1251 * @param homographies Homographies used to transform the given input frame
1252 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
1253 * @param output The output frame using the given homography
1254 * @param outputQuadrantCenterX The horizontal position of the four quadrants in the output frame (the local center not respecting the optional outputOrigin parameter), with range [0, outputWidth)
1255 * @param outputQuadrantCenterY The vertical position of the four quadrants in the output frame (the local center not respecting the optional outputOrigin parameter), with range [0, outputHeight)
1256 * @param outputOriginX The horizontal coordinate of the output frame's origin
1257 * @param outputOriginY The vertical coordinate of the output frame's origin
1258 * @param outputWidth The width of the output image in pixel, with range [1, infinity)
1259 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
1260 * @param inputPaddingElements The number of padding elements at the end of each input frame, in elements, with range [0, infinity)
1261 * @param outputPaddingElements The number of padding elements at the end of each output frame, in elements, with range [0, infinity)
1262 * @param firstOutputRow The first output row to be handled
1263 * @param numberOutputRows Number of output rows to be handled
1264 * @tparam tChannels Number of frame channels
1265 */
1266 template <unsigned int tChannels>
1267 static inline void homographies8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* homographies, const uint8_t* borderColor, uint8_t* output, const Scalar outputQuadrantCenterX, const Scalar outputQuadrantCenterY, const int outputOriginX, const int outputOriginY, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows);
1268
1269 /**
1270 * Transforms an 8 bit per channel frame using the given homography.
1271 * @param input The input frame that will be transformed, must be valid
1272 * @param inputWidth Width of both input images in pixel, with range [1, infinity)
1273 * @param inputHeight Height of both input images pixel, with range [1, infinity)
1274 * @param input_H_output Homography used to transform the given input frame by following equation: inputPoint = input_H_output * outputPoint, must be valid
1275 * @param output The output frame resulting by application of the given homography, must be valid
1276 * @param outputMask Mask frame with 8 bit per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame
1277 * @param maskValue 8 bit mask values for reference pixels lying inside the given camera frame, reference pixels lying outside the camera frame will be assigned with (0xFF - maskValue)
1278 * @param outputWidth The width of the output image in pixel, with range [1, infinity)
1279 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
1280 * @param inputPaddingElements The number of padding elements at the end of each input row, in elements, with range [0, infinity)
1281 * @param outputPaddingElements The number of padding elements at the end of each output row, in elements, with range [0, infinity)
1282 * @param outputMaskPaddingElements The number of padding elements at the end of output mask input row, in elements, with range [0, infinity)
1283 * @param firstOutputRow The first output row to be handled
1284 * @param numberOutputRows Number of output rows to be handled
1285 * @tparam tChannels Number of frame channels, with range [1, infinity)
1286 */
1287 template <unsigned int tChannels>
1288 static inline void homographyMask8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* input_H_output, uint8_t* output, uint8_t* outputMask, const uint8_t maskValue, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows);
1289
1290 /**
1291 * Transforms an 8 bit per channel frame using the given homography.
1292 * @param input The input frame that will be transformed
1293 * @param inputWidth Width of both input images in pixel, with range [1, infinity)
1294 * @param inputHeight Height of both input images pixel, with range [1, infinity)
1295 * @param homographies Homographies used to transform the given input frame
1296 * @param output The output frame resulting by application of the given homography
1297 * @param outputMask Mask frame with 8 bit per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame
1298 * @param maskValue 8 bit mask values for reference pixels lying inside the given camera frame, reference pixels lying outside the camera frame will be assigned with (0xFF - maskValue)
1299 * @param outputQuadrantCenterX The horizontal position of the four quadrants in the output frame (the local center not respecting the optional outputOrigin parameter), with range [0, outputWidth)
1300 * @param outputQuadrantCenterY The vertical position of the four quadrants in the output frame (the local center not respecting the optional outputOrigin parameter), with range [0, outputHeight)
1301 * @param outputOriginX The horizontal coordinate of the output frame's origin
1302 * @param outputOriginY The vertical coordinate of the output frame's origin
1303 * @param outputWidth The width of the output image in pixel, with range [1, infinity)
1304 * @param outputHeight The height of the output image in pixel, with range [1, infinity)
1305 * @param inputPaddingElements The number of padding elements at the end of each input row, in elements, with range [0, infinity)
1306 * @param outputPaddingElements The number of padding elements at the end of each output row, in elements, with range [0, infinity)
1307 * @param outputMaskPaddingElements The number of padding elements at the end of each row of the output mask, in elements, with range [0, infinity)
1308 * @param firstOutputRow The first output row to be handled
1309 * @param numberOutputRows Number of output rows to be handled
1310 * @tparam tChannels Number of frame channels
1311 */
1312 template <unsigned int tChannels>
1313 static inline void homographiesMask8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* homographies, uint8_t* output, uint8_t* outputMask, const uint8_t maskValue, const Scalar outputQuadrantCenterX, const Scalar outputQuadrantCenterY, const int outputOriginX, const int outputOriginY, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows);
1314
1315 /**
1316 * Transforms an 8 bit per channel frame using the given homography.
1317 * @param inputCamera The pinhole camera profile to be applied for the input frame
1318 * @param outputCamera The pinhole camera profile to be applied for the output frame
1319 * @param outputCameraDistortionLookup The distortion lookup table of the of the output camera
1320 * @param input The input frame that will be transformed
1321 * @param normalizedHomography The homography used to transform the given input frame specified in normalized camera coordinates
1322 * @param useDistortionParameters True, to apply the distortion parameters of the camera profile
1323 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
1324 * @param output The output frame resulting by application of the given homography
1325 * @param inputPaddingElements The number of padding elements at the end of each input row, in elements, with range [0, infinity)
1326 * @param outputPaddingElements The number of padding elements at the end of each output row, in elements, with range [0, infinity)
1327 * @param firstRow The first row to be handled
1328 * @param numberRows Number of rows to be handled
1329 * @tparam tChannels Number of frame channels
1330 */
1331 template <unsigned int tChannels>
1332 static void homographyWithCamera8BitPerChannelSubset(const PinholeCamera* inputCamera, const PinholeCamera* outputCamera, const PinholeCamera::DistortionLookup* outputCameraDistortionLookup, const uint8_t* input, const SquareMatrix3* normalizedHomography, const bool useDistortionParameters, const uint8_t* borderColor, uint8_t* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows);
1333
1334 /**
1335 * Transforms an 8 bit per channel frame using the given homography.
1336 * @param inputCamera The pinhole camera profile to be applied for the input frame
1337 * @param outputCamera The pinhole camera profile to be applied for the output frame
1338 * @param outputCameraDistortionLookup The distortion lookup table of the of the output camera
1339 * @param input The input frame that will be transformed, must be valid
1340 * @param inputPaddingElements The number of padding elements at the end of each input row, in elements, with range [0, infinity)
1341 * @param normalizedHomography The homography used to transform the given input frame specified in normalized camera coordinates
1342 * @param output The output frame resulting by application of the given homography
1343 * @param outputMask Mask frame with 8 bit per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame
1344 * @param outputPaddingElements The number of padding elements at the end of each output row, in elements, with range [0, infinity)
1345 * @param outputMaskPaddingElements The number of padding elements at the end of each output mask row, in elements, with range [0, infinity)
1346 * @param maskValue 8 bit mask values for reference pixels lying inside the given camera frame, reference pixels lying outside the camera frame will be assigned with (0xFF - maskValue)
1347 * @param firstRow The first row to be handled
1348 * @param numberRows Number of rows to be handled
1349 * @tparam tChannels Number of frame channels
1350 */
1351 template <unsigned int tChannels>
1352 static void homographyWithCameraMask8BitPerChannelSubset(const PinholeCamera* inputCamera, const PinholeCamera* outputCamera, const PinholeCamera::DistortionLookup* outputCameraDistortionLookup, const uint8_t* input, const unsigned int inputPaddingElements, const SquareMatrix3* normalizedHomography, uint8_t* output, uint8_t* outputMask, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, const uint8_t maskValue, const unsigned int firstRow, const unsigned int numberRows);
1353
1354 /**
1355 * Transforms a subset of a given input frame with uint8_t as element type into an output frame by application of an interpolation lookup table.
1356 * The output frame must have the same pixel format and pixel origin as the input frame.<br>
1357 * @param input The input frame which will be transformed, must be valid
1358 * @param inputWidth The width of the given input frame in pixel, with range [1, infinity)
1359 * @param inputHeight The height of the given input frame in pixel, with range [1, infinity)
1360 * @param input_LT_output The lookup table which defines the transformation from locations defined in the output frame to locations defined in the input frame (the lookup table stores the corresponding locations in the input frame), must be valid
1361 * @param offset True, if the lookup table store local offsets; False, if the lookup table stores absolute positions
1362 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
1363 * @param output Resulting output frame with frame dimension equal to the size of the given lookup table
1364 * @param inputPaddingElements Number of padding elements at the end of each input row, in elements, with range [0, infinity)
1365 * @param outputPaddingElements Number of padding elements at the end of each output row, in elements, with range [0, infinity)
1366 * @param firstRow First row to be handled, with range [0, input_LT_output->sizeY())
1367 * @param numberRows Number of rows to be handled, with range [1, input_LT_output->sizeY() - firstRow]
1368 * @tparam tChannels Number of channels of the frame, with range [1, infinity)
1369 */
1370 template <unsigned int tChannels>
1371 static void lookup8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable* input_LT_output, const bool offset, const uint8_t* borderColor, uint8_t* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows);
1372
1373 /**
1374 * Transforms a subset of a given input frame with arbitrary element type into an output frame by application of an interpolation lookup table.
1375 * The output frame must have the same pixel format and pixel origin as the input frame.<br>
1376 * @param input The input frame which will be transformed, must be valid
1377 * @param inputWidth The width of the given input frame in pixel, with range [1, infinity)
1378 * @param inputHeight The height of the given input frame in pixel, with range [1, infinity)
1379 * @param input_LT_output The lookup table which defines the transformation from locations defined in the output frame to locations defined in the input frame (the lookup table stores the corresponding locations in the input frame), must be valid
1380 * @param offset True, if the lookup table store local offsets; False, if the lookup table stores absolute positions
1381 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign T(0) to each channel
1382 * @param output Resulting output frame with frame dimension equal to the size of the given lookup table, must be valid
1383 * @param inputPaddingElements Number of padding elements at the end of each input row, in elements, with range [0, infinity)
1384 * @param outputPaddingElements Number of padding elements at the end of each output row, in elements, with range [0, infinity)
1385 * @param firstRow First row to be handled, with range [0, input_LT_output->sizeY())
1386 * @param numberRows Number of rows to be handled, with range [1, input_LT_output->sizeY() - firstRow]
1387 * @tparam T Data type of each pixel channel, must not be 'uint8_t'
1388 * @tparam tChannels Number of channels of the frame, with range [1, infinity)
1389 */
1390 template <typename T, unsigned int tChannels>
1391 static void lookupSubset(const T* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable* input_LT_output, const bool offset, const T* borderColor, T* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows);
1392
1393#if defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
1394
1395 /**
1396 * Transforms a subset of a given input frame into an output frame by application of an interpolation lookup table and uses NEON instructions.
1397 * The output frame must have the same pixel format and pixel origin as the input frame.<br>
1398 * @param input The input frame which will be transformed, must be valid
1399 * @param inputWidth The width of the given input frame in pixel, with range [1, infinity)
1400 * @param inputHeight The height of the given input frame in pixel, with range [1, infinity)
1401 * @param input_LT_output The lookup table which defines the transformation from locations defined in the output frame to locations defined in the input frame (the lookup table stores the corresponding locations in the input frame), with table width >= 4, must be valid
1402 * @param offset True, if the lookup table store local offsets; False, if the lookup table stores absolute positions
1403 * @param borderColor Color of undefined pixel positions, the size of the buffer must match to the number of channels, nullptr to assign 0x00 to each channel
1404 * @param output Resulting output frame with frame dimension equal to the size of the given lookup table
1405 * @param inputPaddingElements Number of padding elements at the end of each input row, in elements, with range [0, infinity)
1406 * @param outputPaddingElements Number of padding elements at the end of each output row, in elements, with range [0, infinity)
1407 * @param firstRow First row to be handled, with range [0, input_LT_output->sizeY())
1408 * @param numberRows Number of rows to be handled, with range [1, input_LT_output->sizeY() - firstRow]
1409 * @tparam tChannels Number of channels of the frame, with range [1, infinity)
1410 */
1411 template <unsigned int tChannels>
1412 static void lookup8BitPerChannelSubsetNEON(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable* input_LT_output, const bool offset, const uint8_t* borderColor, uint8_t* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows, const bool useOptimizedNEON = false, const bool useOptimizedBilinearValuesAndFactorCalculation = false, const bool useOptimizedNEONFactorReplication = false, const bool use4ChannelSamplingFastPaths = false);
1413
1414#endif // defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
1415
1416 /**
1417 * Transforms a given input frame into an output frame by application of an interpolation lookup table.
1418 * The output frame must have the same pixel format and pixel origin as the input frame.<br>
1419 * Input frame pixels lying outside the frame will be masked in the resulting output mask frame, further theses pixels are untouched in the output frame.<br>
1420 * @param input The input frame which will be transformed
1421 * @param inputWidth The width of the given input frame in pixel, with range [1, infinity)
1422 * @param inputHeight The height of the given input frame in pixel, with range [1, infinity)
1423 * @param input_LT_output The lookup table which defines the transformation from locations defined in the output frame to locations defined in the input frame (the lookup table stores the corresponding locations in the input frame), must be valid
1424 * @param offset True, if the lookup table store local offsets; False, if the lookup table stores absolute positions
1425 * @param output Resulting output frame with frame dimension equal to the size of the given lookup table
1426 * @param outputMask Resulting mask frame with 8 bit per pixel defining whether an output frame pixel has a valid corresponding pixel in the input frame
1427 * @param maskValue 8 bit mask values for pixels lying inside the input frame, pixels lying outside the input frame will be assigned with (0xFF - maskValue)
1428 * @param inputPaddingElements The number of padding elements at the end of each row of `input`, in elements, with range [0, infinity)
1429 * @param outputPaddingElements The number of padding elements at the end of each row of `output`, in elements, with range [0, infinity)
1430 * @param outputMaskPaddingElements The number of padding elements at the end of each row of `outputMask`, in elements, with range [0, infinity)
1431 * @param firstRow First row to be handled
1432 * @param numberRows Number of rows to be handled
1433 * @tparam tChannels Number of channels of the frame
1434 */
1435 template <unsigned int tChannels>
1436 static void lookupMask8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable* input_LT_output, const bool offset, uint8_t* output, uint8_t* outputMask, const uint8_t maskValue, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, const unsigned int firstRow, const unsigned int numberRows);
1437};
1438
1439inline bool FrameInterpolatorBilinear::Comfort::resize(Frame& frame, const unsigned int width, const unsigned int height, Worker* worker)
1440{
1441 ocean_assert(frame.isValid());
1442 ocean_assert(width >= 1u && height >= 1u);
1443
1444 Frame target(FrameType(frame, width, height));
1445
1446 if (!resize(frame, target, worker))
1447 {
1448 return false;
1449 }
1450
1451 target.setTimestamp(frame.timestamp());
1453
1454 frame = std::move(target);
1455 return true;
1456}
1457
1458template <typename TScalar>
1459bool FrameInterpolatorBilinear::Comfort::interpolatePixel8BitPerChannel(const uint8_t* frame, const unsigned int channels, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const PixelCenter pixelCenter, const VectorT2<TScalar>& position, uint8_t* result)
1460{
1461 ocean_assert(frame != nullptr);
1462 ocean_assert(channels >= 1u && channels <= 8u);
1463
1464 if (pixelCenter == PC_TOP_LEFT)
1465 {
1466 switch (channels)
1467 {
1468 case 1u:
1469 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<1u, PC_TOP_LEFT, TScalar>(frame, width, height, framePaddingElements, position, result);
1470 return true;
1471
1472 case 2u:
1473 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<2u, PC_TOP_LEFT, TScalar>(frame, width, height, framePaddingElements, position, result);
1474 return true;
1475
1476 case 3u:
1477 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<3u, PC_TOP_LEFT, TScalar>(frame, width, height, framePaddingElements, position, result);
1478 return true;
1479
1480 case 4u:
1481 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<4u, PC_TOP_LEFT, TScalar>(frame, width, height, framePaddingElements, position, result);
1482 return true;
1483
1484 case 5u:
1485 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<5u, PC_TOP_LEFT, TScalar>(frame, width, height, framePaddingElements, position, result);
1486 return true;
1487
1488 case 6u:
1489 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<6u, PC_TOP_LEFT, TScalar>(frame, width, height, framePaddingElements, position, result);
1490 return true;
1491
1492 case 7u:
1493 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<7u, PC_TOP_LEFT, TScalar>(frame, width, height, framePaddingElements, position, result);
1494 return true;
1495
1496 case 8u:
1497 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<8u, PC_TOP_LEFT, TScalar>(frame, width, height, framePaddingElements, position, result);
1498 return true;
1499
1500 default:
1501 break;
1502 }
1503 }
1504 else
1505 {
1506 ocean_assert(pixelCenter == PC_CENTER);
1507
1508 switch (channels)
1509 {
1510 case 1u:
1511 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<1u, PC_CENTER, TScalar>(frame, width, height, framePaddingElements, position, result);
1512 return true;
1513
1514 case 2u:
1515 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<2u, PC_CENTER, TScalar>(frame, width, height, framePaddingElements, position, result);
1516 return true;
1517
1518 case 3u:
1519 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<3u, PC_CENTER, TScalar>(frame, width, height, framePaddingElements, position, result);
1520 return true;
1521
1522 case 4u:
1523 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<4u, PC_CENTER, TScalar>(frame, width, height, framePaddingElements, position, result);
1524 return true;
1525
1526 case 5u:
1527 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<5u, PC_CENTER, TScalar>(frame, width, height, framePaddingElements, position, result);
1528 return true;
1529
1530 case 6u:
1531 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<6u, PC_CENTER, TScalar>(frame, width, height, framePaddingElements, position, result);
1532 return true;
1533
1534 case 7u:
1535 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<7u, PC_CENTER, TScalar>(frame, width, height, framePaddingElements, position, result);
1536 return true;
1537
1538 case 8u:
1539 FrameInterpolatorBilinear::interpolatePixel8BitPerChannel<8u, PC_CENTER, TScalar>(frame, width, height, framePaddingElements, position, result);
1540 return true;
1541
1542 default:
1543 break;
1544 }
1545 }
1546
1547 ocean_assert(false && "Invalid channel number");
1548 return false;
1549}
1550
1551template <typename TSource, typename TTarget, typename TScalar, typename TIntermediate>
1552bool FrameInterpolatorBilinear::Comfort::interpolatePixel(const TSource* frame, const unsigned int channels, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const PixelCenter pixelCenter, const VectorT2<TScalar>& position, TTarget* result, const TIntermediate& resultBias)
1553{
1554 ocean_assert(frame != nullptr);
1555 ocean_assert(channels >= 1u && channels <= 8u);
1556
1557 if (pixelCenter == PC_TOP_LEFT)
1558 {
1559 switch (channels)
1560 {
1561 case 1u:
1562 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 1u, PC_TOP_LEFT, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1563 return true;
1564
1565 case 2u:
1566 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 2u, PC_TOP_LEFT, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1567 return true;
1568
1569 case 3u:
1570 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 3u, PC_TOP_LEFT, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1571 return true;
1572
1573 case 4u:
1574 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 4u, PC_TOP_LEFT, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1575 return true;
1576
1577 case 5u:
1578 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 5u, PC_TOP_LEFT, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1579 return true;
1580
1581 case 6u:
1582 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 6u, PC_TOP_LEFT, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1583 return true;
1584
1585 case 7u:
1586 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 7u, PC_TOP_LEFT, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1587 return true;
1588
1589 case 8u:
1590 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 8u, PC_TOP_LEFT, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1591 return true;
1592
1593 default:
1594 break;
1595 }
1596 }
1597 else
1598 {
1599 ocean_assert(pixelCenter == PC_CENTER);
1600
1601 switch (channels)
1602 {
1603 case 1u:
1604 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 1u, PC_CENTER, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1605 return true;
1606
1607 case 2u:
1608 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 2u, PC_CENTER, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1609 return true;
1610
1611 case 3u:
1612 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 3u, PC_CENTER, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1613 return true;
1614
1615 case 4u:
1616 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 4u, PC_CENTER, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1617 return true;
1618
1619 case 5u:
1620 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 5u, PC_CENTER, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1621 return true;
1622
1623 case 6u:
1624 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 6u, PC_CENTER, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1625 return true;
1626
1627 case 7u:
1628 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 7u, PC_CENTER, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1629 return true;
1630
1631 case 8u:
1632 FrameInterpolatorBilinear::interpolatePixel<TSource, TTarget, 8u, PC_CENTER, TScalar, TIntermediate>(frame, width, height, framePaddingElements, position, result, resultBias);
1633 return true;
1634
1635 default:
1636 break;
1637 }
1638 }
1639
1640 ocean_assert(false && "Invalid channel number");
1641 return false;
1642}
1643
1644template <typename T, unsigned int tChannels>
1645inline void FrameInterpolatorBilinear::resize(const T* source, T* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker)
1646{
1647 ocean_assert(source != nullptr && target != nullptr);
1648 ocean_assert(sourceWidth >= 1u && sourceHeight >= 1u);
1649 ocean_assert(targetWidth >= 1u && targetHeight >= 1u);
1650
1651 const double sourceX_s_targetX = double(sourceWidth) / double(targetWidth);
1652 const double sourceY_s_targetY = double(sourceHeight) / double(targetHeight);
1653
1654 scale<T, tChannels>(source, target, sourceWidth, sourceHeight, targetWidth, targetHeight, sourceX_s_targetX, sourceY_s_targetY, sourcePaddingElements, targetPaddingElements, worker);
1655}
1656
1657template <typename T, unsigned int tChannels>
1658inline void FrameInterpolatorBilinear::scale(const T* source, T* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker)
1659{
1660 ocean_assert(source != nullptr && target != nullptr);
1661 ocean_assert(sourceWidth >= 1u && sourceHeight >= 1u);
1662 ocean_assert(targetWidth >= 1u && targetHeight >= 1u);
1663 ocean_assert(sourceX_s_targetX > 0.0);
1664 ocean_assert(sourceY_s_targetY > 0.0);
1665
1666 if (sourceWidth == targetWidth && sourceHeight == targetHeight && sourceX_s_targetX == 1.0 && sourceY_s_targetY == 1.0)
1667 {
1668 FrameConverter::subFrame<T>(source, target, sourceWidth, sourceHeight, targetWidth, targetHeight, tChannels, 0u, 0u, 0u, 0u, sourceWidth, sourceHeight, sourcePaddingElements, targetPaddingElements);
1669 return;
1670 }
1671
1672 if (std::is_same<T, uint8_t>::value)
1673 {
1674 // we have a SIMD-based optimized version for 'uint8_t' data types
1675
1676 scale8BitPerChannel<tChannels>((const uint8_t*)source, (uint8_t*)target, sourceWidth, sourceHeight, targetWidth, targetHeight, sourceX_s_targetX, sourceY_s_targetY, sourcePaddingElements, targetPaddingElements, worker);
1677 }
1678 else
1679 {
1680 using TScale = typename FloatTyper<T>::Type;
1681
1682 if (worker)
1683 {
1684 worker->executeFunction(Worker::Function::createStatic(&scaleSubset<T, TScale, tChannels>, source, target, sourceWidth, sourceHeight, targetWidth, targetHeight, sourceX_s_targetX, sourceY_s_targetY, sourcePaddingElements, targetPaddingElements, 0u, 0u), 0u, targetHeight);
1685 }
1686 else
1687 {
1688 scaleSubset<T, TScale, tChannels>(source, target, sourceWidth, sourceHeight, targetWidth, targetHeight, sourceX_s_targetX, sourceY_s_targetY, sourcePaddingElements, targetPaddingElements, 0u, targetHeight);
1689 }
1690 }
1691}
1692
1693template <unsigned int tChannels>
1694inline void FrameInterpolatorBilinear::affine8BitPerChannel(const uint8_t* source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3& source_A_target, const uint8_t* borderColor, uint8_t* target, const CV::PixelPositionI& targetOrigin, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker)
1695{
1696 // If applicable, apply an additional translation to the affine transformation.
1697 const SquareMatrix3 adjustedAffineTransform = source_A_target * SquareMatrix3(Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(Scalar(targetOrigin.x()), Scalar(targetOrigin.y()), 1));
1698
1699 if (worker)
1700 {
1701 if (targetWidth >= 4u)
1702 {
1703#if defined(OCEAN_HARDWARE_SSE_VERSION) && OCEAN_HARDWARE_SSE_VERSION >= 41
1704 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::affine8BitPerChannelSSESubset<tChannels>, source, sourceWidth, sourceHeight, &adjustedAffineTransform, borderColor, target, targetWidth, targetHeight, 0u, 0u, sourcePaddingElements, targetPaddingElements), 0, targetHeight, 8u, 9u, 20u);
1705 return;
1706#elif defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
1707 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::affine8BitPerChannelNEONSubset<tChannels>, source, sourceWidth, sourceHeight, &adjustedAffineTransform, borderColor, target, targetWidth, targetHeight, 0u, 0u, sourcePaddingElements, targetPaddingElements), 0, targetHeight, 8u, 9u, 20u);
1708 return;
1709#endif
1710 }
1711
1712 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::affine8BitPerChannelSubset<tChannels>, source, sourceWidth, sourceHeight, &adjustedAffineTransform, borderColor, target, targetWidth, targetHeight, 0u, 0u, sourcePaddingElements, targetPaddingElements), 0, targetHeight, 8u, 9u, 20u);
1713 }
1714 else
1715 {
1716 if (targetWidth >= 4u)
1717 {
1718#if defined(OCEAN_HARDWARE_SSE_VERSION) && OCEAN_HARDWARE_SSE_VERSION >= 41
1719 affine8BitPerChannelSSESubset<tChannels>(source, sourceWidth, sourceHeight, &adjustedAffineTransform, borderColor, target, targetWidth, targetHeight, 0u, targetHeight, sourcePaddingElements, targetPaddingElements);
1720 return;
1721#elif defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
1722 affine8BitPerChannelNEONSubset<tChannels>(source, sourceWidth, sourceHeight, &adjustedAffineTransform, borderColor, target, targetWidth, targetHeight, 0u, targetHeight, sourcePaddingElements, targetPaddingElements);
1723 return;
1724#endif
1725 }
1726
1727 affine8BitPerChannelSubset<tChannels>(source, sourceWidth, sourceHeight, &adjustedAffineTransform, borderColor, target, targetWidth, targetHeight, 0u, targetHeight, sourcePaddingElements, targetPaddingElements);
1728 }
1729}
1730
1731template <unsigned int tChannels>
1732inline void FrameInterpolatorBilinear::homography8BitPerChannel(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3& input_H_output, const uint8_t* borderColor, uint8_t* output, const CV::PixelPositionI& outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker* worker)
1733{
1734 // we adjust the homography to address 'outputOrigin'
1735 const SquareMatrix3 input_H_shiftedOutput = input_H_output * SquareMatrix3(Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(Scalar(outputOrigin.x()), Scalar(outputOrigin.y()), 1));
1736
1737 if (worker)
1738 {
1739 if (outputWidth >= 4u)
1740 {
1741#if defined(OCEAN_HARDWARE_SSE_VERSION) && OCEAN_HARDWARE_SSE_VERSION >= 41
1742 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::homography8BitPerChannelSSESubset<tChannels>, input, inputWidth, inputHeight, &input_H_shiftedOutput, borderColor, output, outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, 0u, 0u), 0, outputHeight, 10u, 11u, 20u);
1743 return;
1744#elif defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
1745 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::homography8BitPerChannelNEONSubset<tChannels>, input, inputWidth, inputHeight, &input_H_shiftedOutput, borderColor, output, outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, 0u, 0u), 0, outputHeight, 10u, 11u, 20u);
1746 return;
1747#endif
1748 }
1749
1750 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::homography8BitPerChannelSubset<tChannels>, input, inputWidth, inputHeight, &input_H_shiftedOutput, borderColor, output, outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, 0u, 0u), 0, outputHeight, 10u, 11u, 20u);
1751 }
1752 else
1753 {
1754 if (outputWidth >= 4u)
1755 {
1756#if defined(OCEAN_HARDWARE_SSE_VERSION) && OCEAN_HARDWARE_SSE_VERSION >= 41
1757 homography8BitPerChannelSSESubset<tChannels>(input, inputWidth, inputHeight, &input_H_shiftedOutput, borderColor, output, outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, 0u, outputHeight);
1758 return;
1759#elif defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
1760 homography8BitPerChannelNEONSubset<tChannels>(input, inputWidth, inputHeight, &input_H_shiftedOutput, borderColor, output, outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, 0u, outputHeight);
1761 return;
1762#endif
1763 }
1764
1765 homography8BitPerChannelSubset<tChannels>(input, inputWidth, inputHeight, &input_H_shiftedOutput, borderColor, output, outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, 0u, outputHeight);
1766 }
1767}
1768
1769template <typename T, unsigned int tChannels>
1770inline void FrameInterpolatorBilinear::homography(const T* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3& input_H_output, const T* borderColor, T* output, const CV::PixelPositionI& outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker* worker)
1771{
1772 if (std::is_same<T, uint8_t>::value)
1773 {
1774 homography8BitPerChannel<tChannels>((const uint8_t*)input, inputWidth, inputHeight, input_H_output, (const uint8_t*)borderColor, (uint8_t*)output, outputOrigin, outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, worker);
1775 return;
1776 }
1777 else
1778 {
1779 // we adjust the homography to address 'outputOrigin'
1780 const SquareMatrix3 input_H_shiftedOutput = input_H_output * SquareMatrix3(Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(Scalar(outputOrigin.x()), Scalar(outputOrigin.y()), 1));
1781
1782 if (worker)
1783 {
1784 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::homographySubset<T, tChannels>, input, inputWidth, inputHeight, &input_H_shiftedOutput, borderColor, output, outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, 0u, 0u), 0, outputHeight, 10u, 11u, 20u);
1785 }
1786 else
1787 {
1788 homographySubset<T, tChannels>(input, inputWidth, inputHeight, &input_H_shiftedOutput, borderColor, output, outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, 0u, outputHeight);
1789 }
1790 }
1791}
1792
1793template <unsigned int tChannels>
1794inline void FrameInterpolatorBilinear::homographies8BitPerChannel(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 homographies[4], const uint8_t* borderColor, uint8_t* output, const Vector2& outputQuadrantCenter, const PixelPositionI& outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker* worker)
1795{
1796 if (worker)
1797 {
1798 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::homographies8BitPerChannelSubset<tChannels>, input, inputWidth, inputHeight, homographies, borderColor, output, outputQuadrantCenter.x(), outputQuadrantCenter.y(), outputOrigin.x(), outputOrigin.y(), outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, 0u, 0u), 0, outputHeight, 14u, 15u, 20u);
1799 }
1800 else
1801 {
1802 homographies8BitPerChannelSubset<tChannels>(input, inputWidth, inputHeight, homographies, borderColor, output, outputQuadrantCenter.x(), outputQuadrantCenter.y(), outputOrigin.x(), outputOrigin.y(), outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, 0u, outputHeight);
1803 }
1804}
1805
1806template <unsigned int tChannels>
1807inline void FrameInterpolatorBilinear::homographyMask8BitPerChannel(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3& input_H_output, uint8_t* output, uint8_t* outputMask, const CV::PixelPositionI& outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const uint8_t maskValue, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker* worker)
1808{
1809 // we adjust the homography to address 'outputOrigin'
1810 const SquareMatrix3 input_H_shiftedOutput = input_H_output * SquareMatrix3(Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(Scalar(outputOrigin.x()), Scalar(outputOrigin.y()), 1));
1811
1812 if (worker)
1813 {
1814 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::homographyMask8BitPerChannelSubset<tChannels>, input, inputWidth, inputHeight, &input_H_shiftedOutput, output, outputMask, maskValue, outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, outputMaskPaddingElements, 0u, 0u), 0, outputHeight, 12u, 13u, 20u);
1815 }
1816 else
1817 {
1818 homographyMask8BitPerChannelSubset<tChannels>(input, inputWidth, inputHeight, &input_H_shiftedOutput, output, outputMask, maskValue, outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, outputMaskPaddingElements, 0u, outputHeight);
1819 }
1820}
1821
1822template <unsigned int tChannels>
1823inline void FrameInterpolatorBilinear::homographiesMask8BitPerChannel(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 homographies[4], uint8_t* output, uint8_t* outputMask, const Vector2& outputQuadrantCenter, const CV::PixelPositionI& outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker* worker, const uint8_t maskValue)
1824{
1825 if (worker)
1826 {
1827 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::homographiesMask8BitPerChannelSubset<tChannels>, input, inputWidth, inputHeight, homographies, output, outputMask, maskValue, outputQuadrantCenter.x(), outputQuadrantCenter.y(), outputOrigin.x(), outputOrigin.y(), outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, outputMaskPaddingElements, 0u, 0u), 0, outputHeight);
1828 }
1829 else
1830 {
1831 homographiesMask8BitPerChannelSubset<tChannels>(input, inputWidth, inputHeight, homographies, output, outputMask, maskValue, outputQuadrantCenter.x(), outputQuadrantCenter.y(), outputOrigin.x(), outputOrigin.y(), outputWidth, outputHeight, inputPaddingElements, outputPaddingElements, outputMaskPaddingElements, 0u, outputHeight);
1832 }
1833}
1834
1835template <unsigned int tChannels>
1836inline void FrameInterpolatorBilinear::homographyWithCamera8BitPerChannel(const PinholeCamera& inputCamera, const PinholeCamera& outputCamera, const uint8_t* input, const SquareMatrix3& homography, const bool useDistortionParameters, const uint8_t* borderColor, uint8_t* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker* worker)
1837{
1838 const SquareMatrix3 normalizedHomography(inputCamera.invertedIntrinsic() * homography * outputCamera.intrinsic());
1839
1840 const PinholeCamera::DistortionLookup outputCameraDistortionLookup(outputCamera, 10u);
1841
1842 if (worker)
1843 {
1844 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::homographyWithCamera8BitPerChannelSubset<tChannels>, &inputCamera, &outputCamera, &outputCameraDistortionLookup, input, &normalizedHomography, useDistortionParameters, borderColor, output, inputPaddingElements, outputPaddingElements, 0u, 0u), 0, outputCamera.height());
1845 }
1846 else
1847 {
1848 homographyWithCamera8BitPerChannelSubset<tChannels>(&inputCamera, &outputCamera, &outputCameraDistortionLookup, input, &normalizedHomography, useDistortionParameters, borderColor, output, inputPaddingElements, outputPaddingElements, 0u, outputCamera.height());
1849 }
1850}
1851
1852template <unsigned int tChannels>
1853inline void FrameInterpolatorBilinear::homographyWithCameraMask8BitPerChannel(const PinholeCamera& inputCamera, const PinholeCamera& outputCamera, const uint8_t* input, const unsigned int inputPaddingElements, const SquareMatrix3& homography, uint8_t* output, uint8_t* outputMask, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker* worker, const uint8_t maskValue)
1854{
1855 const SquareMatrix3 normalizedHomography(inputCamera.invertedIntrinsic() * homography * outputCamera.intrinsic());
1856
1857 const PinholeCamera::DistortionLookup outputCameraDistortionLookup(outputCamera, 10u);
1858
1859 if (worker)
1860 {
1861 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::homographyWithCameraMask8BitPerChannelSubset<tChannels>, &inputCamera, &outputCamera, &outputCameraDistortionLookup, input, inputPaddingElements, &normalizedHomography, output, outputMask, outputPaddingElements, outputMaskPaddingElements, maskValue, 0u, 0u), 0, outputCamera.height(), 11u, 12u, 10u);
1862 }
1863 else
1864 {
1865 homographyWithCameraMask8BitPerChannelSubset<tChannels>(&inputCamera, &outputCamera, &outputCameraDistortionLookup, input, inputPaddingElements, &normalizedHomography, output, outputMask, outputPaddingElements, outputMaskPaddingElements, maskValue, 0u, outputCamera.height());
1866 }
1867}
1868
1869template <typename T, unsigned int tChannels>
1870inline void FrameInterpolatorBilinear::lookup(const T* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable& input_LT_output, const bool offset, const T* borderColor, T* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker* worker, const bool useOptimizedNEON, const bool useOptimizedBilinearValuesAndFactorCalculation, [[maybe_unused]] const bool useOptimizedNEONFactorReplication, [[maybe_unused]] const bool use4ChannelSamplingFastPaths)
1871{
1872 if constexpr (std::is_same<T, uint8_t>::value)
1873 {
1874#if defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
1875 if ((tChannels >= 1u && input_LT_output.sizeX() >= 8) || (tChannels >= 2u && input_LT_output.sizeX() >= 4))
1876 {
1877 // NEON implementation for 1 channel: min width 8; for 2+ channels: min width 4
1878
1879 if (worker)
1880 {
1881 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::lookup8BitPerChannelSubsetNEON<tChannels>, input, inputWidth, inputHeight, &input_LT_output, offset, borderColor, output, inputPaddingElements, outputPaddingElements, 0u, 0u, useOptimizedNEON, useOptimizedBilinearValuesAndFactorCalculation, useOptimizedNEONFactorReplication, use4ChannelSamplingFastPaths), 0u, (unsigned int)(input_LT_output.sizeY()), 9u, 10u, 20u);
1882 }
1883 else
1884 {
1885 lookup8BitPerChannelSubsetNEON<tChannels>(input, inputWidth, inputHeight, &input_LT_output, offset, borderColor, output, inputPaddingElements, outputPaddingElements, 0u, (unsigned int)(input_LT_output.sizeY()), useOptimizedNEON, useOptimizedBilinearValuesAndFactorCalculation, useOptimizedNEONFactorReplication, use4ChannelSamplingFastPaths);
1886 }
1887
1888 return;
1889 }
1890#endif // defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
1891
1892 if (worker)
1893 {
1894 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::lookup8BitPerChannelSubset<tChannels>, input, inputWidth, inputHeight, &input_LT_output, offset, borderColor, output, inputPaddingElements, outputPaddingElements, 0u, 0u), 0u, (unsigned int)input_LT_output.sizeY(), 9u, 10u, 20u);
1895 }
1896 else
1897 {
1898 lookup8BitPerChannelSubset<tChannels>(input, inputWidth, inputHeight, &input_LT_output, offset, borderColor, output, inputPaddingElements, outputPaddingElements, 0u, (unsigned int)(input_LT_output.sizeY()));
1899 }
1900 }
1901 else
1902 {
1903 ocean_assert((!std::is_same<T, uint8_t>::value));
1904
1905 if (worker)
1906 {
1907 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::lookupSubset<T, tChannels>, input, inputWidth, inputHeight, &input_LT_output, offset, borderColor, output, inputPaddingElements, outputPaddingElements, 0u, 0u), 0u, (unsigned int)(input_LT_output.sizeY()), 9u, 10u, 20u);
1908 }
1909 else
1910 {
1911 lookupSubset<T, tChannels>(input, inputWidth, inputHeight, &input_LT_output, offset, borderColor, output, inputPaddingElements, outputPaddingElements, 0u, (unsigned int)(input_LT_output.sizeY()));
1912 }
1913 }
1914}
1915
1916template <unsigned int tChannels>
1917inline void FrameInterpolatorBilinear::lookupMask8BitPerChannel(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable& input_LT_output, const bool offset, uint8_t* output, uint8_t* outputMask, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker* worker, const uint8_t maskValue)
1918{
1919 if (worker)
1920 {
1921 worker->executeFunction(Worker::Function::createStatic(&FrameInterpolatorBilinear::lookupMask8BitPerChannelSubset<tChannels>, input, inputWidth, inputHeight, &input_LT_output, offset, output, outputMask, maskValue, inputPaddingElements, outputPaddingElements, outputMaskPaddingElements, 0u, 0u), 0u, (unsigned int)(input_LT_output.sizeY()), 11u, 12u, 20u);
1922 }
1923 else
1924 {
1925 lookupMask8BitPerChannelSubset<tChannels>(input, inputWidth, inputHeight, &input_LT_output, offset, output, outputMask, maskValue, inputPaddingElements, outputPaddingElements, outputMaskPaddingElements, 0u, (unsigned int)(input_LT_output.sizeY()));
1926 }
1927}
1928
1929template <typename T, unsigned int tChannels>
1930void FrameInterpolatorBilinear::resampleCameraImage(const T* sourceFrame, const AnyCamera& sourceCamera, const SquareMatrix3& source_R_target, const AnyCamera& targetCamera, T* targetFrame, const unsigned int sourceFramePaddingElements, const unsigned int targetFramePaddingElements, LookupCorner2<Vector2>* source_OLT_target, Worker* worker, const unsigned int binSizeInPixel, const T* borderColor)
1931{
1932 static_assert(tChannels >= 1u, "Invalid channel number!");
1933
1934 ocean_assert(sourceFrame != nullptr);
1935 ocean_assert(sourceCamera.isValid());
1936 ocean_assert(source_R_target.isOrthonormal());
1937 ocean_assert(targetCamera.isValid());
1938 ocean_assert(targetFrame != nullptr);
1939 ocean_assert(binSizeInPixel >= 1u);
1940
1941 const size_t binsX = std::max(1u, targetCamera.width() / binSizeInPixel);
1942 const size_t binsY = std::max(1u, targetCamera.height() / binSizeInPixel);
1943 CV::FrameInterpolatorBilinear::LookupTable lookupTable(targetCamera.width(), targetCamera.height(), binsX, binsY);
1944
1945 for (size_t yBin = 0; yBin <= lookupTable.binsY(); ++yBin)
1946 {
1947 for (size_t xBin = 0; xBin <= lookupTable.binsX(); ++xBin)
1948 {
1949 const Vector2 cornerPosition = lookupTable.binTopLeftCornerPosition(xBin, yBin);
1950
1951 constexpr bool makeUnitVector = false; // we don't need a unit/normalized vector as we project the vector into the camera again
1952
1953 const Vector3 rayI = source_R_target * targetCamera.vector(cornerPosition, makeUnitVector);
1954 const Vector3 rayIF = Vector3(rayI.x(), -rayI.y(), -rayI.z());
1955
1956 if (rayIF.z() > Numeric::eps())
1957 {
1958 const Vector2 projectedPoint = sourceCamera.projectToImageIF(rayIF);
1959
1960 lookupTable.setBinTopLeftCornerValue(xBin, yBin, projectedPoint - cornerPosition);
1961 }
1962 else
1963 {
1964 // simply a coordinate far outside the input
1965 lookupTable.setBinTopLeftCornerValue(xBin, yBin, Vector2(Scalar(sourceCamera.width() * 10u), Scalar(sourceCamera.height() * 10u)));
1966 }
1967 }
1968 }
1969
1970 lookup<T, tChannels>(sourceFrame, sourceCamera.width(), sourceCamera.height(), lookupTable, true /*offset*/, borderColor, targetFrame, sourceFramePaddingElements, targetFramePaddingElements, worker);
1971
1972 if (source_OLT_target)
1973 {
1974 *source_OLT_target = std::move(lookupTable);
1975 }
1976}
1977
1978template <unsigned int tChannels>
1979void FrameInterpolatorBilinear::rotate8BitPerChannel(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const Scalar horizontalAnchorPosition, const Scalar verticalAnchorPosition, const Scalar angle, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker, const uint8_t* borderColor)
1980{
1981 static_assert(tChannels != 0u, "Invalid channel number!");
1982
1983 ocean_assert(source != nullptr && target != nullptr);
1984 ocean_assert(width >= 1u && height >= 1u);
1985
1986 if (worker)
1987 {
1988 worker->executeFunction(Worker::Function::createStatic(&rotate8BitPerChannelSubset<tChannels>, source, target, width, height, horizontalAnchorPosition, verticalAnchorPosition, angle, borderColor, sourcePaddingElements, targetPaddingElements, 0u, 0u), 0u, height);
1989 }
1990 else
1991 {
1992 rotate8BitPerChannelSubset<tChannels>(source, target, width, height, horizontalAnchorPosition, verticalAnchorPosition, angle, borderColor, sourcePaddingElements, targetPaddingElements, 0u, height);
1993 }
1994}
1995
1996template <unsigned int tChannels, PixelCenter tPixelCenter, typename TScalar>
1997inline void FrameInterpolatorBilinear::interpolatePixel8BitPerChannel(const uint8_t* frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const VectorT2<TScalar>& position, uint8_t* result)
1998{
1999 static_assert(tChannels != 0u, "Invalid channel number!");
2000 static_assert(tPixelCenter == PC_TOP_LEFT || tPixelCenter == PC_CENTER, "Invalid pixel center!");
2001
2002 ocean_assert(frame != nullptr && result != nullptr);
2003 ocean_assert(width != 0u && height != 0u);
2004
2005 const unsigned int frameStrideElements = width * tChannels + framePaddingElements;
2006
2007 ocean_assert(position.x() >= TScalar(0));
2008 ocean_assert(position.y() >= TScalar(0));
2009
2010 if constexpr (tPixelCenter == PC_TOP_LEFT)
2011 {
2012 ocean_assert(position.x() <= TScalar(width - 1u));
2013 ocean_assert(position.y() <= TScalar(height - 1u));
2014
2015 const unsigned int left = (unsigned int)(position.x());
2016 const unsigned int top = (unsigned int)(position.y());
2017 ocean_assert(left < width && top < height);
2018
2019 const TScalar tx = position.x() - TScalar(left);
2020 ocean_assert(tx >= 0 && tx <= 1);
2021 const unsigned int txi = (unsigned int)(tx * TScalar(128) + TScalar(0.5));
2022 const unsigned int txi_ = 128u - txi;
2023
2024 const TScalar ty = position.y() - TScalar(top);
2025 ocean_assert(ty >= 0 && ty <= 1);
2026 const unsigned int tyi = (unsigned int)(ty * TScalar(128) + TScalar(0.5));
2027 const unsigned int tyi_ = 128u - tyi;
2028
2029 const unsigned int rightOffset = left + 1u < width ? tChannels : 0u;
2030 const unsigned int bottomOffset = top + 1u < height ? frameStrideElements : 0u;
2031
2032 const uint8_t* const topLeft = frame + top * frameStrideElements + tChannels * left;
2033
2034 const unsigned int txty = txi * tyi;
2035 const unsigned int txty_ = txi * tyi_;
2036 const unsigned int tx_ty = txi_ * tyi;
2037 const unsigned int tx_ty_ = txi_ * tyi_;
2038
2039 for (unsigned int n = 0u; n < tChannels; ++n)
2040 {
2041 result[n] = uint8_t((topLeft[n] * tx_ty_ + topLeft[rightOffset + n] * txty_ + topLeft[bottomOffset + n] * tx_ty + topLeft[bottomOffset + rightOffset + n] * txty + 8192u) >> 14u);
2042 }
2043 }
2044 else
2045 {
2046 ocean_assert(tPixelCenter == PC_CENTER);
2047
2048 ocean_assert(position.x() <= TScalar(width));
2049 ocean_assert(position.y() <= TScalar(height));
2050
2051 const TScalar xShifted = std::max(TScalar(0.0), position.x() - TScalar(0.5));
2052 const TScalar yShifted = std::max(TScalar(0.0), position.y() - TScalar(0.5));
2053
2054 const unsigned int left = (unsigned int)(xShifted);
2055 const unsigned int top = (unsigned int)(yShifted);
2056
2057 ocean_assert(left < width);
2058 ocean_assert(top < height);
2059
2060 const TScalar tx = xShifted - TScalar(left);
2061 const TScalar ty = yShifted - TScalar(top);
2062
2063 ocean_assert(tx >= 0 && tx <= 1);
2064 ocean_assert(ty >= 0 && ty <= 1);
2065
2066 const unsigned int txi = (unsigned int)(tx * TScalar(128) + TScalar(0.5));
2067 const unsigned int txi_ = 128u - txi;
2068
2069 const unsigned int tyi = (unsigned int)(ty * TScalar(128) + TScalar(0.5));
2070 const unsigned int tyi_ = 128u - tyi;
2071
2072 const unsigned int rightOffset = left + 1u < width ? tChannels : 0u;
2073 const unsigned int bottomOffset = top + 1u < height ? frameStrideElements : 0u;
2074
2075 const uint8_t* const topLeft = frame + top * frameStrideElements + left * tChannels;
2076
2077 const unsigned int txty = txi * tyi;
2078 const unsigned int txty_ = txi * tyi_;
2079 const unsigned int tx_ty = txi_ * tyi;
2080 const unsigned int tx_ty_ = txi_ * tyi_;
2081
2082 for (unsigned int n = 0u; n < tChannels; ++n)
2083 {
2084 result[n] = uint8_t((topLeft[n] * tx_ty_ + topLeft[rightOffset + n] * txty_ + topLeft[bottomOffset + n] * tx_ty + topLeft[bottomOffset + rightOffset + n] * txty + 8192u) >> 14u);
2085 }
2086 }
2087}
2088
2089template <typename TSource, typename TTarget, unsigned int tChannels, PixelCenter tPixelCenter, typename TScalar, typename TIntermediate>
2090inline void FrameInterpolatorBilinear::interpolatePixel(const TSource* frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const VectorT2<TScalar>& position, TTarget* result, const TIntermediate& resultBias)
2091{
2092 static_assert(tChannels != 0u, "Invalid channel number!");
2093 static_assert(tPixelCenter == PC_TOP_LEFT || tPixelCenter == PC_CENTER, "Invalid pixel center!");
2094
2095 ocean_assert(frame != nullptr && result != nullptr);
2096 ocean_assert(width != 0u && height != 0u);
2097
2098 const unsigned int frameStrideElements = width * tChannels + framePaddingElements;
2099
2100 ocean_assert(position.x() >= TScalar(0));
2101 ocean_assert(position.y() >= TScalar(0));
2102
2103 if constexpr (tPixelCenter == PC_TOP_LEFT)
2104 {
2105 ocean_assert(position.x() <= TScalar(width - 1u));
2106 ocean_assert(position.y() <= TScalar(height - 1u));
2107
2108 const unsigned int left = (unsigned int)(position.x());
2109 const unsigned int top = (unsigned int)(position.y());
2110
2111 const TScalar tx = position.x() - TScalar(left);
2112 ocean_assert(tx >= 0 && tx <= 1);
2113
2114 const TScalar ty = position.y() - TScalar(top);
2115 ocean_assert(ty >= 0 && ty <= 1);
2116
2117 const unsigned int rightOffset = left + 1u < width ? tChannels : 0u;
2118 const unsigned int bottomOffset = top + 1u < height ? frameStrideElements : 0u;
2119
2120 const TSource* const topLeft = frame + top * frameStrideElements + tChannels * left;
2121
2122 const TIntermediate txty = TIntermediate(tx) * TIntermediate(ty);
2123 const TIntermediate txty_ = TIntermediate(tx) * (TIntermediate(1) - TIntermediate(ty));
2124 const TIntermediate tx_ty = (TIntermediate(1) - TIntermediate(tx)) * TIntermediate(ty);
2125 const TIntermediate tx_ty_ = (TIntermediate(1) - TIntermediate(tx)) * (TIntermediate(1) - TIntermediate(ty));
2126
2127 ocean_assert_accuracy(NumericT<TIntermediate>::isEqual(txty + txty_ + tx_ty + tx_ty_, TIntermediate(1)));
2128
2129 for (unsigned int n = 0u; n < tChannels; ++n)
2130 {
2131 result[n] = TTarget(TIntermediate(topLeft[n]) * tx_ty_ + TIntermediate(topLeft[rightOffset + n]) * txty_ + TIntermediate(topLeft[bottomOffset + n]) * tx_ty + TIntermediate(topLeft[bottomOffset + rightOffset + n]) * txty + resultBias);
2132 }
2133 }
2134 else
2135 {
2136 ocean_assert(tPixelCenter == PC_CENTER);
2137
2138 ocean_assert(position.x() <= TScalar(width));
2139 ocean_assert(position.y() <= TScalar(height));
2140
2141 const TScalar xShifted = std::max(TScalar(0.0), position.x() - TScalar(0.5));
2142 const TScalar yShifted = std::max(TScalar(0.0), position.y() - TScalar(0.5));
2143
2144 const unsigned int left = (unsigned int)(xShifted);
2145 const unsigned int top = (unsigned int)(yShifted);
2146
2147 ocean_assert(left < width);
2148 ocean_assert(top < height);
2149
2150 const TScalar tx = xShifted - TScalar(left);
2151 const TScalar ty = yShifted - TScalar(top);
2152
2153 ocean_assert(tx >= 0 && tx <= 1);
2154 ocean_assert(ty >= 0 && ty <= 1);
2155
2156 const unsigned int rightOffset = left + 1u < width ? tChannels : 0u;
2157 const unsigned int bottomOffset = top + 1u < height ? frameStrideElements : 0u;
2158
2159 const TSource* const topLeft = frame + top * frameStrideElements + tChannels * left;
2160
2161 const TIntermediate txty = TIntermediate(tx) * TIntermediate(ty);
2162 const TIntermediate txty_ = TIntermediate(tx) * (TIntermediate(1) - TIntermediate(ty));
2163 const TIntermediate tx_ty = (TIntermediate(1) - TIntermediate(tx)) * TIntermediate(ty);
2164 const TIntermediate tx_ty_ = (TIntermediate(1) - TIntermediate(tx)) * (TIntermediate(1) - TIntermediate(ty));
2165
2166 ocean_assert_accuracy(NumericT<TIntermediate>::isEqual(txty + txty_ + tx_ty + tx_ty_, TIntermediate(1)));
2167
2168 for (unsigned int n = 0u; n < tChannels; ++n)
2169 {
2170 result[n] = TTarget(TIntermediate(topLeft[n]) * tx_ty_ + TIntermediate(topLeft[rightOffset + n]) * txty_ + TIntermediate(topLeft[bottomOffset + n]) * tx_ty + TIntermediate(topLeft[bottomOffset + rightOffset + n]) * txty + resultBias);
2171 }
2172 }
2173}
2174
2175template <unsigned int tChannels, bool tAlphaAtFront, bool tTransparentIs0xFF>
2176inline void FrameInterpolatorBilinear::interpolate1PixelFullAlphaBorder8BitPerChannel(const uint8_t* frame, const unsigned int width, const unsigned int height, const Vector2& position, uint8_t* result, const unsigned int framePaddingElements)
2177{
2178 static_assert(tChannels != 0u, "Invalid channel number!");
2179
2180 ocean_assert(frame && result);
2181
2182 const Vector2 pos(position.x() - Scalar(0.5), position.y() - Scalar(0.5));
2183
2184 // check whether the position is outside the frame and will therefore be 100% transparent
2185 if (pos.x() <= Scalar(-1) || pos.y() <= Scalar(-1) || pos.x() >= Scalar(width) || pos.y() >= Scalar(height))
2186 {
2187 for (unsigned int n = 0u; n < tChannels - 1u; ++n)
2188 {
2190 }
2191
2193
2194 return;
2195 }
2196
2197 const unsigned int frameStrideElements = width * tChannels + framePaddingElements;
2198
2199 const int left = int(Numeric::floor(pos.x()));
2200 const int top = int(Numeric::floor(pos.y()));
2201
2202 ocean_assert(left >= -1 && left < int(width));
2203 ocean_assert(top >= -1 && top < int(height));
2204
2205 if ((unsigned int)left < width - 1u && (unsigned int)top < height - 1u)
2206 {
2207 // we have a valid pixel position for the left, top, right and bottom pixel
2208
2209 const unsigned int txi = (unsigned int)((pos.x() - Scalar(left)) * Scalar(128) + Scalar(0.5));
2210 const unsigned int txi_ = 128u - txi;
2211
2212 const unsigned int tyi = (unsigned int)((pos.y() - Scalar(top)) * Scalar(128) + Scalar(0.5));
2213 const unsigned int tyi_ = 128u - tyi;
2214
2215 const uint8_t* const topLeft = frame + top * frameStrideElements + left * tChannels;
2216
2217 const unsigned int txty = txi * tyi;
2218 const unsigned int txty_ = txi * tyi_;
2219 const unsigned int tx_ty = txi_ * tyi;
2220 const unsigned int tx_ty_ = txi_ * tyi_;
2221
2222 for (unsigned int n = 0u; n < tChannels; ++n)
2223 {
2224 result[n] = (topLeft[n] * tx_ty_ + topLeft[tChannels + n] * txty_
2225 + topLeft[frameStrideElements + n] * tx_ty + topLeft[frameStrideElements + tChannels + n] * txty + 8192u) >> 14u;
2226 }
2227 }
2228 else
2229 {
2230 // we do not have a valid pixel for all 4-neighborhood pixels
2231
2232 const unsigned int txi = (unsigned int)((pos.x() - Scalar(left)) * Scalar(128) + Scalar(0.5));
2233 const unsigned int txi_ = 128u - txi;
2234
2235 const unsigned int tyi = (unsigned int)((pos.y() - Scalar(top)) * Scalar(128) + Scalar(0.5));
2236 const unsigned int tyi_ = 128u - tyi;
2237
2238 const unsigned int rightOffset = (left >= 0 && left + 1u < width) ? tChannels : 0u;
2239 const unsigned int bottomOffset = (top >= 0 && top + 1u < height) ? frameStrideElements : 0u;
2240
2241 ocean_assert(left < int(width) && top < int(height));
2242 const uint8_t* const topLeft = frame + max(0, top) * frameStrideElements + max(0, left) * tChannels;
2243
2244 const unsigned int txty = txi * tyi;
2245 const unsigned int txty_ = txi * tyi_;
2246 const unsigned int tx_ty = txi_ * tyi;
2247 const unsigned int tx_ty_ = txi_ * tyi_;
2248
2249 for (unsigned int n = FrameBlender::SourceOffset<tAlphaAtFront>::data(); n < tChannels + FrameBlender::SourceOffset<tAlphaAtFront>::data() - 1u; ++n)
2250 {
2251 result[n] = (topLeft[n] * tx_ty_ + topLeft[rightOffset + n] * txty_
2252 + topLeft[bottomOffset + n] * tx_ty + topLeft[bottomOffset + rightOffset + n] * txty + 8192u) >> 14u;
2253 }
2254
2255 const uint8_t alphaTopLeft = (left >= 0 && top >= 0) ? topLeft[FrameBlender::SourceOffset<tAlphaAtFront>::template alpha<tChannels>()] : FrameBlender::fullTransparent8Bit<tTransparentIs0xFF>();
2256 const uint8_t alphaTopRight = (left + 1u < width && top >= 0) ? topLeft[rightOffset + FrameBlender::SourceOffset<tAlphaAtFront>::template alpha<tChannels>()] : FrameBlender::fullTransparent8Bit<tTransparentIs0xFF>();
2257 const uint8_t alphaBottomLeft = (left >= 0 && top + 1u < height) ? topLeft[bottomOffset + FrameBlender::SourceOffset<tAlphaAtFront>::template alpha<tChannels>()] : FrameBlender::fullTransparent8Bit<tTransparentIs0xFF>();
2258 const uint8_t alphaBottomRight = (left + 1u < width && top + 1u < height) ? topLeft[bottomOffset + rightOffset + FrameBlender::SourceOffset<tAlphaAtFront>::template alpha<tChannels>()] : FrameBlender::fullTransparent8Bit<tTransparentIs0xFF>();
2259
2260 result[FrameBlender::SourceOffset<tAlphaAtFront>::template alpha<tChannels>()] = (alphaTopLeft * tx_ty_ + alphaTopRight * txty_ + alphaBottomLeft * tx_ty + alphaBottomRight * txty + 8192u) >> 14u;
2261 }
2262}
2263
2264template <unsigned int tChannels>
2265void FrameInterpolatorBilinear::affine8BitPerChannelSubset(const uint8_t* source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3* source_A_target, const uint8_t* borderColor, uint8_t* target, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int firstTargetRow, const unsigned int numberOutputRows, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements)
2266{
2267 static_assert(tChannels >= 1u, "Invalid channel number!");
2268
2269 ocean_assert(source != nullptr && target != nullptr);
2270 ocean_assert(sourceWidth > 0u && sourceHeight > 0u);
2271 ocean_assert_and_suppress_unused(targetWidth > 0u && targetHeight > 0u, targetHeight);
2272 ocean_assert(source_A_target);
2273 ocean_assert(!source_A_target->isNull() && Numeric::isEqualEps((*source_A_target)[2]) && Numeric::isEqualEps((*source_A_target)[5]));
2274
2275 ocean_assert(firstTargetRow + numberOutputRows <= targetHeight);
2276
2277 const unsigned int targetStrideElements = tChannels * targetWidth + targetPaddingElements;
2278
2279 const Scalar scalarSourceWidth_1 = Scalar(sourceWidth - 1u);
2280 const Scalar scalarSourceHeight_1 = Scalar(sourceHeight - 1u);
2281
2282 using PixelType = typename DataType<uint8_t, tChannels>::Type;
2283
2284 uint8_t zeroColor[tChannels] = {uint8_t(0)};
2285 const PixelType* const bColor = borderColor ? (PixelType*)borderColor : (PixelType*)zeroColor;
2286
2287 for (unsigned int y = firstTargetRow; y < firstTargetRow + numberOutputRows; ++y)
2288 {
2289 PixelType* targetRow = (PixelType*)(target + y * targetStrideElements);
2290
2291 /*
2292 * We can slightly optimize the 3x3 matrix multiplication:
2293 *
2294 * | X0 Y0 Z0 | | x |
2295 * | X1 Y1 Z1 | * | y |
2296 * | 0 0 1 | | 1 |
2297 *
2298 * | xx | | X0 * x | | Y0 * y + Z0 |
2299 * | yy | = | X1 * x | + | Y1 * y + Z1 |
2300 *
2301 * As y is constant within the inner loop, the two terms on the right side in the above equations can be pre-calculated:
2302 *
2303 * C0 = Y0 * y + Z0
2304 * C1 = Y1 * y + Z1
2305 *
2306 * So the computation becomes:
2307 *
2308 * | x' | | X0 * x | | C0 |
2309 * | y' | = | X1 * x | + | C1 |
2310 */
2311
2312 const Vector2 X(source_A_target->data() + 0);
2313 const Vector2 c(Vector2(source_A_target->data() + 3) * Scalar(y) + Vector2(source_A_target->data() + 6));
2314
2315 for (unsigned int x = 0u; x < targetWidth; ++x)
2316 {
2317 const Vector2 sourcePosition = X * Scalar(x) + c;
2318
2319#ifdef OCEAN_DEBUG
2320 const Scalar debugSourceX = (*source_A_target)[0] * Scalar(x) + (*source_A_target)[3] * Scalar(y) + (*source_A_target)[6];
2321 const Scalar debugSourceY = (*source_A_target)[1] * Scalar(x) + (*source_A_target)[4] * Scalar(y) + (*source_A_target)[7];
2322 ocean_assert(sourcePosition.isEqual(Vector2(debugSourceX, debugSourceY), Scalar(0.01)));
2323#endif
2324
2325 if (sourcePosition.x() < Scalar(0) || sourcePosition.x() > scalarSourceWidth_1 || sourcePosition.y() < Scalar(0) || sourcePosition.y() > scalarSourceHeight_1)
2326 {
2327 *targetRow = *bColor;
2328 }
2329 else
2330 {
2331 interpolatePixel8BitPerChannel<tChannels, PC_TOP_LEFT>(source, sourceWidth, sourceHeight, sourcePaddingElements, sourcePosition, (uint8_t*)(targetRow));
2332 }
2333
2334 targetRow++;
2335 }
2336 }
2337}
2338
2339template <unsigned int tChannels>
2340void FrameInterpolatorBilinear::homography8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* input_H_output, const uint8_t* borderColor, uint8_t* output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
2341{
2342 static_assert(tChannels >= 1u, "Invalid channel number!");
2343
2344 ocean_assert(input != nullptr && output != nullptr);
2345 ocean_assert(inputWidth > 0u && inputHeight > 0u);
2346 ocean_assert(outputWidth > 0u && outputHeight > 0u);
2347 ocean_assert(input_H_output != nullptr);
2348
2349 ocean_assert_and_suppress_unused(firstOutputRow + numberOutputRows <= outputHeight, outputHeight);
2350
2351 const unsigned int outputStrideElements = outputWidth * tChannels + outputPaddingElements;
2352
2353 const Scalar scalarInputWidth_1 = Scalar(inputWidth - 1u);
2354 const Scalar scalarInputHeight_1 = Scalar(inputHeight - 1u);
2355
2356 using PixelType = typename DataType<uint8_t, tChannels>::Type;
2357
2358 uint8_t zeroColor[tChannels] = {uint8_t(0)};
2359 const PixelType bColor = borderColor ? *(PixelType*)borderColor : *(PixelType*)zeroColor;
2360
2361 for (unsigned int y = firstOutputRow; y < firstOutputRow + numberOutputRows; ++y)
2362 {
2363 /*
2364 * We can slightly optimize the 3x3 matrix multiplication:
2365 *
2366 * | X0 Y0 Z0 | | x |
2367 * | X1 Y1 Z1 | * | y |
2368 * | X2 Y2 Z2 | | 1 |
2369 *
2370 * | xx | | X0 * x | | Y0 * y + Z0 |
2371 * | yy | = | X1 * x | + | Y1 * y + Z1 |
2372 * | zz | | X2 * x | | Y2 * y + Z2 |
2373 *
2374 * | xx | | X0 * x | | C0 |
2375 * | yy | = | X1 * x | + | C1 |
2376 * | zz | | X2 * x | | C2 |
2377 *
2378 * As y is constant within the inner loop, we can pre-calculate the following terms:
2379 *
2380 * | x' | | (X0 * x + C0) / (X2 * x + C2) |
2381 * | y' | = | (X1 * x + C1) / (X2 * x + C2) |
2382 */
2383
2384 const Vector2 X(input_H_output->data() + 0);
2385 const Vector2 c(Vector2(input_H_output->data() + 3) * Scalar(y) + Vector2(input_H_output->data() + 6));
2386
2387 const Scalar X2 = (*input_H_output)(2, 0);
2388 const Scalar constValue2 = (*input_H_output)(2, 1) * Scalar(y) + (*input_H_output)(2, 2);
2389
2390 PixelType* outputRowPixel = (PixelType*)(output + y * outputStrideElements);
2391
2392 for (unsigned int x = 0u; x < outputWidth; ++x)
2393 {
2394 ocean_assert_accuracy(Numeric::isNotEqualEps((X2 * Scalar(x) + constValue2)));
2395 const Vector2 inputPosition((X * Scalar(x) + c) / (X2 * Scalar(x) + constValue2));
2396
2397#ifdef OCEAN_DEBUG
2398 const Vector2 debugInputPosition(*input_H_output * Vector2(Scalar(x), Scalar(y)));
2399 ocean_assert(inputPosition.isEqual(debugInputPosition, Scalar(0.01)));
2400#endif
2401
2402 if (inputPosition.x() < Scalar(0) || inputPosition.x() > scalarInputWidth_1 || inputPosition.y() < Scalar(0) || inputPosition.y() > scalarInputHeight_1)
2403 {
2404 *outputRowPixel = bColor;
2405 }
2406 else
2407 {
2408 interpolatePixel8BitPerChannel<tChannels, PC_TOP_LEFT>(input, inputWidth, inputHeight, inputPaddingElements, inputPosition, (uint8_t*)(outputRowPixel));
2409 }
2410
2411 ++outputRowPixel;
2412 }
2413 }
2414}
2415
2416template <typename T, unsigned int tChannels>
2417void FrameInterpolatorBilinear::homographySubset(const T* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* input_H_output, const T* borderColor, T* output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
2418{
2419 static_assert(tChannels >= 1u, "Invalid channel number!");
2420
2421 ocean_assert(input != nullptr && output != nullptr);
2422 ocean_assert(inputWidth > 0u && inputHeight > 0u);
2423 ocean_assert_and_suppress_unused(outputWidth > 0u && outputHeight > 0u, outputHeight);
2424 ocean_assert(input_H_output != nullptr);
2425
2426 ocean_assert(firstOutputRow + numberOutputRows <= outputHeight);
2427
2428 const unsigned int outputStrideElements = outputWidth * tChannels + outputPaddingElements;
2429
2430 const Scalar scalarInputWidth1 = Scalar(inputWidth - 1u);
2431 const Scalar scalarInputHeight1 = Scalar(inputHeight - 1u);
2432
2433 // we need to find a best matching floating point data type for the intermediate interpolation results
2434 using TIntermediate = typename FloatTyper<T>::Type;
2435
2436 using PixelType = typename DataType<T, tChannels>::Type;
2437
2438 constexpr T zeroColor[tChannels] = {T(0)};
2439 const PixelType* const bColor = borderColor ? (PixelType*)(borderColor) : (PixelType*)(zeroColor);
2440
2441 constexpr TIntermediate bias = TIntermediate(0);
2442
2443 for (unsigned int y = firstOutputRow; y < firstOutputRow + numberOutputRows; ++y)
2444 {
2445 /*
2446 * We can slightly optimize the 3x3 matrix multiplication:
2447 *
2448 * | X0 Y0 Z0 | | x |
2449 * | X1 Y1 Z1 | * | y |
2450 * | X2 Y2 Z2 | | 1 |
2451 *
2452 * | xx | | X0 * x | | Y0 * y + Z0 |
2453 * | yy | = | X1 * x | + | Y1 * y + Z1 |
2454 * | zz | | X2 * x | | Y2 * y + Z2 |
2455 *
2456 * | xx | | X0 * x | | C0 |
2457 * | yy | = | X1 * x | + | C1 |
2458 * | zz | | X2 * x | | C3 |
2459 *
2460 * As y is constant within the inner loop, we can pre-calculate the following terms:
2461 *
2462 * | x' | | (X0 * x + C0) / (X2 * x + C2) |
2463 * | y' | = | (X1 * x + C1) / (X2 * x + C2) |
2464 */
2465
2466 const Vector2 X(input_H_output->data() + 0);
2467 const Vector2 c(Vector2(input_H_output->data() + 3) * Scalar(y) + Vector2(input_H_output->data() + 6));
2468
2469 const Scalar X2 = (*input_H_output)(2, 0);
2470 const Scalar constValue2 = (*input_H_output)(2, 1) * Scalar(y) + (*input_H_output)(2, 2);
2471
2472 PixelType* outputRowPixel = (PixelType*)(output + y * outputStrideElements);
2473
2474 for (unsigned int x = 0u; x < outputWidth; ++x)
2475 {
2476 ocean_assert_accuracy(Numeric::isNotEqualEps((X2 * Scalar(x) + constValue2)));
2477 const Vector2 inputPosition((X * Scalar(x) + c) / (X2 * Scalar(x) + constValue2));
2478
2479#ifdef OCEAN_DEBUG
2480 const Vector2 debugInputPosition(*input_H_output * Vector2(Scalar(x), Scalar(y)));
2481 ocean_assert((std::is_same<float, Scalar>::value) || inputPosition.isEqual(debugInputPosition, Scalar(0.01)));
2482#endif
2483
2484 if (inputPosition.x() >= Scalar(0) && inputPosition.x() <= scalarInputWidth1 && inputPosition.y() >= Scalar(0) && inputPosition.y() <= scalarInputHeight1)
2485 {
2486 interpolatePixel<T, T, tChannels, CV::PC_TOP_LEFT, Scalar, TIntermediate>(input, inputWidth, inputHeight, inputPaddingElements, inputPosition, (T*)(outputRowPixel), bias);
2487 }
2488 else
2489 {
2490 *outputRowPixel = *bColor;
2491 }
2492
2493 ++outputRowPixel;
2494 }
2495 }
2496}
2497
2498#if defined(OCEAN_HARDWARE_SSE_VERSION) && OCEAN_HARDWARE_SSE_VERSION >= 41
2499
2500template <unsigned int tChannels>
2501inline void FrameInterpolatorBilinear::affine8BitPerChannelSSESubset(const uint8_t* source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3* source_A_target, const uint8_t* borderColor, uint8_t* target, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int firstTargetRow, const unsigned int numberTargetRows, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements)
2502{
2503 static_assert(tChannels >= 1u, "Invalid channel number!");
2504
2505 ocean_assert(source && target);
2506 ocean_assert(sourceWidth > 0u && sourceHeight > 0u);
2507 ocean_assert(targetWidth >= 4u && targetHeight > 0u);
2508 ocean_assert(source_A_target);
2509 ocean_assert(!source_A_target->isNull() && Numeric::isEqualEps((*source_A_target)[2]) && Numeric::isEqualEps((*source_A_target)[5]));
2510
2511 ocean_assert_and_suppress_unused(firstTargetRow + numberTargetRows <= targetHeight, targetHeight);
2512
2513 const unsigned int sourceStrideElements = tChannels * sourceWidth + sourcePaddingElements;
2514 const unsigned int targetStrideElements = tChannels * targetWidth + targetPaddingElements;
2515
2516 using PixelType = typename DataType<uint8_t, tChannels>::Type;
2517
2518 uint8_t zeroColor[tChannels] = {uint8_t(0)};
2519 const PixelType* const bColor = borderColor ? (PixelType*)borderColor : (PixelType*)zeroColor;
2520
2521 OCEAN_ALIGN_DATA(16) unsigned int validPixels[4];
2522
2523 OCEAN_ALIGN_DATA(16) unsigned int topLeftOffsets[4];
2524 OCEAN_ALIGN_DATA(16) unsigned int topRightOffsets[4];
2525 OCEAN_ALIGN_DATA(16) unsigned int bottomLeftOffsets[4];
2526 OCEAN_ALIGN_DATA(16) unsigned int bottomRightOffsets[4];
2527
2528 // we store 4 floats: [X0, X0, X0, X0], and same with X1 and X2
2529 const __m128 m128_f_X0 = _mm_set_ps1(float((*source_A_target)(0, 0)));
2530 const __m128 m128_f_X1 = _mm_set_ps1(float((*source_A_target)(1, 0)));
2531
2532 for (unsigned int y = firstTargetRow; y < firstTargetRow + numberTargetRows; ++y)
2533 {
2534 PixelType* targetRow = (PixelType*)(target + y * targetStrideElements);
2535
2536 /*
2537 * We can slightly optimize the 3x3 matrix multiplication:
2538 *
2539 * | X0 Y0 Z0 | | x |
2540 * | X1 Y1 Z1 | * | y |
2541 * | 0 0 1 | | 1 |
2542 *
2543 * | xx | | X0 * x | | Y0 * y + Z0 |
2544 * | yy | = | X1 * x | + | Y1 * y + Z1 |
2545 *
2546 * As y is constant within the inner loop, the two terms on the right side in the above equations can be pre-calculated:
2547 *
2548 * C0 = Y0 * y + Z0
2549 * C1 = Y1 * y + Z1
2550 *
2551 * So the computation becomes:
2552 *
2553 * | x' | | X0 * x | | C0 |
2554 * | y' | = | X1 * x | + | C1 |
2555 */
2556
2557 // we store 4 floats: [C0, C0, C0, C0], and same with C1 and C2
2558 const __m128 m128_f_C0 = _mm_set_ps1(float((*source_A_target)(0, 1) * Scalar(y) + (*source_A_target)(0, 2)));
2559 const __m128 m128_f_C1 = _mm_set_ps1(float((*source_A_target)(1, 1) * Scalar(y) + (*source_A_target)(1, 2)));
2560
2561 // we store 4 floats: [0.0f, 0.0f, 0.0f, 0.0f]
2562 const __m128 m128_f_zero = _mm_setzero_ps();
2563
2564 // we store 4 integers: [tChannels, tChannels, tChannels, tChannels]
2565 const __m128i m128_i_channels = _mm_set1_epi32(tChannels);
2566
2567 // we store 4 integers: [sourceStrideElements, sourceStrideElements, sourceStrideElements, sourceStrideElements]
2568 const __m128i m128_i_sourceStrideElements = _mm_set1_epi32(sourceStrideElements);
2569
2570 // we store 4 integers: [inputWidth - 1, inputWidth - 1, inputWidth - 1, inputWidth - 1], and same with inputHeight
2571 const __m128i m128_i_sourceWidth_1 = _mm_set1_epi32(int(sourceWidth) - 1);
2572 const __m128i m128_i_sourceHeight_1 = _mm_set1_epi32(int(sourceHeight) - 1);
2573
2574 // we store 4 floats: [inputWidth - 1, inputWidth - 1, inputWidth - 1, inputWidth - 1], and same with inputHeight
2575 const __m128 m128_f_sourceWidth_1 = _mm_set_ps1(float(sourceWidth - 1u));
2576 const __m128 m128_f_sourceHeight_1 = _mm_set_ps1(float(sourceHeight - 1u));
2577
2578 for (unsigned int x = 0u; x < targetWidth; x += 4u)
2579 {
2580 if (x + 4u > targetWidth)
2581 {
2582 // the last iteration will not fit into the output frame,
2583 // so we simply shift x left by some pixels (at most 3) and we will calculate some pixels again
2584
2585 ocean_assert(x >= 4u && targetWidth > 4u);
2586 const unsigned int newX = targetWidth - 4u;
2587
2588 ocean_assert(x > newX);
2589 targetRow -= x - newX;
2590
2591 x = newX;
2592
2593 // the for loop will stop after this iteration
2594 ocean_assert(!(x + 4u < targetWidth));
2595 }
2596
2597
2598 // we need four successive x coordinate floats:
2599 // [x + 3.0f, x + 2.0f, x + 1.0f; x + 0.0f]
2600 const __m128 m128_f_x_0123 = _mm_set_ps(float(x + 3u), float(x + 2u), float(x + 1u), float(x + 0u));
2601
2602 // we calculate xx and yy for [x + 3.0f, x + 2.0f, x + 1.0f, x + 0.0f]
2603 const __m128 m128_f_sourceX = _mm_add_ps(_mm_mul_ps(m128_f_X0, m128_f_x_0123), m128_f_C0);
2604 const __m128 m128_f_sourceY = _mm_add_ps(_mm_mul_ps(m128_f_X1, m128_f_x_0123), m128_f_C1);
2605
2606 // now we check whether we are inside the input frame
2607 const __m128 m128_f_validPixelX = _mm_and_ps(_mm_cmple_ps(m128_f_sourceX, m128_f_sourceWidth_1), _mm_cmpge_ps(m128_f_sourceX, m128_f_zero)); // inputPosition.x() <= (inputWidth - 1) && inputPosition.x() >= 0 ? 0xFFFFFFFF : 0x00000000
2608 const __m128 m128_f_validPixelY = _mm_and_ps(_mm_cmple_ps(m128_f_sourceY, m128_f_sourceHeight_1), _mm_cmpge_ps(m128_f_sourceY, m128_f_zero)); // inputPosition.y() <= (inputHeight - 1) && inputPosition.y() >= 0 ? 0xFFFFFFFF : 0x00000000
2609
2610 const __m128i m128_i_validPixel = _mm_castps_si128(_mm_and_ps(m128_f_validPixelX, m128_f_validPixelY)); // is_inside_input_frame(inputPosition) ? 0xFFFFFFFF : 0x00000000
2611
2612 // we can stop here if all pixels are invalid
2613 if (_mm_test_all_zeros(m128_i_validPixel, _mm_set1_epi32(0xFFFFFFFF)))
2614 {
2615#ifdef OCEAN_DEBUG
2616 OCEAN_ALIGN_DATA(16) unsigned int debugValidPixels[4];
2617 _mm_store_si128((__m128i*)debugValidPixels, m128_i_validPixel);
2618 ocean_assert(!(debugValidPixels[0] || debugValidPixels[1] || debugValidPixels[2] || debugValidPixels[3]));
2619#endif
2620
2621 targetRow[0] = *bColor;
2622 targetRow[1] = *bColor;
2623 targetRow[2] = *bColor;
2624 targetRow[3] = *bColor;
2625
2626 targetRow += 4;
2627
2628 continue;
2629 }
2630
2631 // we store the result
2632 _mm_store_si128((__m128i*)validPixels, m128_i_validPixel);
2633 ocean_assert(validPixels[0] || validPixels[1] || validPixels[2] || validPixels[3]);
2634
2635
2636 // now we determine the left, top, right and bottom pixel used for the interpolation
2637 const __m128 m128_f_tx_floor = _mm_floor_ps(m128_f_sourceX);
2638 const __m128 m128_f_ty_floor = _mm_floor_ps(m128_f_sourceY);
2639
2640 // left = floor(x); top = floor(y)
2641 const __m128i m128_i_left = _mm_cvtps_epi32(m128_f_tx_floor);
2642 const __m128i m128_i_top = _mm_cvtps_epi32(m128_f_ty_floor);
2643
2644 // right = min(left + 1, width - 1); bottom = min(top + 1; height - 1)
2645 const __m128i m128_i_right = _mm_min_epu32(_mm_add_epi32(m128_i_left, _mm_set1_epi32(1)), m128_i_sourceWidth_1);
2646 const __m128i m128_i_bottom = _mm_min_epu32(_mm_add_epi32(m128_i_top, _mm_set1_epi32(1)), m128_i_sourceHeight_1);
2647
2648 // offset = (y * sourceStrideElements + tChannels * x)
2649 const __m128i m128_i_topLeftOffset = _mm_add_epi32(_mm_mullo_epi32(m128_i_top, m128_i_sourceStrideElements), _mm_mullo_epi32(m128_i_channels, m128_i_left)); // topleftOffset = (top * sourceStrideElements + tChannels * left)
2650 const __m128i m128_i_topRightOffset = _mm_add_epi32(_mm_mullo_epi32(m128_i_top, m128_i_sourceStrideElements), _mm_mullo_epi32(m128_i_channels, m128_i_right)); // toprightOffset = (top * sourceStrideElements + tChannels * right)
2651 const __m128i m128_i_bottomLeftOffset = _mm_add_epi32(_mm_mullo_epi32(m128_i_bottom, m128_i_sourceStrideElements), _mm_mullo_epi32(m128_i_channels, m128_i_left)); // ...
2652 const __m128i m128_i_bottomRightOffset = _mm_add_epi32(_mm_mullo_epi32(m128_i_bottom, m128_i_sourceStrideElements), _mm_mullo_epi32(m128_i_channels, m128_i_right));
2653
2654 // we store the offsets
2655 _mm_store_si128((__m128i*)topLeftOffsets, m128_i_topLeftOffset);
2656 _mm_store_si128((__m128i*)topRightOffsets, m128_i_topRightOffset);
2657 _mm_store_si128((__m128i*)bottomLeftOffsets, m128_i_bottomLeftOffset);
2658 _mm_store_si128((__m128i*)bottomRightOffsets, m128_i_bottomRightOffset);
2659
2660
2661 // now we need to determine the interpolation factors tx, tx_ and ty, ty_: (top_left * tx_ + top_right * tx) * ty_ + (bottom_left * tx_ + bottom_right * tx) * ty
2662
2663 // we determine the fractional portions of the x' and y':
2664 // e.g., [43.1231, -12.5543, -34.123, 99.2]
2665 // [ 0.1231, 0.4457, 0.877, 0.2] // note the result for negative value - but we will not process negative values anyway due to 'validPixel'
2666 __m128 m128_f_tx = _mm_sub_ps(m128_f_sourceX, m128_f_tx_floor);
2667 __m128 m128_f_ty = _mm_sub_ps(m128_f_sourceY, m128_f_ty_floor);
2668
2669 // we use integer interpolation [0.0, 1.0] -> [0, 128]
2670 m128_f_tx = _mm_mul_ps(m128_f_tx, _mm_set_ps1(128.0f));
2671 m128_f_ty = _mm_mul_ps(m128_f_ty, _mm_set_ps1(128.0f));
2672
2673 m128_f_tx = _mm_round_ps(m128_f_tx, _MM_FROUND_TO_NEAREST_INT);
2674 m128_f_ty = _mm_round_ps(m128_f_ty, _MM_FROUND_TO_NEAREST_INT);
2675
2676 const __m128i m128_i_tx = _mm_cvtps_epi32(m128_f_tx);
2677 const __m128i m128_i_ty = _mm_cvtps_epi32(m128_f_ty);
2678
2679 interpolate4Pixels8BitPerChannelSSE<tChannels>(source, topLeftOffsets, topRightOffsets, bottomLeftOffsets, bottomRightOffsets, validPixels, *bColor, m128_i_tx, m128_i_ty, targetRow);
2680 targetRow += 4;
2681 }
2682 }
2683}
2684
2685template <unsigned int tChannels>
2686inline void FrameInterpolatorBilinear::homography8BitPerChannelSSESubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* input_H_output, const uint8_t* borderColor, uint8_t* output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
2687{
2688 static_assert(tChannels >= 1u, "Invalid channel number!");
2689
2690 ocean_assert(input != nullptr && output != nullptr);
2691 ocean_assert(inputWidth > 0u && inputHeight > 0u);
2692 ocean_assert(outputWidth >= 4u && outputHeight > 0u);
2693 ocean_assert(input_H_output != nullptr);
2694
2695 ocean_assert_and_suppress_unused(firstOutputRow + numberOutputRows <= outputHeight, outputHeight);
2696
2697 const unsigned int inputStrideElements = inputWidth * tChannels + inputPaddingElements;
2698 const unsigned int outputStrideElements = outputWidth * tChannels + outputPaddingElements;
2699
2700 using PixelType = typename DataType<uint8_t, tChannels>::Type;
2701
2702 uint8_t zeroColor[tChannels] = {uint8_t(0)};
2703 const PixelType* const bColor = borderColor ? (PixelType*)borderColor : (PixelType*)zeroColor;
2704
2705 OCEAN_ALIGN_DATA(16) unsigned int validPixels[4];
2706
2707 OCEAN_ALIGN_DATA(16) unsigned int topLeftOffsets[4];
2708 OCEAN_ALIGN_DATA(16) unsigned int topRightOffsets[4];
2709 OCEAN_ALIGN_DATA(16) unsigned int bottomLeftOffsets[4];
2710 OCEAN_ALIGN_DATA(16) unsigned int bottomRightOffsets[4];
2711
2712 // we store 4 floats: [X0, X0, X0, X0], and same with X1 and X2
2713 const __m128 m128_f_X0 = _mm_set_ps1(float((*input_H_output)(0, 0)));
2714 const __m128 m128_f_X1 = _mm_set_ps1(float((*input_H_output)(1, 0)));
2715 const __m128 m128_f_X2 = _mm_set_ps1(float((*input_H_output)(2, 0)));
2716
2717 // we store 4 floats: [0.0f, 0.0f, 0.0f, 0.0f]
2718 const __m128 m128_f_zero = _mm_setzero_ps();
2719
2720 // we store 4 integers: [tChannels, tChannels, tChannels, tChannels]
2721 const __m128i m128_i_channels = _mm_set1_epi32(tChannels);
2722
2723 // we store 4 integers: [inputStrideElements, inputStrideElements, inputStrideElements, inputStrideElements]
2724 const __m128i m128_i_inputStrideElements = _mm_set1_epi32(inputStrideElements);
2725
2726 // we store 4 integers: [inputWidth - 1, inputWidth - 1, inputWidth -1, inputWidth -1], and same with inputHeight
2727 const __m128i m128_i_inputWidth_1 = _mm_set1_epi32(int(inputWidth) - 1);
2728 const __m128i m128_i_inputHeight_1 = _mm_set1_epi32(int(inputHeight) - 1);
2729
2730 // we store 4 floats: [inputWidth - 1, inputWidth - 1, inputWidth - 1, inputWidth - 1], and same with inputHeight
2731 const __m128 m128_f_inputWidth_1 = _mm_set_ps1(float(inputWidth - 1u));
2732 const __m128 m128_f_inputHeight_1 = _mm_set_ps1(float(inputHeight - 1u));
2733
2734 for (unsigned int y = firstOutputRow; y < firstOutputRow + numberOutputRows; ++y)
2735 {
2736 PixelType* outputPixelData = (PixelType*)(output + y * outputStrideElements);
2737
2738 /*
2739 * We can slightly optimize the 3x3 matrix multiplication:
2740 *
2741 * | X0 Y0 Z0 | | x |
2742 * | X1 Y1 Z1 | * | y |
2743 * | X2 Y2 Z2 | | 1 |
2744 *
2745 * | xx | | X0 * x | | Y0 * y + Z0 |
2746 * | yy | = | X1 * x | + | Y1 * y + Z1 |
2747 * | zz | | X2 * x | | Y2 * y + Z2 |
2748 *
2749 * | xx | | X0 * x | | C0 |
2750 * | yy | = | X1 * x | + | C1 |
2751 * | zz | | X2 * x | | C2 |
2752 *
2753 * As y is constant within the inner loop, we can pre-calculate the following terms:
2754 *
2755 * | x' | | (X0 * x + C0) / (X2 * x + C2) |
2756 * | y' | = | (X1 * x + C1) / (X2 * x + C2) |
2757 */
2758
2759 // we store 4 floats: [C0, C0, C0, C0], and same with C1 and C2
2760 const __m128 m128_f_C0 = _mm_set_ps1(float((*input_H_output)(0, 1) * Scalar(y) + (*input_H_output)(0, 2)));
2761 const __m128 m128_f_C1 = _mm_set_ps1(float((*input_H_output)(1, 1) * Scalar(y) + (*input_H_output)(1, 2)));
2762 const __m128 m128_f_C2 = _mm_set_ps1(float((*input_H_output)(2, 1) * Scalar(y) + (*input_H_output)(2, 2)));
2763
2764 for (unsigned int x = 0u; x < outputWidth; x += 4u)
2765 {
2766 if (x + 4u > outputWidth)
2767 {
2768 // the last iteration will not fit into the output frame,
2769 // so we simply shift x left by some pixels (at most 3) and we will calculate some pixels again
2770
2771 ocean_assert(x >= 4u && outputWidth > 4u);
2772 const unsigned int newX = outputWidth - 4u;
2773
2774 ocean_assert(x > newX);
2775 outputPixelData -= x - newX;
2776
2777 x = newX;
2778
2779 // the for loop will stop after this iteration
2780 ocean_assert(!(x + 4u < outputWidth));
2781 }
2782
2783
2784 // we need four successive x coordinate floats:
2785 // [x + 3.0f, x + 2.0f, x + 1.0f; x + 0.0f]
2786 const __m128 m128_f_x_0123 = _mm_set_ps(float(x + 3u), float(x + 2u), float(x + 1u), float(x + 0u));
2787
2788 // we calculate xx and yy and zz for [x + 3.0f, x + 2.0f, x + 1.0f, x + 0.0f]
2789 const __m128 m128_f_xx = _mm_add_ps(_mm_mul_ps(m128_f_X0, m128_f_x_0123), m128_f_C0);
2790 const __m128 m128_f_yy = _mm_add_ps(_mm_mul_ps(m128_f_X1, m128_f_x_0123), m128_f_C1);
2791 const __m128 m128_f_zz = _mm_add_ps(_mm_mul_ps(m128_f_X2, m128_f_x_0123), m128_f_C2);
2792
2793#ifdef USE_APPROXIMATED_INVERSE_OF_ZZ // (not defined by default)
2794
2795 // we calculate the (approximated) inverse of zz,
2796 // the overall performance will be approx. 5% better while the accuracy will be slightly worse:
2797 // [1/zz3, 1/zz2, 1/zz1, 1/zz0]
2798 const __m128 inv_zz_128 = _mm_rcp_ps(m128_f_zz);
2799
2800 // we determine the normalized coordinates x' and y' for for x + 3.0f, x + 2.0f, ...)
2801 const __m128 m128_f_inputX = _mm_mul_ps(m128_f_xx, inv_zz_128);
2802 const __m128 m128_f_inputY = _mm_mul_ps(m128_f_yy, inv_zz_128);
2803
2804#else
2805
2806 // we determine the normalized coordinates x' and y' for for x + 3.0f, x + 2.0f, ...)
2807 const __m128 m128_f_inputX = _mm_div_ps(m128_f_xx, m128_f_zz);
2808 const __m128 m128_f_inputY = _mm_div_ps(m128_f_yy, m128_f_zz);
2809
2810#endif // USE_APPROXIMATED_INVERSE_OF_ZZ
2811
2812
2813 // now we check whether we are inside the input frame
2814 const __m128 m128_f_validPixelX = _mm_and_ps(_mm_cmple_ps (m128_f_inputX, m128_f_inputWidth_1), _mm_cmpge_ps(m128_f_inputX, m128_f_zero)); // inputPosition.x() <= (inputWidth-1) && inputPosition.x() >= 0 ? 0xFFFFFF : 0x000000
2815 const __m128 m128_f_validPixelY = _mm_and_ps(_mm_cmple_ps (m128_f_inputY, m128_f_inputHeight_1), _mm_cmpge_ps(m128_f_inputY, m128_f_zero)); // inputPosition.y() <= (inputHeight-1) && inputPosition.y() >= 0 ? 0xFFFFFF : 0x000000
2816
2817 const __m128i m128_i_validPixel = _mm_castps_si128(_mm_and_ps(m128_f_validPixelX, m128_f_validPixelY)); // is_inside_input_frame(inputPosition) ? 0xFFFFFF : 0x000000
2818
2819 // we can stop here if all pixels are invalid
2820 if (_mm_test_all_zeros(m128_i_validPixel, _mm_set1_epi32(0xFFFFFFFF)))
2821 {
2822#ifdef OCEAN_DEBUG
2823 OCEAN_ALIGN_DATA(16) unsigned int debugValidPixels[4];
2824 _mm_store_si128((__m128i*)debugValidPixels, m128_i_validPixel);
2825 ocean_assert(!(debugValidPixels[0] || debugValidPixels[1] || debugValidPixels[2] || debugValidPixels[3]));
2826#endif
2827
2828 outputPixelData[0] = *bColor;
2829 outputPixelData[1] = *bColor;
2830 outputPixelData[2] = *bColor;
2831 outputPixelData[3] = *bColor;
2832
2833 outputPixelData += 4;
2834
2835 continue;
2836 }
2837
2838 // we store the result
2839 _mm_store_si128((__m128i*)validPixels, m128_i_validPixel);
2840 ocean_assert(validPixels[0] || validPixels[1] || validPixels[2] || validPixels[3]);
2841
2842
2843 // now we determine the left, top, right and bottom pixel used for the interpolation
2844 const __m128 m128_f_tx_floor = _mm_floor_ps(m128_f_inputX);
2845 const __m128 m128_f_ty_floor = _mm_floor_ps(m128_f_inputY);
2846
2847 // left = floor(x); top = floor(y)
2848 const __m128i m128_i_left = _mm_cvtps_epi32(m128_f_tx_floor);
2849 const __m128i m128_i_top = _mm_cvtps_epi32(m128_f_ty_floor);
2850
2851 // right = min(left + 1, width - 1); bottom = min(top + 1; height - 1)
2852 const __m128i m128_i_right = _mm_min_epu32(_mm_add_epi32(m128_i_left, _mm_set1_epi32(1)), m128_i_inputWidth_1);
2853 const __m128i m128_i_bottom = _mm_min_epu32(_mm_add_epi32(m128_i_top, _mm_set1_epi32(1)), m128_i_inputHeight_1);
2854
2855 // offset = (y * inputStrideElements + tChannels * x)
2856 const __m128i m128_i_topLeftOffset = _mm_add_epi32(_mm_mullo_epi32(m128_i_top, m128_i_inputStrideElements), _mm_mullo_epi32(m128_i_channels, m128_i_left)); // topleftOffset = (top * inputStrideElements + tChannels * left)
2857 const __m128i m128_i_topRightOffset = _mm_add_epi32(_mm_mullo_epi32(m128_i_top, m128_i_inputStrideElements), _mm_mullo_epi32(m128_i_channels, m128_i_right)); // toprightOffset = (top * inputStrideElements + tChannels * right)
2858 const __m128i m128_i_bottomLeftOffset = _mm_add_epi32(_mm_mullo_epi32(m128_i_bottom, m128_i_inputStrideElements), _mm_mullo_epi32(m128_i_channels, m128_i_left)); // ...
2859 const __m128i m128_i_bottomRightOffset = _mm_add_epi32(_mm_mullo_epi32(m128_i_bottom, m128_i_inputStrideElements), _mm_mullo_epi32(m128_i_channels, m128_i_right));
2860
2861 // we store the offsets
2862 _mm_store_si128((__m128i*)topLeftOffsets, m128_i_topLeftOffset);
2863 _mm_store_si128((__m128i*)topRightOffsets, m128_i_topRightOffset);
2864 _mm_store_si128((__m128i*)bottomLeftOffsets, m128_i_bottomLeftOffset);
2865 _mm_store_si128((__m128i*)bottomRightOffsets, m128_i_bottomRightOffset);
2866
2867
2868 // now we need to determine the interpolation factors tx, tx_ and ty, ty_: (top_left * tx_ + top_right * tx) * ty_ + (bottom_left * tx_ + bottom_right * tx) * ty
2869
2870 // we determine the fractional portions of the x' and y':
2871 // e.g., [43.1231, -12.5543, -34.123, 99.2]
2872 // [ 0.1231, 0.4457, 0.877, 0.2] // note the result for negative value - but we will not process negative values anyway due to 'validPixel'
2873 __m128 m128_f_tx = _mm_sub_ps(m128_f_inputX, m128_f_tx_floor);
2874 __m128 m128_f_ty = _mm_sub_ps(m128_f_inputY, m128_f_ty_floor);
2875
2876 // we use integer interpolation [0.0, 1.0] -> [0, 128]
2877 m128_f_tx = _mm_mul_ps(m128_f_tx, _mm_set_ps1(128.0f));
2878 m128_f_ty = _mm_mul_ps(m128_f_ty, _mm_set_ps1(128.0f));
2879
2880 m128_f_tx = _mm_round_ps(m128_f_tx, _MM_FROUND_TO_NEAREST_INT);
2881 m128_f_ty = _mm_round_ps(m128_f_ty, _MM_FROUND_TO_NEAREST_INT);
2882
2883 const __m128i m128_i_tx = _mm_cvtps_epi32(m128_f_tx);
2884 const __m128i m128_i_ty = _mm_cvtps_epi32(m128_f_ty);
2885
2886 interpolate4Pixels8BitPerChannelSSE<tChannels>(input, topLeftOffsets, topRightOffsets, bottomLeftOffsets, bottomRightOffsets, validPixels, *bColor, m128_i_tx, m128_i_ty, outputPixelData);
2887 outputPixelData += 4;
2888 }
2889 }
2890}
2891
2892template <>
2893OCEAN_FORCE_INLINE __m128i FrameInterpolatorBilinear::interpolate4Pixels8BitPerChannelSSE<3u>(const __m128i& sourcesTopLeft, const __m128i& sourcesTopRight, const __m128i& sourcesBottomLeft, const __m128i& sourcesBottomRight, const __m128i& factorsTopLeft, const __m128i& factorsTopRight, const __m128i& factorsBottomLeft, const __m128i& factorsBottomRight)
2894{
2895 // sourcesTopLeft stores the three color values of 4 (independent) pixels (the upper left pixels):
2896 // FEDC BA98 7654 3210
2897 // ---- VUYV UYVU YVUY
2898 // sourcesTopRight, sourcesBottomLeft, sourcesBottomRight have the same pattern
2899
2900 // factorsTopLeft stores the 32 bit interpolation values for 4 pixels:
2901 // FEDC BA98 7654 3210
2902 // 3 2 1 0 (32 bit interpolation values, fitting into 16 bit)
2903
2904
2905 // we will simply extract each channel from the source pixels,
2906 // each extracted channel will be multiplied by the corresponding interpolation factor
2907 // and all interpolation results will be accumulated afterwards
2908
2909 // FEDC BA98 7654 3210
2910 const __m128i mask32_Channel0 = SSE::set128i(0xFFFFFF09FFFFFF06ull, 0xFFFFFF03FFFFFF00ull); // ---9 ---6 ---3 ---0
2911 const __m128i mask32_Channel1 = SSE::set128i(0xFFFFFF0AFFFFFF07ull, 0xFFFFFF04FFFFFF01ull); // ---A ---7 ---4 ---1
2912 const __m128i mask32_Channel2 = SSE::set128i(0xFFFFFF0BFFFFFF08ull, 0xFFFFFF05FFFFFF02ull); // ---B ---8 ---5 ---2
2913
2914
2915 // we extract the first channel from the top left pixel values and multiply the channel with the interpolation factors
2916 // FEDC BA98 7654 3210
2917 // ---9 ---6 ---3 ---0
2918 // *
2919 // FTL3 FTL2 FTL1 FTL0
2920 __m128i multiplication_channel0 = _mm_mullo_epi32(factorsTopLeft, _mm_shuffle_epi8(sourcesTopLeft, mask32_Channel0));
2921
2922 // we the same multiplication for the second channel
2923 __m128i multiplication_channel1 = _mm_mullo_epi32(factorsTopLeft, _mm_shuffle_epi8(sourcesTopLeft, mask32_Channel1));
2924
2925 // and third channel
2926 __m128i multiplication_channel2 = _mm_mullo_epi32(factorsTopLeft, _mm_shuffle_epi8(sourcesTopLeft, mask32_Channel2));
2927
2928
2929 // now we repeat the process for the top right pixel values
2930 multiplication_channel0 = _mm_add_epi32(multiplication_channel0, _mm_mullo_epi32(factorsTopRight, _mm_shuffle_epi8(sourcesTopRight, mask32_Channel0)));
2931 multiplication_channel1 = _mm_add_epi32(multiplication_channel1, _mm_mullo_epi32(factorsTopRight, _mm_shuffle_epi8(sourcesTopRight, mask32_Channel1)));
2932 multiplication_channel2 = _mm_add_epi32(multiplication_channel2, _mm_mullo_epi32(factorsTopRight, _mm_shuffle_epi8(sourcesTopRight, mask32_Channel2)));
2933
2934
2935 // and for the bottom left pixel values
2936 multiplication_channel0 = _mm_add_epi32(multiplication_channel0, _mm_mullo_epi32(factorsBottomLeft, _mm_shuffle_epi8(sourcesBottomLeft, mask32_Channel0)));
2937 multiplication_channel1 = _mm_add_epi32(multiplication_channel1, _mm_mullo_epi32(factorsBottomLeft, _mm_shuffle_epi8(sourcesBottomLeft, mask32_Channel1)));
2938 multiplication_channel2 = _mm_add_epi32(multiplication_channel2, _mm_mullo_epi32(factorsBottomLeft, _mm_shuffle_epi8(sourcesBottomLeft, mask32_Channel2)));
2939
2940
2941 // and for the bottom right pixel values
2942 multiplication_channel0 = _mm_add_epi32(multiplication_channel0, _mm_mullo_epi32(factorsBottomRight, _mm_shuffle_epi8(sourcesBottomRight, mask32_Channel0)));
2943 multiplication_channel1 = _mm_add_epi32(multiplication_channel1, _mm_mullo_epi32(factorsBottomRight, _mm_shuffle_epi8(sourcesBottomRight, mask32_Channel1)));
2944 multiplication_channel2 = _mm_add_epi32(multiplication_channel2, _mm_mullo_epi32(factorsBottomRight, _mm_shuffle_epi8(sourcesBottomRight, mask32_Channel2)));
2945
2946
2947 const __m128i m128_i_8192 = _mm_set1_epi32(8192);
2948
2949 // we add 8192 for rounding and shift the result by 14 bits (division by 128*128) // TODO if using 256 we should be able to avoid the shifting by 14 bits (simply by using shuffle operations)
2950
2951 // in addition to rounding and shifting, we need to move the interpolation results to the correct channel:
2952 // target data: ---9 ---6 ---3 ---0
2953 // shufflet target: ---- --9- -6-- 3--0
2954 // mask location: ---C ---8 ---4 ---0
2955 // mask: ---- --C- -8-- 4--0
2956 __m128i interpolation_channel0 = _mm_shuffle_epi8(_mm_srli_epi32(_mm_add_epi32(multiplication_channel0, m128_i_8192), 14), SSE::set128i(0xFFFFFFFFFFFF0CFFull, 0xFF08FFFF04FFFF00ull));
2957
2958 // target data: ---A ---7 ---4 ---1
2959 // shufflet target: ---- -A-- 7--4 --1-
2960 // mask location: ---C ---8 ---4 ---0
2961 // mask: ---- -C-- 8--4 --0-
2962 __m128i interpolation_channel1 = _mm_shuffle_epi8(_mm_srli_epi32(_mm_add_epi32(multiplication_channel1, m128_i_8192), 14), SSE::set128i(0xFFFFFFFFFF0CFFFFull, 0x08FFFF04FFFF00FFull));
2963
2964 // target data: ---B ---8 ---5 ---2
2965 // shufflet target: ---- B--8 --5- -2--
2966 // mask location: ---C ---8 ---4 ---0
2967 // mask: ---- C--8 --4- -0--
2968 __m128i interpolation_channel2 = _mm_shuffle_epi8(_mm_srli_epi32(_mm_add_epi32(multiplication_channel2, m128_i_8192), 14), SSE::set128i(0xFFFFFFFF0CFFFF08ull, 0xFFFF04FFFF00FFFFull));
2969
2970
2971 // finally, we simply blend all interpolation results together
2972
2973 return _mm_or_si128(_mm_or_si128(interpolation_channel0, interpolation_channel1), interpolation_channel2);
2974}
2975
2976template <>
2977OCEAN_FORCE_INLINE __m128i FrameInterpolatorBilinear::interpolate4Pixels8BitPerChannelSSE<4u>(const __m128i& sourcesTopLeft, const __m128i& sourcesTopRight, const __m128i& sourcesBottomLeft, const __m128i& sourcesBottomRight, const __m128i& factorsTopLeft, const __m128i& factorsTopRight, const __m128i& factorsBottomLeft, const __m128i& factorsBottomRight)
2978{
2979 // sourcesTopLeft stores the four color values of 4 (independent) pixels (the upper left pixels):
2980 // FEDC BA98 7654 3210
2981 // AVUY AVUY AVUY AVUY
2982 // sourcesTopRight, sourcesBottomLeft, sourcesBottomRight have the same pattern
2983
2984 // factorsTopLeft stores the 32 bit interpolation values for 4 pixels:
2985 // FEDC BA98 7654 3210
2986 // 3 2 1 0 (32 bit interpolation values, fitting into 16 bit)
2987
2988
2989 // we will simply extract each channel from the source pixels,
2990 // each extracted channel will be multiplied by the corresponding interpolation factor
2991 // and all interpolation results will be accumulated afterwards
2992
2993 // FEDC BA98 7654 3210
2994 const __m128i mask32_Channel0 = SSE::set128i(0xA0A0A00CA0A0A008ull, 0xA0A0A004A0A0A000ull); // ---C ---8 ---4 ---0
2995 const __m128i mask32_Channel1 = SSE::set128i(0xA0A0A00DA0A0A009ull, 0xA0A0A005A0A0A001ull); // ---D ---9 ---5 ---1
2996 const __m128i mask32_Channel2 = SSE::set128i(0xA0A0A00EA0A0A00Aull, 0xA0A0A006A0A0A002ull); // ---E ---A ---6 ---2
2997 const __m128i mask32_Channel3 = SSE::set128i(0xA0A0A00FA0A0A00Bull, 0xA0A0A007A0A0A003ull); // ---F ---B ---7 ---3
2998
2999
3000 // we extract the first channel from the top left pixel values and multiply the channel with the interpolation factors
3001 // FEDC BA98 7654 3210
3002 // ---C ---8 ---4 ---0
3003 // *
3004 // FTL3 FTL2 FTL1 FTL0
3005 __m128i multiplication_channel0 = _mm_mullo_epi32(factorsTopLeft, _mm_shuffle_epi8(sourcesTopLeft, mask32_Channel0));
3006
3007 // we the same multiplication for the second channel
3008 __m128i multiplication_channel1 = _mm_mullo_epi32(factorsTopLeft, _mm_shuffle_epi8(sourcesTopLeft, mask32_Channel1));
3009
3010 // and third channel
3011 __m128i multiplication_channel2 = _mm_mullo_epi32(factorsTopLeft, _mm_shuffle_epi8(sourcesTopLeft, mask32_Channel2));
3012
3013 // and last channel
3014 __m128i multiplication_channel3 = _mm_mullo_epi32(factorsTopLeft, _mm_shuffle_epi8(sourcesTopLeft, mask32_Channel3));
3015
3016
3017 // now we repeat the process for the top right pixel values
3018 multiplication_channel0 = _mm_add_epi32(multiplication_channel0, _mm_mullo_epi32(factorsTopRight, _mm_shuffle_epi8(sourcesTopRight, mask32_Channel0)));
3019 multiplication_channel1 = _mm_add_epi32(multiplication_channel1, _mm_mullo_epi32(factorsTopRight, _mm_shuffle_epi8(sourcesTopRight, mask32_Channel1)));
3020 multiplication_channel2 = _mm_add_epi32(multiplication_channel2, _mm_mullo_epi32(factorsTopRight, _mm_shuffle_epi8(sourcesTopRight, mask32_Channel2)));
3021 multiplication_channel3 = _mm_add_epi32(multiplication_channel3, _mm_mullo_epi32(factorsTopRight, _mm_shuffle_epi8(sourcesTopRight, mask32_Channel3)));
3022
3023
3024 // and for the bottom left pixel values
3025 multiplication_channel0 = _mm_add_epi32(multiplication_channel0, _mm_mullo_epi32(factorsBottomLeft, _mm_shuffle_epi8(sourcesBottomLeft, mask32_Channel0)));
3026 multiplication_channel1 = _mm_add_epi32(multiplication_channel1, _mm_mullo_epi32(factorsBottomLeft, _mm_shuffle_epi8(sourcesBottomLeft, mask32_Channel1)));
3027 multiplication_channel2 = _mm_add_epi32(multiplication_channel2, _mm_mullo_epi32(factorsBottomLeft, _mm_shuffle_epi8(sourcesBottomLeft, mask32_Channel2)));
3028 multiplication_channel3 = _mm_add_epi32(multiplication_channel3, _mm_mullo_epi32(factorsBottomLeft, _mm_shuffle_epi8(sourcesBottomLeft, mask32_Channel3)));
3029
3030
3031 // and for the bottom right pixel values
3032 multiplication_channel0 = _mm_add_epi32(multiplication_channel0, _mm_mullo_epi32(factorsBottomRight, _mm_shuffle_epi8(sourcesBottomRight, mask32_Channel0)));
3033 multiplication_channel1 = _mm_add_epi32(multiplication_channel1, _mm_mullo_epi32(factorsBottomRight, _mm_shuffle_epi8(sourcesBottomRight, mask32_Channel1)));
3034 multiplication_channel2 = _mm_add_epi32(multiplication_channel2, _mm_mullo_epi32(factorsBottomRight, _mm_shuffle_epi8(sourcesBottomRight, mask32_Channel2)));
3035 multiplication_channel3 = _mm_add_epi32(multiplication_channel3, _mm_mullo_epi32(factorsBottomRight, _mm_shuffle_epi8(sourcesBottomRight, mask32_Channel3)));
3036
3037
3038 const __m128i m128_i_8192 = _mm_set1_epi32(8192);
3039
3040 // we add 8192 for rounding and shift the result by 14 bits (division by 128*128)
3041
3042 // in addition to rounding and shifting, we need to move the interpolation results to the correct channel:
3043 // ---C ---8 ---4 ---0
3044 // ---C ---9 ---4 ---0
3045 __m128i interpolation_channel0 = _mm_srli_epi32(_mm_add_epi32(multiplication_channel0, m128_i_8192), 14);
3046
3047 // in addition to rounding and shifting, we need to move the interpolation results to the correct channel:
3048 // ---D ---9 ---5 ---1
3049 // --D- --9- --5- --1-
3050 __m128i interpolation_channel1 = _mm_slli_epi32(_mm_srli_epi32(_mm_add_epi32(multiplication_channel1, m128_i_8192), 14), 8);
3051
3052 // ---E ---A ---6 ---2
3053 // -E-- -A-- -6-- -2--
3054 __m128i interpolation_channel2 = _mm_slli_epi32(_mm_srli_epi32(_mm_add_epi32(multiplication_channel2, m128_i_8192), 14), 16);
3055
3056 // ---F ---B ---7 ---3
3057 // F--- B--- 7--- 3---
3058 __m128i interpolation_channel3 = _mm_slli_epi32(_mm_srli_epi32(_mm_add_epi32(multiplication_channel3, m128_i_8192), 14), 24);
3059
3060
3061 // finally, we simply blend all interpolation results together
3062
3063 return _mm_or_si128(_mm_or_si128(interpolation_channel0, interpolation_channel1), _mm_or_si128(interpolation_channel2, interpolation_channel3));
3064}
3065
3066#ifdef OCEAN_COMPILER_MSC
3067
3068// we see a significant performance decrease with non-VS compilers/platforms,
3069// so we do not use the 3channel version with non-Windows compilers
3070
3071template <>
3072OCEAN_FORCE_INLINE void FrameInterpolatorBilinear::interpolate4Pixels8BitPerChannelSSE<1u>(const uint8_t* source, const unsigned int offsetsTopLeft[4], const unsigned int offsetsTopRight[4], const unsigned int offsetsBottomLeft[4], const unsigned int offsetsBottomRight[4], const unsigned int validPixels[4], const DataType<uint8_t, 1u>::Type& borderColor, const __m128i& m128_factorsRight, const __m128i& m128_factorsBottom, typename DataType<uint8_t, 1u>::Type* targetPositionPixels)
3073{
3074 ocean_assert(source != nullptr);
3075 ocean_assert(targetPositionPixels != nullptr);
3076
3077 using PixelType = typename DataType<uint8_t, 1u>::Type;
3078
3079 // as we do not initialize the following intermediate data,
3080 // we hopefully will not allocate memory on the stack each time this function is called
3081 OCEAN_ALIGN_DATA(16) PixelType pixels[16];
3082
3083 // we gather the individual source pixel values from the source image,
3084 // based on the calculated pixel locations
3085 for (unsigned int i = 0u; i < 4u; ++i)
3086 {
3087 if (validPixels[i])
3088 {
3089 pixels[i * 4u + 0u] = *((PixelType*)(source + offsetsTopLeft[i]));
3090 pixels[i * 4u + 1u] = *((PixelType*)(source + offsetsTopRight[i]));
3091 pixels[i * 4u + 2u] = *((PixelType*)(source + offsetsBottomLeft[i]));
3092 pixels[i * 4u + 3u] = *((PixelType*)(source + offsetsBottomRight[i]));
3093 }
3094 else
3095 {
3096 pixels[i * 4u + 0u] = borderColor;
3097 pixels[i * 4u + 1u] = borderColor;
3098 pixels[i * 4u + 2u] = borderColor;
3099 pixels[i * 4u + 3u] = borderColor;
3100 }
3101 }
3102
3103 static_assert(sizeof(__m128i) == sizeof(pixels), "Invalid data type!");
3104
3105 const __m128i m128_pixels = _mm_load_si128((const __m128i*)pixels);
3106
3107
3108 // factorLeft = 128 - factorRight
3109 // factorTop = 128 - factorBottom
3110
3111 const __m128i m128_factorsLeft = _mm_sub_epi32(_mm_set1_epi32(128), m128_factorsRight);
3112 const __m128i m128_factorsTop = _mm_sub_epi32(_mm_set1_epi32(128), m128_factorsBottom);
3113
3114 // (top_left * factorLeft + top_right * factorRight) * factorTop + (bottom_left * factorLeft + bottom_right * factorRight) * factorBottom
3115 // == top_left * factorTopLeft + top_right * factorTopRight + bottom_left * factorBottomLeft + bottom_right * factorBottomRight
3116
3117 const __m128i m128_factorsTopLeft = _mm_mullo_epi32(m128_factorsTop, m128_factorsLeft);
3118 const __m128i m128_factorsTopRight = _mm_mullo_epi32(m128_factorsTop, m128_factorsRight);
3119 const __m128i m128_factorsBottomLeft = _mm_mullo_epi32(m128_factorsBottom, m128_factorsLeft);
3120 const __m128i m128_factorsBottomRight = _mm_mullo_epi32(m128_factorsBottom, m128_factorsRight);
3121
3122 // pixels stores the four interpolation grascale pixel values (top left, top right, bottom left, bottom right) for 4 (independent) pixels:
3123 // F E D C B A 9 8 7 6 5 4 3 2 1 0
3124 // BR BL TR TL BR BL TR TL BR BL TR TL BR BL TR TL
3125
3126 // factorsTopLeft stores the 32 bit interpolation values for 4 pixels:
3127 // FEDC BA98 7654 3210
3128 // 3 2 1 0 (32 bit interpolation values, fitting into 16 bit)
3129
3130
3131 // we will simply extract each channel from the source pixels,
3132 // each extracted channel will be multiplied by the corresponding interpolation factor
3133 // and all interpolation results will be accumulated afterwards
3134
3135 // FEDC BA98 7654 3210
3136 const __m128i mask32_topLeft = SSE::set128i(0xFFFFFF0CFFFFFF08ull, 0xFFFFFF04FFFFFF00ull); // ---C ---8 ---4 ---0
3137 const __m128i mask32_topRight = SSE::set128i(0xFFFFFF0DFFFFFF09ull, 0xFFFFFF05FFFFFF01ull); // ---D ---9 ---5 ---1
3138 const __m128i mask32_bottomLeft = SSE::set128i(0xFFFFFF0EFFFFFF0Aull, 0xFFFFFF06FFFFFF02ull); // ---E ---A ---6 ---2
3139 const __m128i mask32_bottomRight = SSE::set128i(0xFFFFFF0FFFFFFF0Bull, 0xFFFFFF07FFFFFF03ull); // ---F ---B ---7 ---3
3140
3141
3142 // we extract the top left values and multiply them with the interpolation factors
3143 // FEDC BA98 7654 3210
3144 // ---C ---8 ---4 ---0
3145 // *
3146 // FTL3 FTL2 FTL1 FTL0
3147 __m128i multiplicationA = _mm_mullo_epi32(m128_factorsTopLeft, _mm_shuffle_epi8(m128_pixels, mask32_topLeft));
3148 __m128i multiplicationB = _mm_mullo_epi32(m128_factorsTopRight, _mm_shuffle_epi8(m128_pixels, mask32_topRight));
3149
3150 multiplicationA = _mm_add_epi32(multiplicationA, _mm_mullo_epi32(m128_factorsBottomLeft, _mm_shuffle_epi8(m128_pixels, mask32_bottomLeft)));
3151 multiplicationB = _mm_add_epi32(multiplicationB, _mm_mullo_epi32(m128_factorsBottomRight, _mm_shuffle_epi8(m128_pixels, mask32_bottomRight)));
3152
3153 __m128i multiplication = _mm_add_epi32(multiplicationA, multiplicationB);
3154
3155 const __m128i m128_i_8192 = _mm_set1_epi32(8192);
3156
3157 // we add 8192 for rounding and shift the result by 14 bits (division by 128*128) // TODO if using 256 we should be able to avoid the shifting by 14 bits (simply by using shuffle operations)
3158 // additionally, we shuffle the individual results together
3159
3160 const __m128i result = _mm_shuffle_epi8(_mm_srli_epi32(_mm_add_epi32(multiplication, m128_i_8192), 14), SSE::set128i(0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF0C080400ull));
3161
3162 *((unsigned int*)targetPositionPixels) = _mm_extract_epi32(result, 0);
3163}
3164
3165template <>
3166OCEAN_FORCE_INLINE void FrameInterpolatorBilinear::interpolate4Pixels8BitPerChannelSSE<3u>(const uint8_t* source, const unsigned int offsetsTopLeft[4], const unsigned int offsetsTopRight[4], const unsigned int offsetsBottomLeft[4], const unsigned int offsetsBottomRight[4], const unsigned int validPixels[4], const DataType<uint8_t, 3u>::Type& borderColor, const __m128i& m128_factorsRight, const __m128i& m128_factorsBottom, typename DataType<uint8_t, 3u>::Type* targetPositionPixels)
3167{
3168 ocean_assert(source != nullptr);
3169 ocean_assert(targetPositionPixels != nullptr);
3170
3171 using PixelType = typename DataType<uint8_t, 3u>::Type;
3172
3173 // as we do not initialize the following intermediate data,
3174 // we hopefully will not allocate memory on the stack each time this function is called
3175 OCEAN_ALIGN_DATA(16) PixelType topLeftPixels[6];
3176 OCEAN_ALIGN_DATA(16) PixelType topRightPixels[6];
3177 OCEAN_ALIGN_DATA(16) PixelType bottomLeftPixels[6];
3178 OCEAN_ALIGN_DATA(16) PixelType bottomRightPixels[6];
3179
3180 // we gather the individual source pixel values from the source image,
3181 // based on the calculated pixel locations
3182 for (unsigned int i = 0u; i < 4u; ++i)
3183 {
3184 if (validPixels[i])
3185 {
3186 topLeftPixels[i] = *((PixelType*)(source + offsetsTopLeft[i]));
3187 topRightPixels[i] = *((PixelType*)(source + offsetsTopRight[i]));
3188 bottomLeftPixels[i] = *((PixelType*)(source + offsetsBottomLeft[i]));
3189 bottomRightPixels[i] = *((PixelType*)(source + offsetsBottomRight[i]));
3190 }
3191 else
3192 {
3193 topLeftPixels[i] = borderColor;
3194 topRightPixels[i] = borderColor;
3195 bottomLeftPixels[i] = borderColor;
3196 bottomRightPixels[i] = borderColor;
3197 }
3198 }
3199
3200 static_assert(sizeof(__m128i) <= sizeof(topLeftPixels), "Invalid data type!");
3201
3202 const __m128i m128_topLeftPixels = _mm_load_si128((const __m128i*)topLeftPixels);
3203 const __m128i m128_topRightPixels = _mm_load_si128((const __m128i*)topRightPixels);
3204 const __m128i m128_bottomLeftPixels = _mm_load_si128((const __m128i*)bottomLeftPixels);
3205 const __m128i m128_bottomRightPixels = _mm_load_si128((const __m128i*)bottomRightPixels);
3206
3207
3208 // factorLeft = 128 - factorRight
3209 // factorTop = 128 - factorBottom
3210
3211 const __m128i m128_factorsLeft = _mm_sub_epi32(_mm_set1_epi32(128), m128_factorsRight);
3212 const __m128i m128_factorsTop = _mm_sub_epi32(_mm_set1_epi32(128), m128_factorsBottom);
3213
3214 // (top_left * factorLeft + top_right * factorRight) * factorTop + (bottom_left * factorLeft + bottom_right * factorRight) * factorBottom
3215 // == top_left * factorTopLeft + top_right * factorTopRight + bottom_left * factorBottomLeft + bottom_right * factorBottomRight
3216
3217 const __m128i m128_factorsTopLeft = _mm_mullo_epi32(m128_factorsTop, m128_factorsLeft);
3218 const __m128i m128_factorsTopRight = _mm_mullo_epi32(m128_factorsTop, m128_factorsRight);
3219 const __m128i m128_factorsBottomLeft = _mm_mullo_epi32(m128_factorsBottom, m128_factorsLeft);
3220 const __m128i m128_factorsBottomRight = _mm_mullo_epi32(m128_factorsBottom, m128_factorsRight);
3221
3222
3223 const __m128i m128_interpolationResult = interpolate4Pixels8BitPerChannelSSE<3u>(m128_topLeftPixels, m128_topRightPixels, m128_bottomLeftPixels, m128_bottomRightPixels, m128_factorsTopLeft, m128_factorsTopRight, m128_factorsBottomLeft, m128_factorsBottomRight);
3224
3225 // we copy the first 12 bytes
3226 memcpy(targetPositionPixels, &m128_interpolationResult, 12u);
3227}
3228
3229#endif // OCEAN_COMPILER_MSC
3230
3231template <>
3232OCEAN_FORCE_INLINE void FrameInterpolatorBilinear::interpolate4Pixels8BitPerChannelSSE<4u>(const uint8_t* source, const unsigned int offsetsTopLeft[4], const unsigned int offsetsTopRight[4], const unsigned int offsetsBottomLeft[4], const unsigned int offsetsBottomRight[4], const unsigned int validPixels[4], const DataType<uint8_t, 4u>::Type& borderColor, const __m128i& m128_factorsRight, const __m128i& m128_factorsBottom, typename DataType<uint8_t, 4u>::Type* targetPositionPixels)
3233{
3234 ocean_assert(source != nullptr);
3235 ocean_assert(targetPositionPixels != nullptr);
3236
3237 using PixelType = typename DataType<uint8_t, 4u>::Type;
3238
3239 // as we do not initialize the following intermediate data,
3240 // we hopefully will not allocate memory on the stack each time this function is called
3241 OCEAN_ALIGN_DATA(16) PixelType topLeftPixels[4];
3242 OCEAN_ALIGN_DATA(16) PixelType topRightPixels[4];
3243 OCEAN_ALIGN_DATA(16) PixelType bottomLeftPixels[4];
3244 OCEAN_ALIGN_DATA(16) PixelType bottomRightPixels[4];
3245
3246 // we gather the individual source pixel values from the source image,
3247 // based on the calculated pixel locations
3248
3249 for (unsigned int i = 0u; i < 4u; ++i)
3250 {
3251 if (validPixels[i])
3252 {
3253 topLeftPixels[i] = *((PixelType*)(source + offsetsTopLeft[i]));
3254 topRightPixels[i] = *((PixelType*)(source + offsetsTopRight[i]));
3255 bottomLeftPixels[i] = *((PixelType*)(source + offsetsBottomLeft[i]));
3256 bottomRightPixels[i] = *((PixelType*)(source + offsetsBottomRight[i]));
3257 }
3258 else
3259 {
3260 topLeftPixels[i] = borderColor;
3261 topRightPixels[i] = borderColor;
3262 bottomLeftPixels[i] = borderColor;
3263 bottomRightPixels[i] = borderColor;
3264 }
3265 }
3266
3267 static_assert(sizeof(__m128i) == sizeof(topLeftPixels), "Invalid data type!");
3268
3269 const __m128i m128_topLeftPixels = _mm_load_si128((const __m128i*)topLeftPixels);
3270 const __m128i m128_topRightPixels = _mm_load_si128((const __m128i*)topRightPixels);
3271 const __m128i m128_bottomLeftPixels = _mm_load_si128((const __m128i*)bottomLeftPixels);
3272 const __m128i m128_bottomRightPixels = _mm_load_si128((const __m128i*)bottomRightPixels);
3273
3274
3275 // factorLeft = 128 - factorRight
3276 // factorTop = 128 - factorBottom
3277
3278 const __m128i m128_factorsLeft = _mm_sub_epi32(_mm_set1_epi32(128), m128_factorsRight);
3279 const __m128i m128_factorsTop = _mm_sub_epi32(_mm_set1_epi32(128), m128_factorsBottom);
3280
3281 // (top_left * factorLeft + top_right * factorRight) * factorTop + (bottom_left * factorLeft + bottom_right * factorRight) * factorBottom
3282 // == top_left * factorTopLeft + top_right * factorTopRight + bottom_left * factorBottomLeft + bottom_right * factorBottomRight
3283
3284 const __m128i m128_factorsTopLeft = _mm_mullo_epi32(m128_factorsTop, m128_factorsLeft);
3285 const __m128i m128_factorsTopRight = _mm_mullo_epi32(m128_factorsTop, m128_factorsRight);
3286 const __m128i m128_factorsBottomLeft = _mm_mullo_epi32(m128_factorsBottom, m128_factorsLeft);
3287 const __m128i m128_factorsBottomRight = _mm_mullo_epi32(m128_factorsBottom, m128_factorsRight);
3288
3289
3290 const __m128i m128_interpolationResult = interpolate4Pixels8BitPerChannelSSE<4u>(m128_topLeftPixels, m128_topRightPixels, m128_bottomLeftPixels, m128_bottomRightPixels, m128_factorsTopLeft, m128_factorsTopRight, m128_factorsBottomLeft, m128_factorsBottomRight);
3291
3292 _mm_storeu_si128((__m128i*)targetPositionPixels, m128_interpolationResult);
3293}
3294
3295template <unsigned int tChannels>
3296OCEAN_FORCE_INLINE void FrameInterpolatorBilinear::interpolate4Pixels8BitPerChannelSSE(const uint8_t* source, const unsigned int offsetsTopLeft[4], const unsigned int offsetsTopRight[4], const unsigned int offsetsBottomLeft[4], const unsigned int offsetsBottomRight[4], const unsigned int validPixels[4], const typename DataType<uint8_t, tChannels>::Type& borderColor, const __m128i& m128_factorsRight, const __m128i& m128_factorsBottom, typename DataType<uint8_t, tChannels>::Type* targetPositionPixels)
3297{
3298 ocean_assert(source != nullptr);
3299 ocean_assert(targetPositionPixels != nullptr);
3300
3301 // as we do not initialize the following intermediate data,
3302 // we hopefully will not allocate memory on the stack each time this function is called
3303 OCEAN_ALIGN_DATA(16) unsigned int factorsTopLeft[4];
3304 OCEAN_ALIGN_DATA(16) unsigned int factorsTopRight[4];
3305 OCEAN_ALIGN_DATA(16) unsigned int factorsBottomLeft[4];
3306 OCEAN_ALIGN_DATA(16) unsigned int factorsBottomRight[4];
3307
3308
3309 // factorLeft = 128 - factorRight
3310 // factorTop = 128 - factorBottom
3311
3312 const __m128i m128_factorsLeft = _mm_sub_epi32(_mm_set1_epi32(128), m128_factorsRight);
3313 const __m128i m128_factorsTop = _mm_sub_epi32(_mm_set1_epi32(128), m128_factorsBottom);
3314
3315 // (top_left * factorLeft + top_right * factorRight) * factorTop + (bottom_left * factorLeft + bottom_right * factorRight) * factorBottom
3316 // == top_left * factorTopLeft + top_right * factorTopRight + bottom_left * factorBottomLeft + bottom_right * factorBottomRight
3317
3318 const __m128i m128_factorsTopLeft = _mm_mullo_epi32(m128_factorsTop, m128_factorsLeft);
3319 const __m128i m128_factorsTopRight = _mm_mullo_epi32(m128_factorsTop, m128_factorsRight);
3320 const __m128i m128_factorsBottomLeft = _mm_mullo_epi32(m128_factorsBottom, m128_factorsLeft);
3321 const __m128i m128_factorsBottomRight = _mm_mullo_epi32(m128_factorsBottom, m128_factorsRight);
3322
3323
3324 // we store the interpolation factors
3325 _mm_store_si128((__m128i*)factorsTopLeft, m128_factorsTopLeft);
3326 _mm_store_si128((__m128i*)factorsTopRight, m128_factorsTopRight);
3327 _mm_store_si128((__m128i*)factorsBottomLeft, m128_factorsBottomLeft);
3328 _mm_store_si128((__m128i*)factorsBottomRight, m128_factorsBottomRight);
3329
3330 for (unsigned int i = 0u; i < 4u; ++i)
3331 {
3332 if (validPixels[i])
3333 {
3334 const uint8_t* topLeft = source + offsetsTopLeft[i];
3335 const uint8_t* topRight = source + offsetsTopRight[i];
3336
3337 const uint8_t* bottomLeft = source + offsetsBottomLeft[i];
3338 const uint8_t* bottomRight = source + offsetsBottomRight[i];
3339
3340 const unsigned int& factorTopLeft = factorsTopLeft[i];
3341 const unsigned int& factorTopRight = factorsTopRight[i];
3342 const unsigned int& factorBottomLeft = factorsBottomLeft[i];
3343 const unsigned int& factorBottomRight = factorsBottomRight[i];
3344
3345 for (unsigned int n = 0u; n < tChannels; ++n)
3346 {
3347 ((uint8_t*)targetPositionPixels)[n] = (uint8_t)((topLeft[n] * factorTopLeft + topRight[n] * factorTopRight + bottomLeft[n] * factorBottomLeft + bottomRight[n] * factorBottomRight + 8192u) >> 14u);
3348 }
3349 }
3350 else
3351 {
3352 *targetPositionPixels = borderColor;
3353 }
3354
3355 targetPositionPixels++;
3356 }
3357}
3358
3359#endif // OCEAN_HARDWARE_SSE_VERSION
3360
3361#if defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
3362
3363template <unsigned int tChannels>
3364void FrameInterpolatorBilinear::affine8BitPerChannelNEONSubset(const uint8_t* source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3* source_A_target, const uint8_t* borderColor, uint8_t* target, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int firstTargetRow, const unsigned int numberTargetRows, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements)
3365{
3366 static_assert(tChannels >= 1u, "Invalid channel number!");
3367
3368 ocean_assert(source && target);
3369 ocean_assert(sourceWidth > 0u && sourceHeight > 0u);
3370 ocean_assert_and_suppress_unused(targetWidth >= 4u && targetHeight > 0u, targetHeight);
3371 ocean_assert(source_A_target);
3372 ocean_assert(!source_A_target->isNull() && Numeric::isEqualEps((*source_A_target)[2]) && Numeric::isEqualEps((*source_A_target)[5]));
3373
3374 ocean_assert(firstTargetRow + numberTargetRows <= targetHeight);
3375
3376 const unsigned int sourceStrideElements = sourceWidth * tChannels + sourcePaddingElements;
3377 const unsigned int targetStrideElements = targetWidth * tChannels + targetPaddingElements;
3378
3379 using PixelType = typename DataType<uint8_t, tChannels>::Type;
3380
3381 uint8_t zeroColor[tChannels] = {uint8_t(0)};
3382 const PixelType* const bColor = borderColor ? (PixelType*)borderColor : (PixelType*)zeroColor;
3383
3384 unsigned int validPixels[4];
3385
3386 unsigned int topLeftOffsetsElements[4];
3387 unsigned int topRightOffsetsElements[4];
3388 unsigned int bottomLeftOffsetsElements[4];
3389 unsigned int bottomRightOffsetsElements[4];
3390
3391 const uint32x4_t constantChannels_u_32x4 = vdupq_n_u32(tChannels);
3392
3393 // we store 4 floats: [X0, X0, X0, X0], and same with X1 and X2
3394 const float32x4_t m128_f_X0 = vdupq_n_f32(float((*source_A_target)(0, 0)));
3395 const float32x4_t m128_f_X1 = vdupq_n_f32(float((*source_A_target)(1, 0)));
3396
3397 for (unsigned int y = firstTargetRow; y < firstTargetRow + numberTargetRows; ++y)
3398 {
3399 PixelType* targetRow = (PixelType*)(target + y * targetStrideElements);
3400
3401 /*
3402 * We can slightly optimize the 3x3 matrix multiplication:
3403 *
3404 * | X0 Y0 Z0 | | x |
3405 * | X1 Y1 Z1 | * | y |
3406 * | 0 0 1 | | 1 |
3407 *
3408 * | xx | | X0 * x | | Y0 * y + Z0 |
3409 * | yy | = | X1 * x | + | Y1 * y + Z1 |
3410 *
3411 * As y is constant within the inner loop, the two terms on the right side in the above equations can be pre-calculated:
3412 *
3413 * C0 = Y0 * y + Z0
3414 * C1 = Y1 * y + Z1
3415 *
3416 * So the computation becomes:
3417 *
3418 * | x' | | X0 * x | | C0 |
3419 * | y' | = | X1 * x | + | C1 |
3420 */
3421
3422 // we store 4 floats: [C0, C0, C0, C0], and same with C1 and C2
3423 const float32x4_t m128_f_C0 = vdupq_n_f32(float((*source_A_target)(0, 1) * Scalar(y) + (*source_A_target)(0, 2)));
3424 const float32x4_t m128_f_C1 = vdupq_n_f32(float((*source_A_target)(1, 1) * Scalar(y) + (*source_A_target)(1, 2)));
3425
3426 // we store 4 floats: [0.0f, 0.0f, 0.0f, 0.0f]
3427 const float32x4_t m128_f_zero = vdupq_n_f32(0.0f);
3428
3429 // we store 4 integers: [sourceStrideElements, sourceStrideElements, sourceStrideElements, sourceStrideElements]
3430 const uint32x4_t m128_u_sourceStrideElements = vdupq_n_u32(sourceStrideElements);
3431
3432 // we store 4 integers: [sourceWidth - 1, sourceWidth - 1, sourceWidth - 1, sourceWidth - 1], and same with sourceHeight
3433 const uint32x4_t m128_u_sourceWidth_1 = vdupq_n_u32(sourceWidth - 1u);
3434 const uint32x4_t m128_u_sourceHeight_1 = vdupq_n_u32(sourceHeight - 1u);
3435
3436 // we store 4 floats: [sourceWidth - 1, sourceWidth - 1, sourceWidth - 1, sourceWidth - 1], and same with sourceHeight
3437 const float32x4_t m128_f_sourceWidth_1 = vdupq_n_f32(float(sourceWidth - 1u));
3438 const float32x4_t m128_f_sourceHeight_1 = vdupq_n_f32(float(sourceHeight - 1u));
3439
3440 for (unsigned int x = 0u; x < targetWidth; x += 4u)
3441 {
3442 if (x + 4u > targetWidth)
3443 {
3444 // the last iteration will not fit into the target frame,
3445 // so we simply shift x left by some pixels (at most 3) and we will calculate some pixels again
3446
3447 ocean_assert(x >= 4u && targetWidth > 4u);
3448 const unsigned int newX = targetWidth - 4u;
3449
3450 ocean_assert(x > newX);
3451 targetRow -= x - newX;
3452
3453 x = newX;
3454
3455 // the for loop will stop after this iteration
3456 ocean_assert(!(x + 4u < targetWidth));
3457 }
3458
3459
3460 // we need four successive x coordinate floats:
3461 // [x + 3.0f, x + 2.0f, x + 1.0f; x + 0.0f]
3462 float x_0123[4] = {float(x + 0u), float(x + 1u), float(x + 2u), float(x + 3u)};
3463 const float32x4_t m128_f_x_0123 = vld1q_f32(x_0123);
3464
3465 // we calculate xx and yy and zz for [x + 3.0f, x + 2.0f, x + 1.0f, x + 0.0f]
3466 const float32x4_t m128_f_sourceX = vmlaq_f32(m128_f_C0, m128_f_X0, m128_f_x_0123);
3467 const float32x4_t m128_f_sourceY = vmlaq_f32(m128_f_C1, m128_f_X1, m128_f_x_0123);
3468
3469
3470 // now we check whether we are inside the source frame
3471 const uint32x4_t m128_u_validPixelX = vandq_u32(vcleq_f32(m128_f_sourceX, m128_f_sourceWidth_1), vcgeq_f32(m128_f_sourceX, m128_f_zero)); // sourcePosition.x() <= (sourceWidth - 1) && sourcePosition.x() >= 0 ? 0xFFFFFFFF : 0x00000000
3472 const uint32x4_t m128_u_validPixelY = vandq_u32(vcleq_f32(m128_f_sourceY, m128_f_sourceHeight_1), vcgeq_f32(m128_f_sourceY, m128_f_zero)); // sourcePosition.y() <= (sourceHeight - 1) && sourcePosition.y() >= 0 ? 0xFFFFFFFF : 0x00000000
3473
3474 const uint32x4_t m128_u_validPixel = vandq_u32(m128_u_validPixelX, m128_u_validPixelY); // is_inside_source_frame(sourcePosition) ? 0xFFFFFFFF : 0x00000000
3475
3476
3477 // we can stop here if all pixels are invalid
3478 const uint32x2_t m64_u_validPixel = vorr_u32(vget_low_u32(m128_u_validPixel), vget_high_u32(m128_u_validPixel));
3479 if ((vget_lane_u32(m64_u_validPixel, 0) | vget_lane_u32(m64_u_validPixel, 1)) == 0x00000000u)
3480 {
3481#ifdef OCEAN_DEBUG
3482 OCEAN_ALIGN_DATA(16) unsigned int debugValidPixels[4];
3483 vst1q_u32(debugValidPixels, m128_u_validPixel);
3484 ocean_assert(!(debugValidPixels[0] || debugValidPixels[1] || debugValidPixels[2] || debugValidPixels[3]));
3485#endif
3486
3487 targetRow[0] = *bColor;
3488 targetRow[1] = *bColor;
3489 targetRow[2] = *bColor;
3490 targetRow[3] = *bColor;
3491
3492 targetRow += 4;
3493
3494 continue;
3495 }
3496
3497
3498 // we store the result
3499 vst1q_u32(validPixels, m128_u_validPixel);
3500 ocean_assert(validPixels[0] || validPixels[1] || validPixels[2] || validPixels[3]);
3501
3502
3503 // now we determine the left, top, right and bottom pixel used for the interpolation
3504 // left = floor(x); top = floor(y)
3505 const uint32x4_t m128_u_left = vcvtq_u32_f32(m128_f_sourceX);
3506 const uint32x4_t m128_u_top = vcvtq_u32_f32(m128_f_sourceY);
3507
3508 // right = min(left + 1, width - 1); bottom = min(top + 1; height - 1)
3509 const uint32x4_t m128_u_right = vminq_u32(vaddq_u32(m128_u_left, vdupq_n_u32(1u)), m128_u_sourceWidth_1);
3510 const uint32x4_t m128_u_bottom = vminq_u32(vaddq_u32(m128_u_top, vdupq_n_u32(1u)), m128_u_sourceHeight_1);
3511
3512 // offset = y * stride + x * channels
3513 const uint32x4_t m128_u_topLeftOffsetElements = vmlaq_u32(vmulq_u32(m128_u_left, constantChannels_u_32x4), m128_u_top, m128_u_sourceStrideElements); // topLeftOffset = top * strideElements + left * channels
3514 const uint32x4_t m128_u_topRightOffsetElements = vmlaq_u32(vmulq_u32(m128_u_right, constantChannels_u_32x4), m128_u_top, m128_u_sourceStrideElements); // topRightOffset = top * strideElements + right * channels
3515 const uint32x4_t m128_u_bottomLeftOffsetElements = vmlaq_u32(vmulq_u32(m128_u_left, constantChannels_u_32x4), m128_u_bottom, m128_u_sourceStrideElements); // ...
3516 const uint32x4_t m128_u_bottomRightOffsetElements = vmlaq_u32(vmulq_u32(m128_u_right, constantChannels_u_32x4), m128_u_bottom, m128_u_sourceStrideElements);
3517
3518 // we store the offsets
3519 vst1q_u32(topLeftOffsetsElements, m128_u_topLeftOffsetElements);
3520 vst1q_u32(topRightOffsetsElements, m128_u_topRightOffsetElements);
3521 vst1q_u32(bottomLeftOffsetsElements, m128_u_bottomLeftOffsetElements);
3522 vst1q_u32(bottomRightOffsetsElements, m128_u_bottomRightOffsetElements);
3523
3524
3525 // now we need to determine the interpolation factors tx, tx_ and ty, ty_: (top_left * tx_ + top_right * tx) * ty_ + (bottom_left * tx_ + bottom_right * tx) * ty
3526
3527 // we determine the fractional portions of the x' and y':
3528 float32x4_t m128_f_tx = vsubq_f32(m128_f_sourceX, vcvtq_f32_u32(m128_u_left));
3529 float32x4_t m128_f_ty = vsubq_f32(m128_f_sourceY, vcvtq_f32_u32(m128_u_top));
3530
3531 // we use integer interpolation [0.0, 1.0] -> [0, 128]
3532 m128_f_tx = vmulq_f32(m128_f_tx, vdupq_n_f32(128.0f));
3533 m128_f_ty = vmulq_f32(m128_f_ty, vdupq_n_f32(128.0f));
3534
3535 const uint32x4_t m128_u_tx = vcvtq_u32_f32(vaddq_f32(m128_f_tx, vdupq_n_f32(0.5)));
3536 const uint32x4_t m128_u_ty = vcvtq_u32_f32(vaddq_f32(m128_f_ty, vdupq_n_f32(0.5)));
3537
3538 if constexpr (tChannels > 4u)
3539 {
3540 // normally we would simply call instead of copying the code of the function to this location
3541 // however, if calling the function instead of applying the code here directly
3542 // clang ends with code approx. 20% slower
3543 // thus we make a copy of the code and keep the function for demonstration purposes
3544
3545 //interpolate4Pixels8BitPerChannelNEON<tChannels>(source, topLeftOffsetsElements, topRightOffsetsElements, bottomLeftOffsetsElements, bottomRightOffsetsElements, validPixels, *bColor, m128_u_tx, m128_u_ty, targetPixelData);
3546 //targetPixelData += 4;
3547
3548 const uint32x4_t m128_u_tx_ = vsubq_u32(vdupq_n_u32(128u), m128_u_tx);
3549 const uint32x4_t m128_u_ty_ = vsubq_u32(vdupq_n_u32(128u), m128_u_ty);
3550
3551 // (top_left * tx_ + top_right * tx) * ty_ + (bottom_left * tx_ + bottom_right * tx) * ty
3552 // == top_left * tx_ty_ + top_right * txty_ + bottom_left * tx_ty + bottom_right * txty
3553 const uint32x4_t m128_u_tx_ty_ = vmulq_u32(m128_u_tx_, m128_u_ty_);
3554 const uint32x4_t m128_u_txty_ = vmulq_u32(m128_u_tx, m128_u_ty_);
3555 const uint32x4_t m128_u_tx_ty = vmulq_u32(m128_u_tx_, m128_u_ty);
3556 const uint32x4_t m128_u_txty = vmulq_u32(m128_u_tx, m128_u_ty);
3557
3558 unsigned int tx_ty_s[4];
3559 unsigned int txty_s[4];
3560 unsigned int tx_tys[4];
3561 unsigned int txtys[4];
3562
3563 // we store the interpolation factors
3564 vst1q_u32(tx_ty_s, m128_u_tx_ty_);
3565 vst1q_u32(txty_s, m128_u_txty_);
3566 vst1q_u32(tx_tys, m128_u_tx_ty);
3567 vst1q_u32(txtys, m128_u_txty);
3568
3569 for (unsigned int i = 0u; i < 4u; ++i)
3570 {
3571 if (validPixels[i])
3572 {
3573 ocean_assert(topLeftOffsetsElements[i] < sourceStrideElements * sourceHeight);
3574 ocean_assert(topRightOffsetsElements[i] < sourceStrideElements * sourceHeight);
3575 ocean_assert(bottomLeftOffsetsElements[i] < sourceStrideElements * sourceHeight);
3576 ocean_assert(bottomRightOffsetsElements[i] < sourceStrideElements * sourceHeight);
3577
3578 const uint8_t* topLeft = source + topLeftOffsetsElements[i];
3579 const uint8_t* topRight = source + topRightOffsetsElements[i];
3580
3581 const uint8_t* bottomLeft = source + bottomLeftOffsetsElements[i];
3582 const uint8_t* bottomRight = source + bottomRightOffsetsElements[i];
3583
3584 const unsigned int tx_ty_ = tx_ty_s[i];
3585 const unsigned int txty_ = txty_s[i];
3586 const unsigned int tx_ty = tx_tys[i];
3587 const unsigned int txty = txtys[i];
3588
3589 ocean_assert(tx_ty_ + txty_ + tx_ty + txty == 128u * 128u);
3590
3591 for (unsigned int n = 0u; n < tChannels; ++n)
3592 {
3593 ((uint8_t*)targetRow)[n] = uint8_t((topLeft[n] * tx_ty_ + topRight[n] * txty_ + bottomLeft[n] * tx_ty + bottomRight[n] * txty + 8192u) >> 14u);
3594 }
3595 }
3596 else
3597 {
3598 *targetRow = *bColor;
3599 }
3600
3601 targetRow++;
3602 }
3603 }
3604 else
3605 {
3606 interpolate4Pixels8BitPerChannelNEON<tChannels>(source, topLeftOffsetsElements, topRightOffsetsElements, bottomLeftOffsetsElements, bottomRightOffsetsElements, validPixels, *bColor, m128_u_tx, m128_u_ty, targetRow);
3607 targetRow += 4;
3608 }
3609 }
3610 }
3611}
3612
3613template <unsigned int tChannels>
3614void FrameInterpolatorBilinear::homography8BitPerChannelNEONSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* input_H_output, const uint8_t* borderColor, uint8_t* output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
3615{
3616 static_assert(tChannels >= 1u, "Invalid channel number!");
3617
3618 ocean_assert(input != nullptr && output != nullptr);
3619 ocean_assert(inputWidth > 0u && inputHeight > 0u);
3620 ocean_assert_and_suppress_unused(outputWidth >= 4u && outputHeight > 0u, outputHeight);
3621 ocean_assert(input_H_output != nullptr);
3622
3623 ocean_assert(firstOutputRow + numberOutputRows <= outputHeight);
3624
3625 const unsigned int inputStrideElements = inputWidth * tChannels + inputPaddingElements;
3626 const unsigned int outputStrideElements = outputWidth * tChannels + outputPaddingElements;
3627
3628 using PixelType = typename DataType<uint8_t, tChannels>::Type;
3629
3630 uint8_t zeroColor[tChannels] = {uint8_t(0)};
3631 const PixelType* const bColor = borderColor ? (PixelType*)borderColor : (PixelType*)zeroColor;
3632
3633 unsigned int validPixels[4];
3634
3635 unsigned int topLeftOffsetsElements[4];
3636 unsigned int topRightOffsetsElements[4];
3637 unsigned int bottomLeftOffsetsElements[4];
3638 unsigned int bottomRightOffsetsElements[4];
3639
3640 const uint32x4_t constantChannels_u_32x4 = vdupq_n_u32(tChannels);
3641
3642 // we store 4 floats: [X0, X0, X0, X0], and same with X1 and X2
3643 const float32x4_t m128_f_X0 = vdupq_n_f32(float((*input_H_output)(0, 0)));
3644 const float32x4_t m128_f_X1 = vdupq_n_f32(float((*input_H_output)(1, 0)));
3645 const float32x4_t m128_f_X2 = vdupq_n_f32(float((*input_H_output)(2, 0)));
3646
3647 for (unsigned int y = firstOutputRow; y < firstOutputRow + numberOutputRows; ++y)
3648 {
3649 PixelType* outputPixelData = (PixelType*)(output + y * outputStrideElements);
3650
3651 /*
3652 * We can slightly optimize the 3x3 matrix multiplication:
3653 *
3654 * | X0 Y0 Z0 | | x |
3655 * | X1 Y1 Z1 | * | y |
3656 * | X2 Y2 Z2 | | 1 |
3657 *
3658 * | xx | | X0 * x | | Y0 * y + Z0 |
3659 * | yy | = | X1 * x | + | Y1 * y + Z1 |
3660 * | zz | | X2 * x | | Y2 * y + Z2 |
3661 *
3662 * | xx | | X0 * x | | C0 |
3663 * | yy | = | X1 * x | + | C1 |
3664 * | zz | | X2 * x | | C3 |
3665 *
3666 * As y is constant within the inner loop, we can pre-calculate the following terms:
3667 *
3668 * | x' | | (X0 * x + C0) / (X2 * x + C2) |
3669 * | y' | = | (X1 * x + C1) / (X2 * x + C2) |
3670 */
3671
3672 // we store 4 floats: [C0, C0, C0, C0], and same with C1 and C2
3673 const float32x4_t m128_f_C0 = vdupq_n_f32(float((*input_H_output)(0, 1) * Scalar(y) + (*input_H_output)(0, 2)));
3674 const float32x4_t m128_f_C1 = vdupq_n_f32(float((*input_H_output)(1, 1) * Scalar(y) + (*input_H_output)(1, 2)));
3675 const float32x4_t m128_f_C2 = vdupq_n_f32(float((*input_H_output)(2, 1) * Scalar(y) + (*input_H_output)(2, 2)));
3676
3677 // we store 4 floats: [0.0f, 0.0f, 0.0f, 0.0f]
3678 const float32x4_t m128_f_zero = vdupq_n_f32(0.0f);
3679
3680 // we store 4 integers: [inputStrideElements, inputStrideElements, inputStrideElements, inputStrideElements]
3681 const uint32x4_t m128_u_inputStrideElements = vdupq_n_u32(inputStrideElements);
3682
3683 // we store 4 integers: [inputWidth - 1, inputWidth - 1, inputWidth - 1, inputWidth - 1], and same with inputHeight
3684 const uint32x4_t m128_u_inputWidth_1 = vdupq_n_u32(inputWidth - 1u);
3685 const uint32x4_t m128_u_inputHeight_1 = vdupq_n_u32(inputHeight - 1u);
3686
3687 // we store 4 floats: [inputWidth - 1, inputWidth - 1, inputWidth - 1, inputWidth - 1], and same with inputHeight
3688 const float32x4_t m128_f_inputWidth_1 = vdupq_n_f32(float(inputWidth - 1u));
3689 const float32x4_t m128_f_inputHeight_1 = vdupq_n_f32(float(inputHeight - 1u));
3690
3691 for (unsigned int x = 0u; x < outputWidth; x += 4u)
3692 {
3693 if (x + 4u > outputWidth)
3694 {
3695 // the last iteration will not fit into the output frame,
3696 // so we simply shift x left by some pixels (at most 3) and we will calculate some pixels again
3697
3698 ocean_assert(x >= 4u && outputWidth > 4u);
3699 const unsigned int newX = outputWidth - 4u;
3700
3701 ocean_assert(x > newX);
3702 outputPixelData -= x - newX;
3703
3704 x = newX;
3705
3706 // the for loop will stop after this iteration
3707 ocean_assert(!(x + 4u < outputWidth));
3708 }
3709
3710
3711 // we need four successive x coordinate floats:
3712 // [x + 3.0f, x + 2.0f, x + 1.0f; x + 0.0f]
3713 float x_0123[4] = {float(x + 0u), float(x + 1u), float(x + 2u), float(x + 3u)};
3714 const float32x4_t m128_f_x_0123 = vld1q_f32(x_0123);
3715
3716 // we calculate xx and yy and zz for [x + 3.0f, x + 2.0f, x + 1.0f, x + 0.0f]
3717 const float32x4_t m128_f_xx = vmlaq_f32(m128_f_C0, m128_f_X0, m128_f_x_0123);
3718 const float32x4_t m128_f_yy = vmlaq_f32(m128_f_C1, m128_f_X1, m128_f_x_0123);
3719 const float32x4_t m128_f_zz = vmlaq_f32(m128_f_C2, m128_f_X2, m128_f_x_0123);
3720
3721#ifdef USE_DIVISION_ARM64_ARCHITECTURE
3722
3723 // using the division available from ARM64 is more precise
3724 const float32x4_t m128_f_inputX = vdivq_f32(m128_f_xx, m128_f_zz);
3725 const float32x4_t m128_f_inputY = vdivq_f32(m128_f_yy, m128_f_zz);
3726
3727#else
3728
3729 // we calculate the (approximated) inverse of zz
3730 // [1/zz3, 1/zz2, 1/zz1, 1/zz0]
3731 float32x4_t inv_zz_128 = vrecpeq_f32(m128_f_zz);
3732 inv_zz_128 = vmulq_f32(vrecpsq_f32(m128_f_zz, inv_zz_128), inv_zz_128); // improving the accuracy of the approx. inverse by Newton/Raphson
3733
3734 // we determine the normalized coordinates x' and y' for for x + 3.0f, x + 2.0f, ...)
3735 const float32x4_t m128_f_inputX = vmulq_f32(m128_f_xx, inv_zz_128);
3736 const float32x4_t m128_f_inputY = vmulq_f32(m128_f_yy, inv_zz_128);
3737
3738#endif // USE_DIVISION_ARM64_ARCHITECTURE
3739
3740
3741 // now we check whether we are inside the input frame
3742 const uint32x4_t m128_u_validPixelX = vandq_u32(vcleq_f32(m128_f_inputX, m128_f_inputWidth_1), vcgeq_f32(m128_f_inputX, m128_f_zero)); // inputPosition.x() >= 0 && inputPosition.x() <= (inputWidth - 1) ? 0xFFFFFF : 0x000000
3743 const uint32x4_t m128_u_validPixelY = vandq_u32(vcleq_f32(m128_f_inputY, m128_f_inputHeight_1), vcgeq_f32(m128_f_inputY, m128_f_zero)); // inputPosition.y() >= 0 && inputPosition.y() <= (inputHeight - 1) ? 0xFFFFFF : 0x000000
3744
3745 const uint32x4_t m128_u_validPixel = vandq_u32(m128_u_validPixelX, m128_u_validPixelY); // is_inside_input_frame(inputPosition) ? 0xFFFFFF : 0x000000
3746
3747
3748 // we can stop here if all pixels are invalid
3749 const uint32x2_t m64_u_validPixel = vorr_u32(vget_low_u32(m128_u_validPixel), vget_high_u32(m128_u_validPixel));
3750 if ((vget_lane_u32(m64_u_validPixel, 0) | vget_lane_u32(m64_u_validPixel, 1)) == 0x00000000u)
3751 {
3752#ifdef OCEAN_DEBUG
3753 OCEAN_ALIGN_DATA(16) unsigned int debugValidPixels[4];
3754 vst1q_u32(debugValidPixels, m128_u_validPixel);
3755 ocean_assert(!(debugValidPixels[0] || debugValidPixels[1] || debugValidPixels[2] || debugValidPixels[3]));
3756#endif
3757
3758 outputPixelData[0] = *bColor;
3759 outputPixelData[1] = *bColor;
3760 outputPixelData[2] = *bColor;
3761 outputPixelData[3] = *bColor;
3762
3763 outputPixelData += 4;
3764
3765 continue;
3766 }
3767
3768
3769 // we store the result
3770 vst1q_u32(validPixels, m128_u_validPixel);
3771 ocean_assert(validPixels[0] || validPixels[1] || validPixels[2] || validPixels[3]);
3772
3773
3774 // now we determine the left, top, right and bottom pixel used for the interpolation
3775 // left = floor(x); top = floor(y)
3776 const uint32x4_t m128_u_left = vcvtq_u32_f32(m128_f_inputX);
3777 const uint32x4_t m128_u_top = vcvtq_u32_f32(m128_f_inputY);
3778
3779 // right = min(left + 1, width - 1); bottom = min(top + 1; height - 1)
3780 const uint32x4_t m128_u_right = vminq_u32(vaddq_u32(m128_u_left, vdupq_n_u32(1u)), m128_u_inputWidth_1);
3781 const uint32x4_t m128_u_bottom = vminq_u32(vaddq_u32(m128_u_top, vdupq_n_u32(1u)), m128_u_inputHeight_1);
3782
3783 // offset = y * stride + x * channels
3784 const uint32x4_t m128_u_topLeftOffsetElements = vmlaq_u32(vmulq_u32(m128_u_left, constantChannels_u_32x4), m128_u_top, m128_u_inputStrideElements); // topLeftOffset = top * strideElements + left * channels
3785 const uint32x4_t m128_u_topRightOffsetElements = vmlaq_u32(vmulq_u32(m128_u_right, constantChannels_u_32x4), m128_u_top, m128_u_inputStrideElements); // topRightOffset = top * strideElements + right * channels
3786 const uint32x4_t m128_u_bottomLeftOffsetElements = vmlaq_u32(vmulq_u32(m128_u_left, constantChannels_u_32x4), m128_u_bottom, m128_u_inputStrideElements); // ...
3787 const uint32x4_t m128_u_bottomRightOffsetElements = vmlaq_u32(vmulq_u32(m128_u_right, constantChannels_u_32x4), m128_u_bottom, m128_u_inputStrideElements);
3788
3789 // we store the offsets
3790 vst1q_u32(topLeftOffsetsElements, m128_u_topLeftOffsetElements);
3791 vst1q_u32(topRightOffsetsElements, m128_u_topRightOffsetElements);
3792 vst1q_u32(bottomLeftOffsetsElements, m128_u_bottomLeftOffsetElements);
3793 vst1q_u32(bottomRightOffsetsElements, m128_u_bottomRightOffsetElements);
3794
3795
3796 // now we need to determine the interpolation factors tx, tx_ and ty, ty_: (top_left * tx_ + top_right * tx) * ty_ + (bottom_left * tx_ + bottom_right * tx) * ty
3797
3798 // we determine the fractional portions of the x' and y':
3799 float32x4_t m128_f_tx = vsubq_f32(m128_f_inputX, vcvtq_f32_u32(m128_u_left));
3800 float32x4_t m128_f_ty = vsubq_f32(m128_f_inputY, vcvtq_f32_u32(m128_u_top));
3801
3802 // we use integer interpolation [0.0, 1.0] -> [0, 128]
3803 m128_f_tx = vmulq_f32(m128_f_tx, vdupq_n_f32(128.0f));
3804 m128_f_ty = vmulq_f32(m128_f_ty, vdupq_n_f32(128.0f));
3805
3806 const uint32x4_t m128_u_tx = vcvtq_u32_f32(vaddq_f32(m128_f_tx, vdupq_n_f32(0.5)));
3807 const uint32x4_t m128_u_ty = vcvtq_u32_f32(vaddq_f32(m128_f_ty, vdupq_n_f32(0.5)));
3808
3809 if constexpr (tChannels > 4u)
3810 {
3811 // normally we would simply call instead of copying the code of the function to this location
3812 // however, if calling the function instead of applying the code here directly
3813 // clang ends with code approx. 20% slower
3814 // thus we make a copy of the code and keep the function for demonstration purposes
3815
3816 //interpolate4Pixels8BitPerChannelNEON<tChannels>(input, topLeftOffsetsElements, topRightOffsetsElements, bottomLeftOffsetsElements, bottomRightOffsetsElements, validPixels, *bColor, m128_u_tx, m128_u_ty, outputPixelData);
3817 //outputPixelData += 4;
3818
3819 const uint32x4_t m128_u_tx_ = vsubq_u32(vdupq_n_u32(128u), m128_u_tx);
3820 const uint32x4_t m128_u_ty_ = vsubq_u32(vdupq_n_u32(128u), m128_u_ty);
3821
3822 // (top_left * tx_ + top_right * tx) * ty_ + (bottom_left * tx_ + bottom_right * tx) * ty
3823 // == top_left * tx_ty_ + top_right * txty_ + bottom_left * tx_ty + bottom_right * txty
3824 const uint32x4_t m128_u_tx_ty_ = vmulq_u32(m128_u_tx_, m128_u_ty_);
3825 const uint32x4_t m128_u_txty_ = vmulq_u32(m128_u_tx, m128_u_ty_);
3826 const uint32x4_t m128_u_tx_ty = vmulq_u32(m128_u_tx_, m128_u_ty);
3827 const uint32x4_t m128_u_txty = vmulq_u32(m128_u_tx, m128_u_ty);
3828
3829 unsigned int tx_ty_s[4];
3830 unsigned int txty_s[4];
3831 unsigned int tx_tys[4];
3832 unsigned int txtys[4];
3833
3834 // we store the interpolation factors
3835 vst1q_u32(tx_ty_s, m128_u_tx_ty_);
3836 vst1q_u32(txty_s, m128_u_txty_);
3837 vst1q_u32(tx_tys, m128_u_tx_ty);
3838 vst1q_u32(txtys, m128_u_txty);
3839
3840 for (unsigned int i = 0u; i < 4u; ++i)
3841 {
3842 if (validPixels[i])
3843 {
3844 ocean_assert(topLeftOffsetsElements[i] < inputStrideElements * inputHeight);
3845 ocean_assert(topRightOffsetsElements[i] < inputStrideElements * inputHeight);
3846 ocean_assert(bottomLeftOffsetsElements[i] < inputStrideElements * inputHeight);
3847 ocean_assert(bottomRightOffsetsElements[i] < inputStrideElements * inputHeight);
3848
3849 const uint8_t* topLeft = input + topLeftOffsetsElements[i];
3850 const uint8_t* topRight = input + topRightOffsetsElements[i];
3851
3852 const uint8_t* bottomLeft = input + bottomLeftOffsetsElements[i];
3853 const uint8_t* bottomRight = input + bottomRightOffsetsElements[i];
3854
3855 const unsigned int tx_ty_ = tx_ty_s[i];
3856 const unsigned int txty_ = txty_s[i];
3857 const unsigned int tx_ty = tx_tys[i];
3858 const unsigned int txty = txtys[i];
3859
3860 ocean_assert(tx_ty_ + txty_ + tx_ty + txty == 128u * 128u);
3861
3862 for (unsigned int n = 0u; n < tChannels; ++n)
3863 {
3864 ((uint8_t*)outputPixelData)[n] = uint8_t((topLeft[n] * tx_ty_ + topRight[n] * txty_ + bottomLeft[n] * tx_ty + bottomRight[n] * txty + 8192u) >> 14u);
3865 }
3866 }
3867 else
3868 {
3869 *outputPixelData = *bColor;
3870 }
3871
3872 outputPixelData++;
3873 }
3874 }
3875 else
3876 {
3877 interpolate4Pixels8BitPerChannelNEON<tChannels>(input, topLeftOffsetsElements, topRightOffsetsElements, bottomLeftOffsetsElements, bottomRightOffsetsElements, validPixels, *bColor, m128_u_tx, m128_u_ty, outputPixelData);
3878 outputPixelData += 4;
3879 }
3880 }
3881 }
3882}
3883
3884template <>
3885OCEAN_FORCE_INLINE void FrameInterpolatorBilinear::interpolate4Pixels8BitPerChannelNEON<1u>(const uint8_t* source, const unsigned int offsetsTopLeftElements[4], const unsigned int offsetsTopRightElements[4], const unsigned int offsetsBottomLeftElements[4], const unsigned int offsetsBottomRightElements[4], const unsigned int validPixels[4], const DataType<uint8_t, 1u>::Type& borderColor, const uint32x4_t& m128_factorsRight, const uint32x4_t& m128_factorsBottom, typename DataType<uint8_t, 1u>::Type* targetPositionPixels)
3886{
3887 ocean_assert(source != nullptr);
3888 ocean_assert(targetPositionPixels != nullptr);
3889
3890 // as we do not initialize the following intermediate data,
3891 // we hopefully will not allocate memory on the stack each time this function is called
3892 DataType<uint8_t, 1u>::Type pixels[16];
3893
3894 // we will store the pixel information in the following pattern:
3895 // F E D C B A 9 8 7 6 5 4 3 2 1 0
3896 // BR3 BL3 TR3 TL3 BR2 BL2 TR2 TL2 BR1 BL1 TR1 TL1 BR0 BL0 TR0 TL0
3897
3898 // we gather the individual source pixel values from the source image,
3899 // based on the calculated pixel locations
3900 for (unsigned int i = 0u; i < 4u; ++i)
3901 {
3902 if (validPixels[i])
3903 {
3904 pixels[i * 4u + 0u] = *((const DataType<uint8_t, 1u>::Type*)(source + offsetsTopLeftElements[i]));
3905 pixels[i * 4u + 1u] = *((const DataType<uint8_t, 1u>::Type*)(source + offsetsTopRightElements[i]));
3906 pixels[i * 4u + 2u] = *((const DataType<uint8_t, 1u>::Type*)(source + offsetsBottomLeftElements[i]));
3907 pixels[i * 4u + 3u] = *((const DataType<uint8_t, 1u>::Type*)(source + offsetsBottomRightElements[i]));
3908 }
3909 else
3910 {
3911 pixels[i * 4u + 0u] = borderColor;
3912 pixels[i * 4u + 1u] = borderColor;
3913 pixels[i * 4u + 2u] = borderColor;
3914 pixels[i * 4u + 3u] = borderColor;
3915 }
3916 }
3917
3918 static_assert(sizeof(uint8x16_t) == sizeof(pixels), "Invalid data type!");
3919
3920 const uint8x16_t m128_pixels = vld1q_u8((const uint8_t*)pixels);
3921
3922
3923 // factorLeft = 128 - factorRight
3924 // factorTop = 128 - factorBottom
3925
3926 const uint32x4_t m128_factorsLeft = vsubq_u32(vdupq_n_u32(128u), m128_factorsRight);
3927 const uint32x4_t m128_factorsTop = vsubq_u32(vdupq_n_u32(128u), m128_factorsBottom);
3928
3929 // (top_left * factorLeft + top_right * factorRight) * factorTop + (bottom_left * factorLeft + bottom_right * factorRight) * factorBottom
3930 // == top_left * factorTopLeft + top_right * factorTopRight + bottom_left * factorBottomLeft + bottom_right * factorBottomRight
3931
3932 const uint32x4_t m128_factorsTopLeft = vmulq_u32(m128_factorsTop, m128_factorsLeft);
3933 const uint32x4_t m128_factorsTopRight = vmulq_u32(m128_factorsTop, m128_factorsRight);
3934 const uint32x4_t m128_factorsBottomLeft = vmulq_u32(m128_factorsBottom, m128_factorsLeft);
3935 const uint32x4_t m128_factorsBottomRight = vmulq_u32(m128_factorsBottom, m128_factorsRight);
3936
3937 // pixels stores the four interpolation grascale pixel values (top left, top right, bottom left, bottom right) for 4 (independent) pixels:
3938 // F E D C B A 9 8 7 6 5 4 3 2 1 0
3939 // BR BL TR TL BR BL TR TL BR BL TR TL BR BL TR TL
3940
3941 // factorsTopLeft stores the 32 bit interpolation values for 4 pixels:
3942 // FEDC BA98 7654 3210
3943 // 3 2 1 0 (32 bit interpolation values, fitting into 16 bit)
3944
3945
3946 // we will simply extract each channel from the source pixels,
3947 // each extracted channel will be multiplied by the corresponding interpolation factor
3948 // and all interpolation results will be accumulated afterwards
3949
3950 const uint32x4_t m128_maskFirstByte = vdupq_n_u32(0x000000FFu);
3951
3952 const uint32x4_t m128_muliplicationA = vmulq_u32(vandq_u32(vreinterpretq_u32_u8(m128_pixels), m128_maskFirstByte), m128_factorsTopLeft);
3953 const uint32x4_t m128_muliplicationB = vmulq_u32(vandq_u32(vshrq_n_u32(vreinterpretq_u32_u8(m128_pixels), 8), m128_maskFirstByte), m128_factorsTopRight);
3954 const uint32x4_t m128_muliplicationC = vmulq_u32(vandq_u32(vshrq_n_u32(vreinterpretq_u32_u8(m128_pixels), 16), m128_maskFirstByte), m128_factorsBottomLeft);
3955 const uint32x4_t m128_muliplicationD = vmulq_u32(vandq_u32(vshrq_n_u32(vreinterpretq_u32_u8(m128_pixels), 24), m128_maskFirstByte), m128_factorsBottomRight);
3956
3957 const uint32x4_t m128_multiplication = vaddq_u32(vaddq_u32(m128_muliplicationA, m128_muliplicationB), vaddq_u32(m128_muliplicationC, m128_muliplicationD));
3958
3959 // we add 8192 and shift by 14 bits
3960
3961 const uint8x16_t m128_interpolation = vreinterpretq_u8_u32(vshrq_n_u32(vaddq_u32(m128_multiplication, vdupq_n_u32(8192u)), 14));
3962
3963 // finally we have the following result:
3964 // ---C ---8 ---4 ---0
3965 // and we need to extract the four pixel values:
3966 //
3967 // NOTE: Because of a possible bug in Clang affecting ARMv7, vget_lane_u32()
3968 // seems to assume 32-bit memory alignment for output location, which cannot
3969 // be guaranteed. This results in bus errors and crashes the application.
3970 // ARM64 is not affected.
3971#if defined(__aarch64__)
3972
3973 constexpr uint8x8_t m64_mask0 = NEON::create_uint8x8(0, 4, 1, 1, 1, 1, 1, 1);
3974 constexpr uint8x8_t m64_mask1 = NEON::create_uint8x8(1, 1, 0, 4, 1, 1, 1, 1);
3975
3976 const uint8x8_t m64_interpolation01 = vtbl1_u8(vget_low_u8(m128_interpolation), m64_mask0);
3977 const uint8x8_t m64_interpolation23 = vtbl1_u8(vget_high_u8(m128_interpolation), m64_mask1);
3978
3979 const uint8x8_t m64_interpolation0123 = vorr_u8(m64_interpolation01, m64_interpolation23);
3980
3981 const uint32_t result = vget_lane_u32(vreinterpret_u32_u8(m64_interpolation0123), 0);
3982 memcpy(targetPositionPixels, &result, sizeof(uint32_t));
3983
3984#else
3985
3986 *((uint8_t*)targetPositionPixels + 0) = vgetq_lane_u8(m128_interpolation, 0);
3987 *((uint8_t*)targetPositionPixels + 1) = vgetq_lane_u8(m128_interpolation, 4);
3988 *((uint8_t*)targetPositionPixels + 2) = vgetq_lane_u8(m128_interpolation, 8);
3989 *((uint8_t*)targetPositionPixels + 3) = vgetq_lane_u8(m128_interpolation, 12);
3990
3991#endif
3992}
3993
3994OCEAN_FORCE_INLINE void FrameInterpolatorBilinear::interpolate8Pixels1Channel8BitNEON(const uint8x8_t& topLeft_u_8x8, const uint8x8_t& topRight_u_8x8, const uint8x8_t& bottomLeft_u_8x8, const uint8x8_t& bottomRight_u_8x8, const uint8x16_t& factorsRight_factorsBottom_128_u_8x16, uint8_t* targetPositionPixels)
3995{
3996 const uint8x16_t factorsLeft_factorsTop_128_u_8x16 = vsubq_u8(vdupq_n_u8(128u), factorsRight_factorsBottom_128_u_8x16); // factorLeft = 128 - factorRight, factorTop = 128 - factorBottomv
3997
3998 const uint8x8_t factorsRight_u_8x8 = vget_low_u8(factorsRight_factorsBottom_128_u_8x16);
3999 const uint16x8_t factorsBottom_u_16x8 = vmovl_u8(vget_high_u8(factorsRight_factorsBottom_128_u_8x16));
4000
4001 const uint8x8_t factorsLeft_u_8x8 = vget_low_u8(factorsLeft_factorsTop_128_u_8x16);
4002 const uint16x8_t factorsTop_u_16x8 = vmovl_u8(vget_high_u8(factorsLeft_factorsTop_128_u_8x16));
4003
4004 const uint16x8_t intermediateTop_u_16x8 = vmlal_u8(vmull_u8(topLeft_u_8x8, factorsLeft_u_8x8), topRight_u_8x8, factorsRight_u_8x8); // intermediateTop = topLeft * factorLeft + topRight * factorRight
4005 const uint16x8_t intermediateBottom_u_16x8 = vmlal_u8(vmull_u8(bottomLeft_u_8x8, factorsLeft_u_8x8), bottomRight_u_8x8, factorsRight_u_8x8); // intermediateBottom = bottomLeft * factorLeft + bottomRight * factorRight
4006
4007 const uint32x4_t resultA_32x4 = vmlal_u16(vmull_u16(vget_low_u16(intermediateTop_u_16x8), vget_low_u16(factorsTop_u_16x8)), vget_low_u16(intermediateBottom_u_16x8), vget_low_u16(factorsBottom_u_16x8)); // result = intermediateTop * factorTop + intermediateBottom + factorBottom
4008 const uint32x4_t resultB_32x4 = vmlal_u16(vmull_u16(vget_high_u16(intermediateTop_u_16x8), vget_high_u16(factorsTop_u_16x8)), vget_high_u16(intermediateBottom_u_16x8), vget_high_u16(factorsBottom_u_16x8));
4009
4010 const uint16x8_t result_16x8 = vcombine_u16(vrshrn_n_u32(resultA_32x4, 14), vrshrn_n_u32(resultB_32x4, 14)); // round(result / 16384.0)
4011
4012 const uint8x8_t result_8x8 = vmovn_u16(result_16x8);
4013
4014 vst1_u8(targetPositionPixels, result_8x8);
4015}
4016
4017template <>
4018OCEAN_FORCE_INLINE void FrameInterpolatorBilinear::interpolate4Pixels8BitPerChannelNEON<2u>(const uint8_t* source, const unsigned int offsetsTopLeftElements[4], const unsigned int offsetsTopRightElements[4], const unsigned int offsetsBottomLeftElements[4], const unsigned int offsetsBottomRightElements[4], const unsigned int validPixels[4], const DataType<uint8_t, 2u>::Type& borderColor, const uint32x4_t& m128_factorsRight, const uint32x4_t& m128_factorsBottom, typename DataType<uint8_t, 2u>::Type* targetPositionPixels)
4019{
4020 ocean_assert(source != nullptr);
4021 ocean_assert(targetPositionPixels != nullptr);
4022
4023 using PixelType = typename DataType<uint8_t, 2u>::Type;
4024
4025 // as we do not initialize the following intermediate data,
4026 // we hopefully will not allocate memory on the stack each time this function is called
4027 PixelType topPixels[8];
4028 PixelType bottomPixels[8];
4029
4030 // we will store the pixel information in the following pattern (here for YA):
4031 // FE DC BA 98 76 54 32 10
4032 // YA YA YA YA YA YA YA YA
4033 // TR TL TR TL TR TL TR TL
4034
4035 // we gather the individual source pixel values from the source image,
4036 // based on the calculated pixel locations
4037 for (unsigned int i = 0u; i < 4u; ++i)
4038 {
4039 if (validPixels[i])
4040 {
4041 *(topPixels + i * 2u + 0u) = *((const PixelType*)(source + offsetsTopLeftElements[i]));
4042 *(topPixels + i * 2u + 1u) = *((const PixelType*)(source + offsetsTopRightElements[i]));
4043 *(bottomPixels + i * 2u + 0u) = *((const PixelType*)(source + offsetsBottomLeftElements[i]));
4044 *(bottomPixels + i * 2u + 1u) = *((const PixelType*)(source + offsetsBottomRightElements[i]));
4045 }
4046 else
4047 {
4048 *(topPixels + i * 2u + 0u) = borderColor;
4049 *(topPixels + i * 2u + 1u) = borderColor;
4050 *(bottomPixels + i * 2u + 0u) = borderColor;
4051 *(bottomPixels + i * 2u + 1u) = borderColor;
4052 }
4053 }
4054
4055 static_assert(sizeof(uint32x4_t) == sizeof(topPixels), "Invalid data type!");
4056
4057 const uint32x4_t m128_topPixels = vreinterpretq_u32_u8(vld1q_u8((const uint8_t*)topPixels));
4058 const uint32x4_t m128_bottomPixels = vreinterpretq_u32_u8(vld1q_u8((const uint8_t*)bottomPixels));
4059
4060
4061 // factorLeft = 128 - factorRight
4062 // factorTop = 128 - factorBottom
4063
4064 const uint32x4_t m128_factorsLeft = vsubq_u32(vdupq_n_u32(128u), m128_factorsRight);
4065 const uint32x4_t m128_factorsTop = vsubq_u32(vdupq_n_u32(128u), m128_factorsBottom);
4066
4067 // (top_left * factorLeft + top_right * factorRight) * factorTop + (bottom_left * factorLeft + bottom_right * factorRight) * factorBottom
4068 // == top_left * factorTopLeft + top_right * factorTopRight + bottom_left * factorBottomLeft + bottom_right * factorBottomRight
4069
4070 const uint32x4_t m128_factorsTopLeft = vmulq_u32(m128_factorsTop, m128_factorsLeft);
4071 const uint32x4_t m128_factorsTopRight = vmulq_u32(m128_factorsTop, m128_factorsRight);
4072 const uint32x4_t m128_factorsBottomLeft = vmulq_u32(m128_factorsBottom, m128_factorsLeft);
4073 const uint32x4_t m128_factorsBottomRight = vmulq_u32(m128_factorsBottom, m128_factorsRight);
4074
4075
4076 const uint32x4_t m128_maskFirstByte = vdupq_n_u32(0x000000FFu);
4077
4078 uint32x4_t m128_muliplicationChannel0 = vmulq_u32(vandq_u32(m128_topPixels, m128_maskFirstByte), m128_factorsTopLeft);
4079 uint32x4_t m128_muliplicationChannel1 = vmulq_u32(vandq_u32(vshrq_n_u32(m128_topPixels, 8), m128_maskFirstByte), m128_factorsTopLeft);
4080
4081 m128_muliplicationChannel0 = vaddq_u32(m128_muliplicationChannel0, vmulq_u32(vandq_u32(vshrq_n_u32(m128_topPixels, 16), m128_maskFirstByte), m128_factorsTopRight));
4082 m128_muliplicationChannel1 = vaddq_u32(m128_muliplicationChannel1, vmulq_u32(vandq_u32(vshrq_n_u32(m128_topPixels, 24), m128_maskFirstByte), m128_factorsTopRight));
4083
4084 m128_muliplicationChannel0 = vaddq_u32(m128_muliplicationChannel0, vmulq_u32(vandq_u32(m128_bottomPixels, m128_maskFirstByte), m128_factorsBottomLeft));
4085 m128_muliplicationChannel1 = vaddq_u32(m128_muliplicationChannel1, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomPixels, 8), m128_maskFirstByte), m128_factorsBottomLeft));
4086
4087 m128_muliplicationChannel0 = vaddq_u32(m128_muliplicationChannel0, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomPixels, 16), m128_maskFirstByte), m128_factorsBottomRight));
4088 m128_muliplicationChannel1 = vaddq_u32(m128_muliplicationChannel1, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomPixels, 24), m128_maskFirstByte), m128_factorsBottomRight));
4089
4090
4091 // we add 8192 and shift by 14 bits
4092
4093 const uint32x4_t m128_interpolation0 = vshrq_n_u32(vaddq_u32(m128_muliplicationChannel0, vdupq_n_u32(8192u)), 14);
4094 const uint32x4_t m128_interpolation1 = vshrq_n_u32(vaddq_u32(m128_muliplicationChannel1, vdupq_n_u32(8192u)), 14);
4095
4096 // finaly we blend the interpolation results together to get the following pattern:
4097 // FE DC BA 98 76 54 32 10
4098 // 00 YA 00 YA 00 YA 00 YA
4099
4100 const uint32x4_t m128_interpolation = vorrq_u32(m128_interpolation0, vshlq_n_u32(m128_interpolation1, 8));
4101
4102 // we shuffle the 128 bit register to a 64 bit register:
4103
4104 const uint8x8_t m64_mask0 = NEON::create_uint8x8(0, 1, 4, 5, 2, 2, 2, 2);
4105 const uint8x8_t m64_mask1 = NEON::create_uint8x8(2, 2, 2, 2, 0, 1, 4, 5);
4106
4107 const uint8x8_t m64_interpolation_low = vtbl1_u8(vget_low_u8(vreinterpretq_u8_u32(m128_interpolation)), m64_mask0);
4108 const uint8x8_t m64_interpolation_high = vtbl1_u8(vget_high_u8(vreinterpretq_u8_u32(m128_interpolation)), m64_mask1);
4109
4110 const uint8x8_t m64_interpolation = vorr_u8(m64_interpolation_low, m64_interpolation_high);
4111
4112 // no we can store the following pattern as one block:
4113
4114 // 76 54 32 10
4115 // YA YA YA YA
4116
4117 vst1_u8((uint8_t*)targetPositionPixels, m64_interpolation);
4118}
4119
4120template <>
4121OCEAN_FORCE_INLINE void FrameInterpolatorBilinear::interpolate4Pixels8BitPerChannelNEON<3u>(const uint8_t* source, const unsigned int offsetsTopLeftElements[4], const unsigned int offsetsTopRightElements[4], const unsigned int offsetsBottomLeftElements[4], const unsigned int offsetsBottomRightElements[4], const unsigned int validPixels[4], const DataType<uint8_t, 3u>::Type& borderColor, const uint32x4_t& m128_factorsRight, const uint32x4_t& m128_factorsBottom, typename DataType<uint8_t, 3u>::Type* targetPositionPixels)
4122{
4123 ocean_assert(source != nullptr);
4124 ocean_assert(targetPositionPixels != nullptr);
4125
4126 // as we do not initialize the following intermediate data,
4127 // we hopefully will not allocate memory on the stack each time this function is called
4128 uint32_t topLeftPixels[4];
4129 uint32_t topRightPixels[4];
4130 uint32_t bottomLeftPixels[4];
4131 uint32_t bottomRightPixels[4];
4132
4133 // we will store the pixel information in the following pattern, note the padding byte after each pixel (here for RGB):
4134 // FEDCBA9876543210
4135 // BGR BGR BGR BGR
4136
4137 // we gather the individual source pixel values from the source image,
4138 // based on the calculated pixel locations
4139 for (unsigned int i = 0u; i < 4u; ++i)
4140 {
4141 if (validPixels[i])
4142 {
4143 memcpy(topLeftPixels + i, source + offsetsTopLeftElements[i], sizeof(uint8_t) * 3);
4144 memcpy(topRightPixels + i, source + offsetsTopRightElements[i], sizeof(uint8_t) * 3);
4145 memcpy(bottomLeftPixels + i, source + offsetsBottomLeftElements[i], sizeof(uint8_t) * 3);
4146 memcpy(bottomRightPixels + i, source + offsetsBottomRightElements[i], sizeof(uint8_t) * 3);
4147 }
4148 else
4149 {
4150 memcpy(topLeftPixels + i, &borderColor, sizeof(uint8_t) * 3);
4151 memcpy(topRightPixels + i, &borderColor, sizeof(uint8_t) * 3);
4152 memcpy(bottomLeftPixels + i, &borderColor, sizeof(uint8_t) * 3);
4153 memcpy(bottomRightPixels + i, &borderColor, sizeof(uint8_t) * 3);
4154 }
4155 }
4156
4157 static_assert(sizeof(uint32x4_t) == sizeof(topLeftPixels), "Invalid data type!");
4158
4159 const uint32x4_t m128_topLeftPixels = vld1q_u32(topLeftPixels);
4160 const uint32x4_t m128_topRightPixels = vld1q_u32(topRightPixels);
4161 const uint32x4_t m128_bottomLeftPixels = vld1q_u32(bottomLeftPixels);
4162 const uint32x4_t m128_bottomRightPixels = vld1q_u32(bottomRightPixels);
4163
4164
4165 // factorLeft = 128 - factorRight
4166 // factorTop = 128 - factorBottom
4167
4168 const uint32x4_t m128_factorsLeft = vsubq_u32(vdupq_n_u32(128u), m128_factorsRight);
4169 const uint32x4_t m128_factorsTop = vsubq_u32(vdupq_n_u32(128u), m128_factorsBottom);
4170
4171 // (top_left * factorLeft + top_right * factorRight) * factorTop + (bottom_left * factorLeft + bottom_right * factorRight) * factorBottom
4172 // == top_left * factorTopLeft + top_right * factorTopRight + bottom_left * factorBottomLeft + bottom_right * factorBottomRight
4173
4174 const uint32x4_t m128_factorsTopLeft = vmulq_u32(m128_factorsTop, m128_factorsLeft);
4175 const uint32x4_t m128_factorsTopRight = vmulq_u32(m128_factorsTop, m128_factorsRight);
4176 const uint32x4_t m128_factorsBottomLeft = vmulq_u32(m128_factorsBottom, m128_factorsLeft);
4177 const uint32x4_t m128_factorsBottomRight = vmulq_u32(m128_factorsBottom, m128_factorsRight);
4178
4179
4180 const uint32x4_t m128_maskFirstByte = vdupq_n_u32(0x000000FFu);
4181
4182 uint32x4_t m128_muliplicationChannel0 = vmulq_u32(vandq_u32(m128_topLeftPixels, m128_maskFirstByte), m128_factorsTopLeft);
4183 uint32x4_t m128_muliplicationChannel1 = vmulq_u32(vandq_u32(vshrq_n_u32(m128_topLeftPixels, 8), m128_maskFirstByte), m128_factorsTopLeft);
4184 uint32x4_t m128_muliplicationChannel2 = vmulq_u32(vandq_u32(vshrq_n_u32(m128_topLeftPixels, 16), m128_maskFirstByte), m128_factorsTopLeft);
4185
4186 m128_muliplicationChannel0 = vaddq_u32(m128_muliplicationChannel0, vmulq_u32(vandq_u32(m128_topRightPixels, m128_maskFirstByte), m128_factorsTopRight));
4187 m128_muliplicationChannel1 = vaddq_u32(m128_muliplicationChannel1, vmulq_u32(vandq_u32(vshrq_n_u32(m128_topRightPixels, 8), m128_maskFirstByte), m128_factorsTopRight));
4188 m128_muliplicationChannel2 = vaddq_u32(m128_muliplicationChannel2, vmulq_u32(vandq_u32(vshrq_n_u32(m128_topRightPixels, 16), m128_maskFirstByte), m128_factorsTopRight));
4189
4190 m128_muliplicationChannel0 = vaddq_u32(m128_muliplicationChannel0, vmulq_u32(vandq_u32(m128_bottomLeftPixels, m128_maskFirstByte), m128_factorsBottomLeft));
4191 m128_muliplicationChannel1 = vaddq_u32(m128_muliplicationChannel1, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomLeftPixels, 8), m128_maskFirstByte), m128_factorsBottomLeft));
4192 m128_muliplicationChannel2 = vaddq_u32(m128_muliplicationChannel2, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomLeftPixels, 16), m128_maskFirstByte), m128_factorsBottomLeft));
4193
4194 m128_muliplicationChannel0 = vaddq_u32(m128_muliplicationChannel0, vmulq_u32(vandq_u32(m128_bottomRightPixels, m128_maskFirstByte), m128_factorsBottomRight));
4195 m128_muliplicationChannel1 = vaddq_u32(m128_muliplicationChannel1, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomRightPixels, 8), m128_maskFirstByte), m128_factorsBottomRight));
4196 m128_muliplicationChannel2 = vaddq_u32(m128_muliplicationChannel2, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomRightPixels, 16), m128_maskFirstByte), m128_factorsBottomRight));
4197
4198
4199 // we add 8192 and shift by 14 bits
4200
4201 const uint32x4_t m128_interpolation0 = vshrq_n_u32(vaddq_u32(m128_muliplicationChannel0, vdupq_n_u32(8192u)), 14);
4202 const uint32x4_t m128_interpolation1 = vshrq_n_u32(vaddq_u32(m128_muliplicationChannel1, vdupq_n_u32(8192u)), 14);
4203 const uint32x4_t m128_interpolation2 = vshrq_n_u32(vaddq_u32(m128_muliplicationChannel2, vdupq_n_u32(8192u)), 14);
4204
4205 // finaly we blend the interpolation results together
4206
4207 const uint32x4_t m128_interpolation = vorrq_u32(vorrq_u32(m128_interpolation0, vshlq_n_u32(m128_interpolation1, 8)), vshlq_n_u32(m128_interpolation2, 16));
4208
4209 // we have to extract the get rid of the padding byte:
4210 // FEDCBA9876543210
4211 // BGR BGR BGR BGR
4212
4213 uint32_t intermediateBuffer[4];
4214 vst1q_u32(intermediateBuffer, m128_interpolation);
4215
4216 for (unsigned int i = 0u; i < 4u; ++i)
4217 {
4218 memcpy(targetPositionPixels + i, intermediateBuffer + i, sizeof(uint8_t) * 3);
4219 }
4220}
4221
4222template <>
4223OCEAN_FORCE_INLINE void FrameInterpolatorBilinear::interpolate4Pixels8BitPerChannelNEON<4u>(const uint8_t* source, const unsigned int offsetsTopLeftElements[4], const unsigned int offsetsTopRightElements[4], const unsigned int offsetsBottomLeftElements[4], const unsigned int offsetsBottomRightElements[4], const unsigned int validPixels[4], const DataType<uint8_t, 4u>::Type& borderColor, const uint32x4_t& m128_factorsRight, const uint32x4_t& m128_factorsBottom, typename DataType<uint8_t, 4u>::Type* targetPositionPixels)
4224{
4225 ocean_assert(source != nullptr);
4226 ocean_assert(targetPositionPixels != nullptr);
4227
4228 using PixelType = typename DataType<uint8_t, 4u>::Type;
4229
4230 // as we do not initialize the following intermediate data,
4231 // we hopefully will not allocate memory on the stack each time this function is called
4232 PixelType topLeftPixels[4];
4233 PixelType topRightPixels[4];
4234 PixelType bottomLeftPixels[4];
4235 PixelType bottomRightPixels[4];
4236
4237 // we will store the pixel information in the following pattern (here for RGBA):
4238 // FEDC BA98 7654 3210
4239 // ABGR ABGR ABGR ABGR
4240
4241 // we gather the individual source pixel values from the source image,
4242 // based on the calculated pixel locations
4243 for (unsigned int i = 0u; i < 4u; ++i)
4244 {
4245 if (validPixels[i])
4246 {
4247 *(topLeftPixels + i) = *((const PixelType*)(source + offsetsTopLeftElements[i]));
4248 *(topRightPixels + i) = *((const PixelType*)(source + offsetsTopRightElements[i]));
4249 *(bottomLeftPixels + i) = *((const PixelType*)(source + offsetsBottomLeftElements[i]));
4250 *(bottomRightPixels + i) = *((const PixelType*)(source + offsetsBottomRightElements[i]));
4251 }
4252 else
4253 {
4254 *(topLeftPixels + i) = borderColor;
4255 *(topRightPixels + i) = borderColor;
4256 *(bottomLeftPixels + i) = borderColor;
4257 *(bottomRightPixels + i) = borderColor;
4258 }
4259 }
4260
4261 static_assert(sizeof(uint32x4_t) == sizeof(topLeftPixels), "Invalid data type!");
4262
4263 const uint32x4_t m128_topLeftPixels = vreinterpretq_u32_u8(vld1q_u8((const uint8_t*)topLeftPixels));
4264 const uint32x4_t m128_topRightPixels = vreinterpretq_u32_u8(vld1q_u8((const uint8_t*)topRightPixels));
4265 const uint32x4_t m128_bottomLeftPixels = vreinterpretq_u32_u8(vld1q_u8((const uint8_t*)bottomLeftPixels));
4266 const uint32x4_t m128_bottomRightPixels = vreinterpretq_u32_u8(vld1q_u8((const uint8_t*)bottomRightPixels));
4267
4268
4269 // factorLeft = 128 - factorRight
4270 // factorTop = 128 - factorBottom
4271
4272 const uint32x4_t m128_factorsLeft = vsubq_u32(vdupq_n_u32(128u), m128_factorsRight);
4273 const uint32x4_t m128_factorsTop = vsubq_u32(vdupq_n_u32(128u), m128_factorsBottom);
4274
4275 // (top_left * factorLeft + top_right * factorRight) * factorTop + (bottom_left * factorLeft + bottom_right * factorRight) * factorBottom
4276 // == top_left * factorTopLeft + top_right * factorTopRight + bottom_left * factorBottomLeft + bottom_right * factorBottomRight
4277
4278 const uint32x4_t m128_factorsTopLeft = vmulq_u32(m128_factorsTop, m128_factorsLeft);
4279 const uint32x4_t m128_factorsTopRight = vmulq_u32(m128_factorsTop, m128_factorsRight);
4280 const uint32x4_t m128_factorsBottomLeft = vmulq_u32(m128_factorsBottom, m128_factorsLeft);
4281 const uint32x4_t m128_factorsBottomRight = vmulq_u32(m128_factorsBottom, m128_factorsRight);
4282
4283
4284 const uint32x4_t m128_maskFirstByte = vdupq_n_u32(0x000000FFu);
4285
4286 uint32x4_t m128_muliplicationChannel0 = vmulq_u32(vandq_u32(m128_topLeftPixels, m128_maskFirstByte), m128_factorsTopLeft);
4287 uint32x4_t m128_muliplicationChannel1 = vmulq_u32(vandq_u32(vshrq_n_u32(m128_topLeftPixels, 8), m128_maskFirstByte), m128_factorsTopLeft);
4288 uint32x4_t m128_muliplicationChannel2 = vmulq_u32(vandq_u32(vshrq_n_u32(m128_topLeftPixels, 16), m128_maskFirstByte), m128_factorsTopLeft);
4289 uint32x4_t m128_muliplicationChannel3 = vmulq_u32(vandq_u32(vshrq_n_u32(m128_topLeftPixels, 24), m128_maskFirstByte), m128_factorsTopLeft);
4290
4291 m128_muliplicationChannel0 = vaddq_u32(m128_muliplicationChannel0, vmulq_u32(vandq_u32(m128_topRightPixels, m128_maskFirstByte), m128_factorsTopRight));
4292 m128_muliplicationChannel1 = vaddq_u32(m128_muliplicationChannel1, vmulq_u32(vandq_u32(vshrq_n_u32(m128_topRightPixels, 8), m128_maskFirstByte), m128_factorsTopRight));
4293 m128_muliplicationChannel2 = vaddq_u32(m128_muliplicationChannel2, vmulq_u32(vandq_u32(vshrq_n_u32(m128_topRightPixels, 16), m128_maskFirstByte), m128_factorsTopRight));
4294 m128_muliplicationChannel3 = vaddq_u32(m128_muliplicationChannel3, vmulq_u32(vandq_u32(vshrq_n_u32(m128_topRightPixels, 24), m128_maskFirstByte), m128_factorsTopRight));
4295
4296 m128_muliplicationChannel0 = vaddq_u32(m128_muliplicationChannel0, vmulq_u32(vandq_u32(m128_bottomLeftPixels, m128_maskFirstByte), m128_factorsBottomLeft));
4297 m128_muliplicationChannel1 = vaddq_u32(m128_muliplicationChannel1, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomLeftPixels, 8), m128_maskFirstByte), m128_factorsBottomLeft));
4298 m128_muliplicationChannel2 = vaddq_u32(m128_muliplicationChannel2, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomLeftPixels, 16), m128_maskFirstByte), m128_factorsBottomLeft));
4299 m128_muliplicationChannel3 = vaddq_u32(m128_muliplicationChannel3, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomLeftPixels, 24), m128_maskFirstByte), m128_factorsBottomLeft));
4300
4301 m128_muliplicationChannel0 = vaddq_u32(m128_muliplicationChannel0, vmulq_u32(vandq_u32(m128_bottomRightPixels, m128_maskFirstByte), m128_factorsBottomRight));
4302 m128_muliplicationChannel1 = vaddq_u32(m128_muliplicationChannel1, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomRightPixels, 8), m128_maskFirstByte), m128_factorsBottomRight));
4303 m128_muliplicationChannel2 = vaddq_u32(m128_muliplicationChannel2, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomRightPixels, 16), m128_maskFirstByte), m128_factorsBottomRight));
4304 m128_muliplicationChannel3 = vaddq_u32(m128_muliplicationChannel3, vmulq_u32(vandq_u32(vshrq_n_u32(m128_bottomRightPixels, 24), m128_maskFirstByte), m128_factorsBottomRight));
4305
4306
4307 // we add 8192 and shift by 14 bits
4308
4309 const uint32x4_t m128_interpolation0 = vshrq_n_u32(vaddq_u32(m128_muliplicationChannel0, vdupq_n_u32(8192u)), 14);
4310 const uint32x4_t m128_interpolation1 = vshrq_n_u32(vaddq_u32(m128_muliplicationChannel1, vdupq_n_u32(8192u)), 14);
4311 const uint32x4_t m128_interpolation2 = vshrq_n_u32(vaddq_u32(m128_muliplicationChannel2, vdupq_n_u32(8192u)), 14);
4312 const uint32x4_t m128_interpolation3 = vshrq_n_u32(vaddq_u32(m128_muliplicationChannel3, vdupq_n_u32(8192u)), 14);
4313
4314 // finaly we blend the interpolation results together
4315
4316 const uint32x4_t m128_interpolation = vorrq_u32(vorrq_u32(m128_interpolation0, vshlq_n_u32(m128_interpolation1, 8)), vorrq_u32(vshlq_n_u32(m128_interpolation2, 16), vshlq_n_u32(m128_interpolation3, 24)));
4317
4318 vst1q_u8((uint8_t*)targetPositionPixels, vreinterpretq_u8_u32(m128_interpolation));
4319}
4320
4321OCEAN_FORCE_INLINE void FrameInterpolatorBilinear::interpolate4Pixels4Channel8BitPerChannelNEON(const uint8x16_t& topLeftPixels_u8x16, const uint8x16_t& topRightPixels_u8x16, const uint8x16_t& bottomLeftPixels_u8x16, const uint8x16_t& bottomRightPixels_u8x16, const uint32x4_t& m128_factorsRight, const uint32x4_t& m128_factorsBottom, typename DataType<uint8_t, 4u>::Type* targetPositionPixels, const bool useOptimizedNEONFactorReplication)
4322{
4323 ocean_assert(targetPositionPixels != nullptr);
4324
4325 // Replicate per-pixel u32 interpolation factors [f0,f1,f2,f3] to per-channel u8 factors.
4326 // Both implementations produce: lo = [f0,f0,f0,f0, f1,f1,f1,f1], hi = [f2,f2,f2,f2, f3,f3,f3,f3]
4327
4328 uint8x8_t factorsRight_lo;
4329 uint8x8_t factorsRight_hi;
4330 uint8x8_t factorsLeft_lo;
4331 uint8x8_t factorsLeft_hi;
4332 uint8x8_t factorsBottom_lo;
4333 uint8x8_t factorsBottom_hi;
4334 uint8x8_t factorsTop_lo;
4335 uint8x8_t factorsTop_hi;
4336
4337#if defined(__aarch64__)
4338 // vqtbl1q_u8 is an AArch64-only intrinsic; ARMv7 always falls through to the original narrow+zip path.
4339 if (!useOptimizedNEONFactorReplication)
4340#else
4341 (void)useOptimizedNEONFactorReplication;
4342#endif
4343 {
4344 const uint8x8_t factorsRight_u8x8 = vmovn_u16(vcombine_u16(vmovn_u32(m128_factorsRight), vmovn_u32(m128_factorsRight)));
4345 const uint8x8x2_t factorsRight_zip1 = vzip_u8(factorsRight_u8x8, factorsRight_u8x8);
4346 const uint8x8x2_t factorsRight_zip2 = vzip_u8(factorsRight_zip1.val[0], factorsRight_zip1.val[0]);
4347
4348 factorsRight_lo = factorsRight_zip2.val[0]; // pixels 0-1
4349 factorsRight_hi = factorsRight_zip2.val[1]; // pixels 2-3
4350 factorsLeft_lo = vsub_u8(vdup_n_u8(128u), factorsRight_lo);
4351 factorsLeft_hi = vsub_u8(vdup_n_u8(128u), factorsRight_hi);
4352
4353 const uint8x8_t factorsBottom_u8x8 = vmovn_u16(vcombine_u16(vmovn_u32(m128_factorsBottom), vmovn_u32(m128_factorsBottom)));
4354 const uint8x8x2_t factorsBottom_zip1 = vzip_u8(factorsBottom_u8x8, factorsBottom_u8x8);
4355 const uint8x8x2_t factorsBottom_zip2 = vzip_u8(factorsBottom_zip1.val[0], factorsBottom_zip1.val[0]);
4356
4357 factorsBottom_lo = factorsBottom_zip2.val[0];
4358 factorsBottom_hi = factorsBottom_zip2.val[1];
4359 factorsTop_lo = vsub_u8(vdup_n_u8(128u), factorsBottom_lo);
4360 factorsTop_hi = vsub_u8(vdup_n_u8(128u), factorsBottom_hi);
4361 }
4362#if defined(__aarch64__)
4363 else
4364 {
4365 // Replicate per-pixel u32 factors [f0,f1,f2,f3] to per-channel u8 via TBL on the u32 register directly.
4366 // On little-endian ARM64, the low byte of each u32 lane holds the factor value (0-128).
4367 // Indices pick byte 0 of each u32 lane (offsets 0, 4, 8, 12) and replicate 4x per channel.
4368 static const uint8x16_t replicateU32Idx = {0, 0, 0, 0, 4, 4, 4, 4, 8, 8, 8, 8, 12, 12, 12, 12};
4369
4370 const uint8x16_t factorsRight_rep = vqtbl1q_u8(vreinterpretq_u8_u32(m128_factorsRight), replicateU32Idx);
4371 const uint8x16_t factorsLeft_rep = vsubq_u8(vdupq_n_u8(128u), factorsRight_rep);
4372
4373 factorsRight_lo = vget_low_u8(factorsRight_rep);
4374 factorsRight_hi = vget_high_u8(factorsRight_rep);
4375 factorsLeft_lo = vget_low_u8(factorsLeft_rep);
4376 factorsLeft_hi = vget_high_u8(factorsLeft_rep);
4377
4378 const uint8x16_t factorsBottom_rep = vqtbl1q_u8(vreinterpretq_u8_u32(m128_factorsBottom), replicateU32Idx);
4379 const uint8x16_t factorsTop_rep = vsubq_u8(vdupq_n_u8(128u), factorsBottom_rep);
4380
4381 factorsBottom_lo = vget_low_u8(factorsBottom_rep);
4382 factorsBottom_hi = vget_high_u8(factorsBottom_rep);
4383 factorsTop_lo = vget_low_u8(factorsTop_rep);
4384 factorsTop_hi = vget_high_u8(factorsTop_rep);
4385 }
4386#endif // defined(__aarch64__)
4387
4388 // Split pixel data into halves: pixels 0-1 (lo) and pixels 2-3 (hi)
4389 const uint8x8_t topLeft_lo = vget_low_u8(topLeftPixels_u8x16);
4390 const uint8x8_t topLeft_hi = vget_high_u8(topLeftPixels_u8x16);
4391 const uint8x8_t topRight_lo = vget_low_u8(topRightPixels_u8x16);
4392 const uint8x8_t topRight_hi = vget_high_u8(topRightPixels_u8x16);
4393 const uint8x8_t bottomLeft_lo = vget_low_u8(bottomLeftPixels_u8x16);
4394 const uint8x8_t bottomLeft_hi = vget_high_u8(bottomLeftPixels_u8x16);
4395 const uint8x8_t bottomRight_lo = vget_low_u8(bottomRightPixels_u8x16);
4396 const uint8x8_t bottomRight_hi = vget_high_u8(bottomRightPixels_u8x16);
4397
4398 // --- Pixels 0-1 ---
4399 // Horizontal: topInterp = TL * fLeft + TR * fRight (u8 x u8 -> u16)
4400 const uint16x8_t topInterp_lo = vmlal_u8(vmull_u8(topLeft_lo, factorsLeft_lo), topRight_lo, factorsRight_lo);
4401 const uint16x8_t botInterp_lo = vmlal_u8(vmull_u8(bottomLeft_lo, factorsLeft_lo), bottomRight_lo, factorsRight_lo);
4402
4403 // Vertical: result = topInterp * fTop + botInterp * fBot (u16 x u16 -> u32)
4404 const uint16x8_t factorsTop_lo_u16 = vmovl_u8(factorsTop_lo);
4405 const uint16x8_t factorsBottom_lo_u16 = vmovl_u8(factorsBottom_lo);
4406
4407 const uint32x4_t result_lo_A = vmlal_u16(vmull_u16(vget_low_u16(topInterp_lo), vget_low_u16(factorsTop_lo_u16)), vget_low_u16(botInterp_lo), vget_low_u16(factorsBottom_lo_u16));
4408 const uint32x4_t result_lo_B = vmlal_u16(vmull_u16(vget_high_u16(topInterp_lo), vget_high_u16(factorsTop_lo_u16)), vget_high_u16(botInterp_lo), vget_high_u16(factorsBottom_lo_u16));
4409
4410 // --- Pixels 2-3 ---
4411 const uint16x8_t topInterp_hi = vmlal_u8(vmull_u8(topLeft_hi, factorsLeft_hi), topRight_hi, factorsRight_hi);
4412 const uint16x8_t botInterp_hi = vmlal_u8(vmull_u8(bottomLeft_hi, factorsLeft_hi), bottomRight_hi, factorsRight_hi);
4413
4414 const uint16x8_t factorsTop_hi_u16 = vmovl_u8(factorsTop_hi);
4415 const uint16x8_t factorsBottom_hi_u16 = vmovl_u8(factorsBottom_hi);
4416
4417 const uint32x4_t result_hi_A = vmlal_u16(vmull_u16(vget_low_u16(topInterp_hi), vget_low_u16(factorsTop_hi_u16)), vget_low_u16(botInterp_hi), vget_low_u16(factorsBottom_hi_u16));
4418 const uint32x4_t result_hi_B = vmlal_u16(vmull_u16(vget_high_u16(topInterp_hi), vget_high_u16(factorsTop_hi_u16)), vget_high_u16(botInterp_hi), vget_high_u16(factorsBottom_hi_u16));
4419
4420 // Round and narrow: (result + 8192) >> 14 via vrshrn_n_u32
4421 const uint16x4_t narrow_lo_A = vrshrn_n_u32(result_lo_A, 14);
4422 const uint16x4_t narrow_lo_B = vrshrn_n_u32(result_lo_B, 14);
4423 const uint16x4_t narrow_hi_A = vrshrn_n_u32(result_hi_A, 14);
4424 const uint16x4_t narrow_hi_B = vrshrn_n_u32(result_hi_B, 14);
4425
4426 const uint8x8_t result_lo = vmovn_u16(vcombine_u16(narrow_lo_A, narrow_lo_B));
4427 const uint8x8_t result_hi = vmovn_u16(vcombine_u16(narrow_hi_A, narrow_hi_B));
4428
4429 vst1q_u8((uint8_t*)targetPositionPixels, vcombine_u8(result_lo, result_hi));
4430}
4431
4432template <unsigned int tChannels>
4433OCEAN_FORCE_INLINE void FrameInterpolatorBilinear::interpolate4Pixels8BitPerChannelNEON(const uint8_t* source, const unsigned int offsetsTopLeftElements[4], const unsigned int offsetsTopRightElements[4], const unsigned int offsetsBottomLeftElements[4], const unsigned int offsetsBottomRightElements[4], const unsigned int validPixels[4], const typename DataType<uint8_t, tChannels>::Type& borderColor, const uint32x4_t& m128_factorsRight, const uint32x4_t& m128_factorsBottom, typename DataType<uint8_t, tChannels>::Type* targetPositionPixels)
4434{
4435 ocean_assert(source != nullptr);
4436 ocean_assert(targetPositionPixels != nullptr);
4437
4438 // as we do not initialize the following intermediate data,
4439 // we hopefully will not allocate memory on the stack each time this function is called
4440 unsigned int factorsTopLeft[4];
4441 unsigned int factorsTopRight[4];
4442 unsigned int factorsBottomLeft[4];
4443 unsigned int factorsBottomRight[4];
4444
4445
4446 // factorLeft = 128 - factorRight
4447 // factorTop = 128 - factorBottom
4448
4449 const uint32x4_t m128_factorsLeft = vsubq_u32(vdupq_n_u32(128u), m128_factorsRight);
4450 const uint32x4_t m128_factorsTop = vsubq_u32(vdupq_n_u32(128u), m128_factorsBottom);
4451
4452 // (top_left * factorLeft + top_right * factorRight) * factorTop + (bottom_left * factorLeft + bottom_right * factorRight) * factorBottom
4453 // == top_left * factorTopLeft + top_right * factorTopRight + bottom_left * factorBottomLeft + bottom_right * factorBottomRight
4454
4455 const uint32x4_t m128_factorsTopLeft = vmulq_u32(m128_factorsTop, m128_factorsLeft);
4456 const uint32x4_t m128_factorsTopRight = vmulq_u32(m128_factorsTop, m128_factorsRight);
4457 const uint32x4_t m128_factorsBottomLeft = vmulq_u32(m128_factorsBottom, m128_factorsLeft);
4458 const uint32x4_t m128_factorsBottomRight = vmulq_u32(m128_factorsBottom, m128_factorsRight);
4459
4460
4461 // we store the interpolation factors
4462 vst1q_u32(factorsTopLeft, m128_factorsTopLeft);
4463 vst1q_u32(factorsTopRight, m128_factorsTopRight);
4464 vst1q_u32(factorsBottomLeft, m128_factorsBottomLeft);
4465 vst1q_u32(factorsBottomRight, m128_factorsBottomRight);
4466
4467 for (unsigned int i = 0u; i < 4u; ++i)
4468 {
4469 if (validPixels[i])
4470 {
4471 const uint8_t* topLeft = source + offsetsTopLeftElements[i];
4472 const uint8_t* topRight = source + offsetsTopRightElements[i];
4473
4474 const uint8_t* bottomLeft = source + offsetsBottomLeftElements[i];
4475 const uint8_t* bottomRight = source + offsetsBottomRightElements[i];
4476
4477 const unsigned int& factorTopLeft = factorsTopLeft[i];
4478 const unsigned int& factorTopRight = factorsTopRight[i];
4479 const unsigned int& factorBottomLeft = factorsBottomLeft[i];
4480 const unsigned int& factorBottomRight = factorsBottomRight[i];
4481
4482 for (unsigned int n = 0u; n < tChannels; ++n)
4483 {
4484 ((uint8_t*)targetPositionPixels)[n] = (topLeft[n] * factorTopLeft + topRight[n] * factorTopRight + bottomLeft[n] * factorBottomLeft + bottomRight[n] * factorBottomRight + 8192u) >> 14u;
4485 }
4486 }
4487 else
4488 {
4489 *targetPositionPixels = borderColor;
4490 }
4491
4492 targetPositionPixels++;
4493 }
4494}
4495
4496#endif // OCEAN_HARDWARE_NEON_VERSION
4497
4498template <unsigned int tChannels>
4499inline void FrameInterpolatorBilinear::homographies8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* homographies, const uint8_t* borderColor, uint8_t* output, const Scalar outputQuadrantCenterX, const Scalar outputQuadrantCenterY, const int outputOriginX, const int outputOriginY, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
4500{
4501 static_assert(tChannels >= 1u, "Invalid channel number!");
4502
4503 ocean_assert(input && output);
4504 ocean_assert(inputWidth > 0u && inputHeight > 0u);
4505 ocean_assert(outputWidth > 0u && outputHeight > 0u);
4506
4507 ocean_assert(outputQuadrantCenterX >= 0 && outputQuadrantCenterX < Scalar(outputWidth));
4508 ocean_assert(outputQuadrantCenterY >= 0 && outputQuadrantCenterY < Scalar(outputHeight));
4509 ocean_assert(homographies);
4510
4511 const unsigned int outputStrideElements = outputWidth * tChannels + outputPaddingElements;
4512
4513 const Scalar scalarInputWidth_1 = Scalar(inputWidth - 1u);
4514 const Scalar scalarInputHeight_1 = Scalar(inputHeight - 1u);
4515
4516 constexpr uint8_t zeroColor[tChannels] = {uint8_t(0)};
4517 const uint8_t* const bColor = borderColor ? borderColor : zeroColor;
4518
4519 uint8_t* outputData = output + firstOutputRow * outputStrideElements;
4520
4521 const Scalar left = Scalar(outputQuadrantCenterX) * Scalar(0.5);
4522 const Scalar right = (Scalar(outputWidth) + Scalar(outputQuadrantCenterX)) * Scalar(0.5);
4523
4524 const Scalar top = Scalar(outputQuadrantCenterY) * Scalar(0.5);
4525 const Scalar bottom = (Scalar(outputHeight) + Scalar(outputQuadrantCenterY)) * Scalar(0.5);
4526
4527 ocean_assert(right - left > Numeric::eps());
4528 ocean_assert(bottom - top > Numeric::eps());
4529
4530 const Scalar invWidth = Scalar(1) / Scalar(right - left);
4531 const Scalar invHeight = Scalar(1) / Scalar(bottom - top);
4532
4533 for (unsigned int y = firstOutputRow; y < firstOutputRow + numberOutputRows; ++y)
4534 {
4535 for (unsigned int x = 0; x < outputWidth; ++x)
4536 {
4537 Vector2 outputPosition = Vector2(Scalar(int(x)), Scalar(int(y)));
4538
4539 const Scalar _tx = minmax<Scalar>(0, (outputPosition.x() - left) * invWidth, 1);
4540 const Scalar _ty = minmax<Scalar>(0, (outputPosition.y() - top) * invHeight, 1);
4541
4542 outputPosition += Vector2(Scalar(outputOriginX), Scalar(outputOriginY));
4543
4544 const Scalar tx = 1 - _tx;
4545 const Scalar ty = 1 - _ty;
4546
4547 const Vector2 inputPositionTopLeft(homographies[0] * outputPosition);
4548 const Vector2 inputPositionTopRight(homographies[1] * outputPosition);
4549 const Vector2 inputPositionBottomLeft(homographies[2] * outputPosition);
4550 const Vector2 inputPositionBottomRight(homographies[3] * outputPosition);
4551
4552 const Scalar tTopLeft = tx * ty;
4553 const Scalar tTopRight = _tx * ty;
4554 const Scalar tBottomLeft = tx * _ty;
4555 const Scalar tBottomRight = _tx * _ty;
4556
4557 const Vector2 inputPosition = inputPositionTopLeft * tTopLeft + inputPositionTopRight * tTopRight
4558 + inputPositionBottomLeft * tBottomLeft + inputPositionBottomRight * tBottomRight;
4559
4560 if (inputPosition.x() < Scalar(0) || inputPosition.x() > scalarInputWidth_1 || inputPosition.y() < Scalar(0) || inputPosition.y() > scalarInputHeight_1)
4561 {
4562 for (unsigned int c = 0u; c < tChannels; ++c)
4563 {
4564 outputData[c] = bColor[c];
4565 }
4566 }
4567 else
4568 {
4569 interpolatePixel8BitPerChannel<tChannels, PC_TOP_LEFT>(input, inputWidth, inputHeight, inputPaddingElements, inputPosition, outputData);
4570 }
4571
4572 outputData += tChannels;
4573 }
4574
4575 outputData += outputPaddingElements;
4576 }
4577}
4578
4579template <unsigned int tChannels>
4580void FrameInterpolatorBilinear::homographyMask8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* input_H_output, uint8_t* output, uint8_t* outputMask, const uint8_t maskValue, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, unsigned int firstOutputRow, const unsigned int numberOutputRows)
4581{
4582 static_assert(tChannels >= 1u, "Invalid channel number!");
4583
4584 ocean_assert(input != nullptr && output != nullptr);
4585 ocean_assert(inputWidth > 0u && inputHeight > 0u);
4586 ocean_assert(outputWidth > 0u && outputHeight > 0u);
4587 ocean_assert(input_H_output != nullptr);
4588
4589 ocean_assert_and_suppress_unused(firstOutputRow + numberOutputRows <= outputHeight, outputHeight);
4590
4591 const unsigned int outputStrideElements = outputWidth * tChannels + outputPaddingElements;
4592 const unsigned int outputMaskStrideElements = outputWidth + outputMaskPaddingElements;
4593
4594 const Scalar scalarInputWidth_1 = Scalar(inputWidth - 1u);
4595 const Scalar scalarInputHeight_1 = Scalar(inputHeight - 1u);
4596
4597 using PixelType = typename DataType<uint8_t, tChannels>::Type;
4598
4599 for (unsigned int y = firstOutputRow; y < firstOutputRow + numberOutputRows; ++y)
4600 {
4601 PixelType* outputData = (PixelType*)(output + y * outputStrideElements);
4602 uint8_t* outputMaskData = outputMask + y * outputMaskStrideElements;
4603
4604 /*
4605 * We can slightly optimize the 3x3 matrix multiplication:
4606 *
4607 * | X0 Y0 Z0 | | x |
4608 * | X1 Y1 Z1 | * | y |
4609 * | X2 Y2 Z2 | | 1 |
4610 *
4611 * | x' | | X0 * x | | Y0 * y + Z0 |
4612 * | y' | = | X1 * x | + | Y1 * y + Z1 |
4613 * | z' | | X2 * x | | Y2 * y + Z2 |
4614 *
4615 * As y is constant within the inner loop, we can pre-calculate the following terms:
4616 *
4617 * | x' | | (X0 * x + constValue0) / (X2 * x + constValue2) |
4618 * | y' | = | (X1 * x + constValue1) / (X2 * x + constValue2) |
4619 *
4620 * | p | = | (X * x + c) / (X2 * x + constValue2) |
4621 */
4622
4623 const Vector2 X(input_H_output->data() + 0);
4624 const Vector2 c(Vector2(input_H_output->data() + 3) * Scalar(y) + Vector2(input_H_output->data() + 6));
4625
4626 const Scalar X2 = (*input_H_output)(2, 0);
4627 const Scalar constValue2 = (*input_H_output)(2, 1) * Scalar(y) + (*input_H_output)(2, 2);
4628
4629 for (unsigned int x = 0; x < outputWidth; ++x)
4630 {
4631 const Vector2 inputPosition((X * Scalar(x) + c) / (X2 * Scalar(x) + constValue2));
4632
4633#ifdef OCEAN_DEBUG
4634 const Vector2 debugInputPosition(*input_H_output * Vector2(Scalar(x), Scalar(y)));
4635 ocean_assert(inputPosition.isEqual(debugInputPosition, Scalar(0.01)));
4636#endif
4637
4638 if (inputPosition.x() < Scalar(0) || inputPosition.x() > scalarInputWidth_1 || inputPosition.y() < Scalar(0) || inputPosition.y() > scalarInputHeight_1)
4639 {
4640 *outputMaskData = 0xFF - maskValue;
4641 }
4642 else
4643 {
4644 interpolatePixel8BitPerChannel<tChannels, PC_TOP_LEFT>(input, inputWidth, inputHeight, inputPaddingElements, inputPosition, (uint8_t*)(outputData));
4645 *outputMaskData = maskValue;
4646 }
4647
4648 outputData++;
4649 outputMaskData++;
4650 }
4651 }
4652}
4653
4654template <unsigned int tChannels>
4655inline void FrameInterpolatorBilinear::homographiesMask8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3* homographies, uint8_t* output, uint8_t* outputMask, const uint8_t maskValue, const Scalar outputQuadrantCenterX, const Scalar outputQuadrantCenterY, const int outputOriginX, const int outputOriginY, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
4656{
4657 static_assert(tChannels >= 1u, "Invalid channel number!");
4658
4659 ocean_assert(input && output);
4660 ocean_assert(inputWidth > 0u && inputHeight > 0u);
4661 ocean_assert(outputWidth > 0u && outputHeight > 0u);
4662
4663 ocean_assert(outputQuadrantCenterX >= 0 && outputQuadrantCenterX < Scalar(outputWidth));
4664 ocean_assert(outputQuadrantCenterY >= 0 && outputQuadrantCenterY < Scalar(outputHeight));
4665 ocean_assert(homographies);
4666
4667 const unsigned int outputStrideElements = tChannels * outputWidth + outputPaddingElements;
4668 const unsigned int outputMaskStrideElements = outputWidth + outputMaskPaddingElements;
4669
4670 const Scalar scalarInputWidth_1 = Scalar(inputWidth - 1u);
4671 const Scalar scalarInputHeight_1 = Scalar(inputHeight - 1u);
4672
4673 uint8_t* outputData = output + firstOutputRow * outputStrideElements;
4674 outputMask += firstOutputRow * outputMaskStrideElements;
4675
4676 const Scalar left = Scalar(outputQuadrantCenterX) * Scalar(0.5);
4677 const Scalar right = (Scalar(outputWidth) + Scalar(outputQuadrantCenterX)) * Scalar(0.5);
4678
4679 const Scalar top = Scalar(outputQuadrantCenterY) * Scalar(0.5);
4680 const Scalar bottom = (Scalar(outputHeight) + Scalar(outputQuadrantCenterY)) * Scalar(0.5);
4681
4682 ocean_assert(right - left > Numeric::eps());
4683 ocean_assert(bottom - top > Numeric::eps());
4684
4685 const Scalar invWidth = Scalar(1) / Scalar(right - left);
4686 const Scalar invHeight = Scalar(1) / Scalar(bottom - top);
4687
4688 for (unsigned int y = firstOutputRow; y < firstOutputRow + numberOutputRows; ++y)
4689 {
4690 for (unsigned int x = 0u; x < outputWidth; ++x)
4691 {
4692 Vector2 outputPosition = Vector2(Scalar(int(x)), Scalar(int(y)));
4693
4694 const Scalar _tx = minmax<Scalar>(0, (outputPosition.x() - left) * invWidth, 1);
4695 const Scalar _ty = minmax<Scalar>(0, (outputPosition.y() - top) * invHeight, 1);
4696
4697 outputPosition += Vector2(Scalar(outputOriginX), Scalar(outputOriginY));
4698
4699 const Scalar tx = 1 - _tx;
4700 const Scalar ty = 1 - _ty;
4701
4702 const Vector2 inputPositionTopLeft(homographies[0] * outputPosition);
4703 const Vector2 inputPositionTopRight(homographies[1] * outputPosition);
4704 const Vector2 inputPositionBottomLeft(homographies[2] * outputPosition);
4705 const Vector2 inputPositionBottomRight(homographies[3] * outputPosition);
4706
4707 const Scalar tTopLeft = tx * ty;
4708 const Scalar tTopRight = _tx * ty;
4709 const Scalar tBottomLeft = tx * _ty;
4710 const Scalar tBottomRight = _tx * _ty;
4711
4712 const Vector2 inputPosition = inputPositionTopLeft * tTopLeft + inputPositionTopRight * tTopRight
4713 + inputPositionBottomLeft * tBottomLeft + inputPositionBottomRight * tBottomRight;
4714
4715 if (inputPosition.x() < Scalar(0) || inputPosition.x() > scalarInputWidth_1 || inputPosition.y() < Scalar(0) || inputPosition.y() > scalarInputHeight_1)
4716 {
4717 *outputMask = 0xFFu - maskValue;
4718 }
4719 else
4720 {
4721 interpolatePixel8BitPerChannel<tChannels, PC_TOP_LEFT>(input, inputWidth, inputHeight, inputPaddingElements, inputPosition, outputData);
4722 *outputMask = maskValue;
4723 }
4724
4725 outputData += tChannels;
4726 outputMask++;
4727 }
4728
4729 outputData += outputPaddingElements;
4730 outputMask += outputMaskPaddingElements;
4731 }
4732}
4733
4734template <unsigned int tChannels>
4735void FrameInterpolatorBilinear::homographyWithCamera8BitPerChannelSubset(const PinholeCamera* inputCamera, const PinholeCamera* outputCamera, const PinholeCamera::DistortionLookup* outputCameraDistortionLookup, const uint8_t* input, const SquareMatrix3* normalizedHomography, const bool useDistortionParameters, const uint8_t* borderColor, uint8_t* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows)
4736{
4737 static_assert(tChannels >= 1u, "Invalid channel number!");
4738
4739 ocean_assert(inputCamera && outputCamera && normalizedHomography);
4740 ocean_assert(input && output);
4741
4742 ocean_assert(firstRow + numberRows <= outputCamera->height());
4743
4744 const unsigned int outputStrideElements = tChannels * outputCamera->width() + outputPaddingElements;
4745
4746 const Scalar scalarInputWidth_1 = Scalar(inputCamera->width() - 1u);
4747 const Scalar scalarInputHeight_1 = Scalar(inputCamera->height() - 1u);
4748
4749 const SquareMatrix3 combinedMatrix(*normalizedHomography * outputCamera->invertedIntrinsic());
4750
4751 using PixelType = typename DataType<uint8_t, tChannels>::Type;
4752
4753 const uint8_t zeroColor[tChannels] = {uint8_t(0)};
4754 const PixelType* const bColor = borderColor ? (PixelType*)borderColor : (PixelType*)zeroColor;
4755
4756 uint8_t* outputData = output + firstRow * outputStrideElements;
4757
4758 for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
4759 {
4760 for (unsigned int x = 0; x < outputCamera->width(); ++x)
4761 {
4762 const Vector2 inputPosition(inputCamera->normalizedImagePoint2imagePoint<true>(combinedMatrix * outputCameraDistortionLookup->undistortedImagePoint(Vector2(Scalar(x), Scalar(y))), useDistortionParameters));
4763
4764 if (inputPosition.x() < Scalar(0) || inputPosition.x() > scalarInputWidth_1 || inputPosition.y() < Scalar(0) || inputPosition.y() > scalarInputHeight_1)
4765 {
4766 *((PixelType*)outputData) = *bColor;
4767 }
4768 else
4769 {
4770 interpolatePixel8BitPerChannel<tChannels, PC_TOP_LEFT>(input, inputCamera->width(), inputCamera->height(), inputPaddingElements, inputPosition, outputData);
4771 }
4772
4773 outputData += tChannels;
4774 }
4775
4776 outputData += outputPaddingElements;
4777 }
4778}
4779
4780template <unsigned int tChannels>
4781void FrameInterpolatorBilinear::homographyWithCameraMask8BitPerChannelSubset(const PinholeCamera* inputCamera, const PinholeCamera* outputCamera, const PinholeCamera::DistortionLookup* outputCameraDistortionLookup, const uint8_t* input, const unsigned int inputPaddingElements, const SquareMatrix3* normalizedHomography, uint8_t* output, uint8_t* outputMask, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, const uint8_t maskValue, const unsigned int firstRow, const unsigned int numberRows)
4782{
4783 static_assert(tChannels >= 1u, "Invalid channel number!");
4784
4785 ocean_assert(inputCamera != nullptr && outputCamera != nullptr && normalizedHomography != nullptr);
4786 ocean_assert(input != nullptr && output != nullptr);
4787
4788 ocean_assert(firstRow + numberRows <= outputCamera->height());
4789
4790 const unsigned int outputStrideElements = outputCamera->width() * tChannels + outputPaddingElements;
4791 const unsigned int outputMaskStrideElements = outputCamera->width() + outputMaskPaddingElements;
4792
4793 const Scalar scalarInputWidth_1 = Scalar(inputCamera->width() - 1u);
4794 const Scalar scalarInputHeight_1 = Scalar(inputCamera->height() - 1u);
4795
4796 const SquareMatrix3 combinedMatrix(*normalizedHomography * outputCamera->invertedIntrinsic());
4797
4798 uint8_t* outputData = output + firstRow * outputStrideElements;
4799 outputMask += firstRow * outputMaskStrideElements;
4800
4801 constexpr bool useDistortionParameters = true;
4802
4803 for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
4804 {
4805 for (unsigned int x = 0; x < outputCamera->width(); ++x)
4806 {
4807 const Vector2 inputPosition(inputCamera->normalizedImagePoint2imagePoint<true>(combinedMatrix * outputCameraDistortionLookup->undistortedImagePoint(Vector2(Scalar(x), Scalar(y))), useDistortionParameters));
4808
4809 if (inputPosition.x() < Scalar(0) || inputPosition.x() > scalarInputWidth_1 || inputPosition.y() < Scalar(0) || inputPosition.y() > scalarInputHeight_1)
4810 {
4811 *outputMask = 0xFF - maskValue;
4812 }
4813 else
4814 {
4815 interpolatePixel8BitPerChannel<tChannels, PC_TOP_LEFT>(input, inputCamera->width(), inputCamera->height(), inputPaddingElements, inputPosition, outputData);
4816 *outputMask = maskValue;
4817 }
4818
4819 outputData += tChannels;
4820 ++outputMask;
4821 }
4822
4823 outputData += outputPaddingElements;
4824 outputMask += outputMaskPaddingElements;
4825 }
4826}
4827
4828template <unsigned int tChannels>
4829void FrameInterpolatorBilinear::lookup8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable* input_LT_output, const bool offset, const uint8_t* borderColor, uint8_t* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows)
4830{
4831 static_assert(tChannels >= 1u, "Invalid channel number!");
4832
4833 ocean_assert(input_LT_output != nullptr);
4834 ocean_assert(input != nullptr && output != nullptr);
4835
4836 ocean_assert(inputWidth != 0u && inputHeight != 0u);
4837 ocean_assert(firstRow + numberRows <= input_LT_output->sizeY());
4838
4839 using PixelType = typename DataType<uint8_t, tChannels>::Type;
4840
4841 const uint8_t zeroColor[tChannels] = {uint8_t(0)};
4842 const PixelType* const bColor = borderColor ? (PixelType*)borderColor : (PixelType*)zeroColor;
4843
4844 // sizeX() is a size_t, but everything it feeds here is unsigned int: the output
4845 // stride, the loop counters, and Ocean's frame API. So narrow it once, and verify
4846 // the cast round-trips - bilinearValues() always fills the full sizeX(), so a
4847 // truncated count would size the row lookup buffer too small.
4848 const unsigned int columns = (unsigned int)(input_LT_output->sizeX());
4849
4850 if (size_t(columns) != input_LT_output->sizeX())
4851 {
4852 ocean_assert(false && "Lookup table is wider than 32 bits!");
4853 return;
4854 }
4855
4856 const unsigned int outputStrideElements = tChannels * columns + outputPaddingElements;
4857
4858 static_assert(std::is_same<Vector2, LookupTable::Type>::value, "Invalid data type!");
4859
4860 const Scalar inputWidth1 = Scalar(inputWidth - 1u);
4861 const Scalar inputHeight1 = Scalar(inputHeight - 1u);
4862
4863 Memory rowLookupMemory = Memory::create<Vector2>(columns);
4864 Vector2* const rowLookupData = rowLookupMemory.data<Vector2>();
4865
4866 if (rowLookupData == nullptr)
4867 {
4868 // Memory leaves the buffer nullptr if the allocation fails, while
4869 // bilinearValues() below writes values[0] unconditionally
4870 ocean_assert(false && "Failed to allocate the row lookup buffer!");
4871 return;
4872 }
4873
4874 for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
4875 {
4876 input_LT_output->bilinearValues(y, rowLookupData);
4877
4878 PixelType* outputData = (PixelType*)(output + y * outputStrideElements);
4879
4880 for (unsigned int x = 0u; x < columns; ++x)
4881 {
4882 const Vector2& lookupValue = rowLookupData[x];
4883
4884 const Vector2 inputPosition = offset ? Vector2(Scalar(x) + lookupValue.x(), Scalar(y) + lookupValue.y()) : lookupValue;
4885
4886 if (inputPosition.x() >= Scalar(0) && inputPosition.y() >= Scalar(0) && inputPosition.x() <= inputWidth1 && inputPosition.y() <= inputHeight1)
4887 {
4888 interpolatePixel8BitPerChannel<tChannels, PC_TOP_LEFT>(input, inputWidth, inputHeight, inputPaddingElements, inputPosition, (uint8_t*)(outputData));
4889 }
4890 else
4891 {
4892 *outputData = *bColor;
4893 }
4894
4895 outputData++;
4896 }
4897 }
4898}
4899
4900template <typename T, unsigned int tChannels>
4901void FrameInterpolatorBilinear::lookupSubset(const T* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable* input_LT_output, const bool offset, const T* borderColor, T* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows)
4902{
4903 static_assert(tChannels >= 1u, "Invalid channel number!");
4904
4905 ocean_assert((!std::is_same<uint8_t, T>::value));
4906
4907 ocean_assert(input_LT_output != nullptr);
4908 ocean_assert(input != nullptr && output != nullptr);
4909
4910 ocean_assert(inputWidth != 0u && inputHeight != 0u);
4911 ocean_assert(firstRow + numberRows <= input_LT_output->sizeY());
4912
4913 using PixelType = typename DataType<T, tChannels>::Type;
4914
4915 const T zeroColor[tChannels] = {T(0)};
4916 const PixelType* const bColor = borderColor ? (PixelType*)borderColor : (PixelType*)zeroColor;
4917
4918 // sizeX() is a size_t, but everything it feeds here is unsigned int: the output
4919 // stride, the loop counters, and Ocean's frame API. So narrow it once, and verify
4920 // the cast round-trips - bilinearValues() always fills the full sizeX(), so a
4921 // truncated count would size the row lookup buffer too small.
4922 const unsigned int columns = (unsigned int)(input_LT_output->sizeX());
4923
4924 if (size_t(columns) != input_LT_output->sizeX())
4925 {
4926 ocean_assert(false && "Lookup table is wider than 32 bits!");
4927 return;
4928 }
4929
4930 const unsigned int outputStrideElements = tChannels * columns + outputPaddingElements;
4931
4932 static_assert(std::is_same<Vector2, LookupTable::Type>::value, "Invalid data type!");
4933
4934 const Scalar inputWidth1 = Scalar(inputWidth - 1u);
4935 const Scalar inputHeight1 = Scalar(inputHeight - 1u);
4936
4937 Memory rowLookupMemory = Memory::create<Vector2>(columns);
4938 Vector2* const rowLookupData = rowLookupMemory.data<Vector2>();
4939
4940 if (rowLookupData == nullptr)
4941 {
4942 // Memory leaves the buffer nullptr if the allocation fails, while
4943 // bilinearValues() below writes values[0] unconditionally
4944 ocean_assert(false && "Failed to allocate the row lookup buffer!");
4945 return;
4946 }
4947
4948 for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
4949 {
4950 input_LT_output->bilinearValues(y, rowLookupData);
4951
4952 PixelType* outputData = (PixelType*)(output + y * outputStrideElements);
4953
4954 for (unsigned int x = 0u; x < columns; ++x)
4955 {
4956 const Vector2& lookupValue = rowLookupData[x];
4957
4958 const Vector2 inputPosition = offset ? Vector2(Scalar(x) + lookupValue.x(), Scalar(y) + lookupValue.y()) : lookupValue;
4959
4960 if (inputPosition.x() >= Scalar(0) && inputPosition.y() >= Scalar(0) && inputPosition.x() <= inputWidth1 && inputPosition.y() <= inputHeight1)
4961 {
4962 interpolatePixel<T, T, tChannels, PC_TOP_LEFT>(input, inputWidth, inputHeight, inputPaddingElements, inputPosition, (T*)(outputData));
4963 }
4964 else
4965 {
4966 *outputData = *bColor;
4967 }
4968
4969 outputData++;
4970 }
4971 }
4972}
4973
4974#if defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
4975
4976template <>
4977inline void FrameInterpolatorBilinear::lookup8BitPerChannelSubsetNEON<1u>(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable* input_LT_output, const bool offset, const uint8_t* borderColor, uint8_t* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows, const bool useOptimizedNEON, const bool useOptimizedBilinearValuesAndFactorCalculation, const bool /*useOptimizedNEONFactorReplication*/, [[maybe_unused]] const bool use4ChannelSamplingFastPaths)
4978{
4979 ocean_assert(input_LT_output != nullptr);
4980 ocean_assert(input != nullptr && output != nullptr);
4981
4982 ocean_assert(inputWidth != 0u && inputHeight != 0u);
4983 ocean_assert(firstRow + numberRows <= input_LT_output->sizeY());
4984
4985 using PixelType = uint8_t;
4986
4987 const uint8x16_t constantBorderColor_u_8x16 = vdupq_n_u8(borderColor ? *borderColor : 0u);
4988
4989 // sizeX() is a size_t, but everything it feeds here is unsigned int: the output
4990 // stride, the loop counters, and Ocean's frame API. So narrow it once, and verify
4991 // the cast round-trips - bilinearValues() always fills the full sizeX(), so a
4992 // truncated count would size the row lookup buffer too small.
4993 const unsigned int outputWidth = (unsigned int)(input_LT_output->sizeX());
4994
4995 if (size_t(outputWidth) != input_LT_output->sizeX())
4996 {
4997 ocean_assert(false && "Lookup table is wider than 32 bits!");
4998 return;
4999 }
5000
5001 ocean_assert(outputWidth >= 8u);
5002
5003 static_assert(std::is_same<Vector2, LookupTable::Type>::value, "Invalid data type!");
5004
5005 const unsigned int inputStrideElements = inputWidth + inputPaddingElements;
5006 const unsigned int outputStrideElements = outputWidth + outputPaddingElements;
5007
5008 Memory rowLookupMemory = Memory::create<VectorF2>(outputWidth);
5009 VectorF2* const rowLookupData = rowLookupMemory.data<VectorF2>();
5010
5011 if (rowLookupData == nullptr)
5012 {
5013 // Memory leaves the buffer nullptr if the allocation fails, while
5014 // bilinearValues() below writes values[0] unconditionally
5015 ocean_assert(false && "Failed to allocate the row lookup buffer!");
5016 return;
5017 }
5018
5019 const float32x4_t constantZero_f_32x4 = vdupq_n_f32(0.0f); // [0.0f, 0.0f, 0.0f, 0.0f]
5020 const float32x4_t constantEight_f_32x4 = vdupq_n_f32(8.0f); // [4.0f, 4.0f, 4.0f, 4.0f]
5021
5022 // [0.0f, 1.0f, 2.0f, 3.0f, ...]
5023 const float f_01234567[8] = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f};
5024 const float32x4_t conststant0123_f_32x4 = vld1q_f32(f_01234567 + 0);
5025 const float32x4_t conststant4567_f_32x4 = vld1q_f32(f_01234567 + 4);
5026
5027 const float32x4_t constant128_f_32x4 = vdupq_n_f32(128.0f);
5028
5029 const uint32x4_t constantOne_u_32x4 = vdupq_n_u32(1u);
5030
5031 const uint32x4_t constantChannels_u_32x4 = vdupq_n_u32(1u);
5032
5033 const float32x4_t constantInputWidth1_f_32x4 = vdupq_n_f32(float(inputWidth - 1u));
5034 const float32x4_t constantInputHeight1_f_32x4 = vdupq_n_f32(float(inputHeight - 1u));
5035
5036 const uint32x4_t constantInputStrideElements_u_32x4 = vdupq_n_u32(inputStrideElements);
5037 const uint32x4_t constantInputHeight1_u_32x4 = vdupq_n_u32(inputHeight - 1u);
5038
5039 unsigned int validPixels[8];
5040
5041 unsigned int topLeftOffsetsElements[8];
5042 unsigned int bottomLeftOffsetsElements[8];
5043
5044 uint8_t pixels[32];
5045
5046 for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
5047 {
5048 PixelType* outputPixelData = (PixelType*)(output + y * outputStrideElements);
5049
5050 input_LT_output->bilinearValues<VectorF2>(y, rowLookupData);
5051
5052 float32x4_t additionalInputOffsetX0123_f_32x4 = conststant0123_f_32x4;
5053 float32x4_t additionalInputOffsetX4567_f_32x4 = conststant4567_f_32x4;
5054
5055 const float32x4_t additionalInputOffsetY_f_32x4 = vdupq_n_f32(float(y));
5056
5057 for (unsigned int x = 0u; x < outputWidth; x += 8u)
5058 {
5059 if (x + 8u > outputWidth)
5060 {
5061 // the last iteration will not fit into the output frame,
5062 // so we simply shift x left by some pixels (at most 7) and we will calculate some pixels again
5063
5064 ocean_assert(x >= 8u && outputWidth > 8u);
5065 const unsigned int newX = outputWidth - 8u;
5066
5067 ocean_assert(x > newX);
5068 const unsigned int xOffset = x - newX;
5069
5070 outputPixelData -= xOffset;
5071
5072 if (offset)
5073 {
5074 additionalInputOffsetX0123_f_32x4 = vsubq_f32(additionalInputOffsetX0123_f_32x4, vdupq_n_f32(float(xOffset)));
5075 additionalInputOffsetX4567_f_32x4 = vsubq_f32(additionalInputOffsetX4567_f_32x4, vdupq_n_f32(float(xOffset)));
5076 }
5077
5078 x = newX;
5079
5080 // the for loop will stop after this iteration
5081 ocean_assert(!(x + 8u < outputWidth));
5082 }
5083
5084 const float32x4x2_t inputPositions0123_f_32x4x2 = vld2q_f32((const float*)(rowLookupData + x + 0u));
5085 const float32x4x2_t inputPositions4567_f_32x4x2 = vld2q_f32((const float*)(rowLookupData + x + 4u));
5086
5087 float32x4_t inputPositionsX0123_f_32x4 = inputPositions0123_f_32x4x2.val[0];
5088 float32x4_t inputPositionsY0123_f_32x4 = inputPositions0123_f_32x4x2.val[1];
5089
5090 float32x4_t inputPositionsX4567_f_32x4 = inputPositions4567_f_32x4x2.val[0];
5091 float32x4_t inputPositionsY4567_f_32x4 = inputPositions4567_f_32x4x2.val[1];
5092
5093 if (offset)
5094 {
5095 inputPositionsX0123_f_32x4 = vaddq_f32(inputPositionsX0123_f_32x4, additionalInputOffsetX0123_f_32x4);
5096 inputPositionsY0123_f_32x4 = vaddq_f32(inputPositionsY0123_f_32x4, additionalInputOffsetY_f_32x4);
5097
5098 inputPositionsX4567_f_32x4 = vaddq_f32(inputPositionsX4567_f_32x4, additionalInputOffsetX4567_f_32x4);
5099 inputPositionsY4567_f_32x4 = vaddq_f32(inputPositionsY4567_f_32x4, additionalInputOffsetY_f_32x4);
5100
5101 additionalInputOffsetX0123_f_32x4 = vaddq_f32(additionalInputOffsetX0123_f_32x4, constantEight_f_32x4);
5102 additionalInputOffsetX4567_f_32x4 = vaddq_f32(additionalInputOffsetX4567_f_32x4, constantEight_f_32x4);
5103 }
5104
5105 // now we check whether we are inside the input frame
5106 const uint32x4_t validPixelsX0123_u_32x4 = vandq_u32(vcltq_f32(inputPositionsX0123_f_32x4, constantInputWidth1_f_32x4), vcgeq_f32(inputPositionsX0123_f_32x4, constantZero_f_32x4)); // inputPosition.x() >= 0 && inputPosition.x() < (inputWidth - 1) ? 0xFFFFFF : 0x000000
5107 const uint32x4_t validPixelsX4567_u_32x4 = vandq_u32(vcltq_f32(inputPositionsX4567_f_32x4, constantInputWidth1_f_32x4), vcgeq_f32(inputPositionsX4567_f_32x4, constantZero_f_32x4));
5108
5109 const uint32x4_t validPixelsY0123_u_32x4 = vandq_u32(vcltq_f32(inputPositionsY0123_f_32x4, constantInputHeight1_f_32x4), vcgeq_f32(inputPositionsY0123_f_32x4, constantZero_f_32x4)); // inputPosition.y() >= 0 && inputPosition.y() < (inputHeight - 1) ? 0xFFFFFF : 0x000000
5110 const uint32x4_t validPixelsY4567_u_32x4 = vandq_u32(vcltq_f32(inputPositionsY4567_f_32x4, constantInputHeight1_f_32x4), vcgeq_f32(inputPositionsY4567_f_32x4, constantZero_f_32x4));
5111
5112 const uint32x4_t validPixels0123_u_32x4 = vandq_u32(validPixelsX0123_u_32x4, validPixelsY0123_u_32x4); // is_inside_input_frame(inputPosition) ? 0xFFFFFF : 0x000000
5113 const uint32x4_t validPixels4567_u_32x4 = vandq_u32(validPixelsX4567_u_32x4, validPixelsY4567_u_32x4);
5114
5115 vst1q_u32(validPixels + 0, validPixels0123_u_32x4);
5116 vst1q_u32(validPixels + 4, validPixels4567_u_32x4);
5117
5118
5119 const uint32x4_t inputPositionsLeft0123_u_32x4 = vcvtq_u32_f32(inputPositionsX0123_f_32x4);
5120 const uint32x4_t inputPositionsLeft4567_u_32x4 = vcvtq_u32_f32(inputPositionsX4567_f_32x4);
5121
5122 const uint32x4_t inputPositionsTop0123_u_32x4 = vcvtq_u32_f32(inputPositionsY0123_f_32x4);
5123 const uint32x4_t inputPositionsTop4567_u_32x4 = vcvtq_u32_f32(inputPositionsY4567_f_32x4);
5124
5125 const uint32x4_t inputPositionsBottom0123_u_32x4 = vminq_u32(vaddq_u32(inputPositionsTop0123_u_32x4, constantOne_u_32x4), constantInputHeight1_u_32x4);
5126 const uint32x4_t inputPositionsBottom4567_u_32x4 = vminq_u32(vaddq_u32(inputPositionsTop4567_u_32x4, constantOne_u_32x4), constantInputHeight1_u_32x4);
5127
5128
5129 const uint32x4_t topLeftOffsetsElements0123_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsLeft0123_u_32x4, constantChannels_u_32x4), inputPositionsTop0123_u_32x4, constantInputStrideElements_u_32x4); // topLeftOffset = top * strideElements + left * channels
5130 vst1q_u32(topLeftOffsetsElements + 0, topLeftOffsetsElements0123_u_32x4);
5131 const uint32x4_t topLeftOffsetsElements4567_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsLeft4567_u_32x4, constantChannels_u_32x4), inputPositionsTop4567_u_32x4, constantInputStrideElements_u_32x4);
5132 vst1q_u32(topLeftOffsetsElements + 4, topLeftOffsetsElements4567_u_32x4);
5133
5134 const uint32x4_t bottomLeftOffsetsElements0123_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsLeft0123_u_32x4, constantChannels_u_32x4), inputPositionsBottom0123_u_32x4, constantInputStrideElements_u_32x4);
5135 vst1q_u32(bottomLeftOffsetsElements + 0, bottomLeftOffsetsElements0123_u_32x4);
5136 const uint32x4_t bottomLeftOffsetsElements4567_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsLeft4567_u_32x4, constantChannels_u_32x4), inputPositionsBottom4567_u_32x4, constantInputStrideElements_u_32x4);
5137 vst1q_u32(bottomLeftOffsetsElements + 4, bottomLeftOffsetsElements4567_u_32x4);
5138
5139
5140 // we determine the fractional portions of the x' and y' and [0.0, 1.0] -> [0, 128]
5141 float32x4_t tx0123_f_32x4 = vmulq_f32(vsubq_f32(inputPositionsX0123_f_32x4, vcvtq_f32_u32(inputPositionsLeft0123_u_32x4)), constant128_f_32x4);
5142 float32x4_t tx4567_f_32x4 = vmulq_f32(vsubq_f32(inputPositionsX4567_f_32x4, vcvtq_f32_u32(inputPositionsLeft4567_u_32x4)), constant128_f_32x4);
5143
5144 float32x4_t ty0123_f_32x4 = vmulq_f32(vsubq_f32(inputPositionsY0123_f_32x4, vcvtq_f32_u32(inputPositionsTop0123_u_32x4)), constant128_f_32x4);
5145 float32x4_t ty4567_f_32x4 = vmulq_f32(vsubq_f32(inputPositionsY4567_f_32x4, vcvtq_f32_u32(inputPositionsTop4567_u_32x4)), constant128_f_32x4);
5146
5147 const uint32x4_t tx0123_128_u_32x4 = vcvtq_u32_f32(vaddq_f32(tx0123_f_32x4, vdupq_n_f32(0.5)));
5148 const uint32x4_t tx4567_128_u_32x4 = vcvtq_u32_f32(vaddq_f32(tx4567_f_32x4, vdupq_n_f32(0.5)));
5149
5150 const uint32x4_t ty0123_128_u_32x4 = vcvtq_u32_f32(vaddq_f32(ty0123_f_32x4, vdupq_n_f32(0.5)));
5151 const uint32x4_t ty4567_128_u_32x4 = vcvtq_u32_f32(vaddq_f32(ty4567_f_32x4, vdupq_n_f32(0.5)));
5152
5153 const uint16x8_t tx01234567_128_u_16x8 = vcombine_u16(vmovn_u32(tx0123_128_u_32x4), vmovn_u32(tx4567_128_u_32x4));
5154 const uint16x8_t ty01234567_128_u_16x8 = vcombine_u16(vmovn_u32(ty0123_128_u_32x4), vmovn_u32(ty4567_128_u_32x4));
5155
5156 const uint8x16_t tx_ty_128_u_8x16 = vcombine_u8(vmovn_u16(tx01234567_128_u_16x8), vmovn_u16(ty01234567_128_u_16x8));
5157
5158
5159 vst1q_u8(pixels + 0, constantBorderColor_u_8x16); // initialize with border color
5160 vst1q_u8(pixels + 16, constantBorderColor_u_8x16);
5161
5162 struct LeftRightPixel
5163 {
5164 uint8_t left;
5165 uint8_t right;
5166 };
5167
5168 static_assert(sizeof(LeftRightPixel) == 2, "Invalid data type!");
5169
5170 // we gather the individual source pixel values from the source image,
5171 // based on the calculated pixel locations
5172 for (unsigned int i = 0u; i < 8u; ++i)
5173 {
5174 if (validPixels[i])
5175 {
5176 ocean_assert((topLeftOffsetsElements[i] % inputStrideElements) < inputWidth - 1u); // we need to have one additional pixel to the right (as we copy two pixels at once)
5177 ocean_assert((bottomLeftOffsetsElements[i] % inputStrideElements) < inputWidth - 1u);
5178
5179 ((LeftRightPixel*)pixels)[0u + i] = *(LeftRightPixel*)(input + topLeftOffsetsElements[i]);
5180 ((LeftRightPixel*)pixels)[8u + i] = *(LeftRightPixel*)(input + bottomLeftOffsetsElements[i]);
5181 }
5182 }
5183
5184 const uint8x8x2_t topLeft_topRight_u_8x8x2 = vld2_u8(pixels);
5185 const uint8x8x2_t bottomLeft_bottomRight_u_8x8x2 = vld2_u8(pixels + 16);
5186
5187 interpolate8Pixels1Channel8BitNEON(topLeft_topRight_u_8x8x2.val[0], topLeft_topRight_u_8x8x2.val[1], bottomLeft_bottomRight_u_8x8x2.val[0], bottomLeft_bottomRight_u_8x8x2.val[1], tx_ty_128_u_8x16, outputPixelData);
5188
5189 outputPixelData += 8;
5190 }
5191 }
5192}
5193
5194template <unsigned int tChannels>
5195void FrameInterpolatorBilinear::lookup8BitPerChannelSubsetNEON(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable* input_LT_output, const bool offset, const uint8_t* borderColor, uint8_t* output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows, const bool useOptimizedNEON, const bool useOptimizedBilinearValuesAndFactorCalculation, const bool useOptimizedNEONFactorReplication, [[maybe_unused]] const bool use4ChannelSamplingFastPaths)
5196{
5197 ocean_assert(input_LT_output != nullptr);
5198 ocean_assert(input != nullptr && output != nullptr);
5199
5200 ocean_assert(inputWidth != 0u && inputHeight != 0u);
5201 ocean_assert(firstRow + numberRows <= input_LT_output->sizeY());
5202
5203 using PixelType = typename DataType<uint8_t, tChannels>::Type;
5204
5205 const uint8_t zeroColor[tChannels] = {uint8_t(0)};
5206 const PixelType* const bColor = borderColor ? (PixelType*)borderColor : (PixelType*)zeroColor;
5207
5208 // sizeX() is a size_t, but everything it feeds here is unsigned int: the output
5209 // stride, the loop counters, and Ocean's frame API. So narrow it once, and verify
5210 // the cast round-trips - bilinearValues() always fills the full sizeX(), so a
5211 // truncated count would size the row lookup buffer too small.
5212 const unsigned int outputWidth = (unsigned int)(input_LT_output->sizeX());
5213
5214 if (size_t(outputWidth) != input_LT_output->sizeX())
5215 {
5216 ocean_assert(false && "Lookup table is wider than 32 bits!");
5217 return;
5218 }
5219
5220 ocean_assert(outputWidth >= 4u);
5221
5222 static_assert(std::is_same<Vector2, LookupTable::Type>::value, "Invalid data type!");
5223
5224 const unsigned int inputStrideElements = inputWidth * tChannels + inputPaddingElements;
5225 const unsigned int outputStrideElements = outputWidth * tChannels + outputPaddingElements;
5226
5227 Memory rowLookupMemory = Memory::create<VectorF2>(outputWidth);
5228 VectorF2* const rowLookupData = rowLookupMemory.data<VectorF2>();
5229
5230 if (rowLookupData == nullptr)
5231 {
5232 // Memory leaves the buffer nullptr if the allocation fails, while
5233 // bilinearValues() below writes values[0] unconditionally
5234 ocean_assert(false && "Failed to allocate the row lookup buffer!");
5235 return;
5236 }
5237
5238 const float32x4_t constantZero_f_32x4 = vdupq_n_f32(0.0f); // [0.0f, 0.0f, 0.0f, 0.0f]
5239 const float32x4_t constantFour_f_32x4 = vdupq_n_f32(4.0f); // [4.0f, 4.0f, 4.0f, 4.0f]
5240
5241 // [0.0f, 1.0f, 2.0f, 3.0f]
5242 const float f_0123[4] = {0.0f, 1.0f, 2.0f, 3.0f};
5243 float32x4_t conststant0123_f_32x4 = vld1q_f32(f_0123);
5244
5245 const uint32x4_t constantOne_u_32x4 = vdupq_n_u32(1u);
5246
5247 const uint32x4_t constantChannels_u_32x4 = vdupq_n_u32(tChannels);
5248
5249 const float32x4_t constantInputWidth1_f_32x4 = vdupq_n_f32(float(inputWidth - 1u));
5250 const float32x4_t constantInputHeight1_f_32x4 = vdupq_n_f32(float(inputHeight - 1u));
5251
5252#if defined(__aarch64__)
5253 const float32x4_t constant128_f_32x4 = vdupq_n_f32(128.0f);
5254#endif
5255
5256 const uint32x4_t constantInputStrideElements_u_32x4 = vdupq_n_u32(inputStrideElements);
5257 const uint32x4_t constantInputWidth1_u_32x4 = vdupq_n_u32(inputWidth - 1u);
5258 const uint32x4_t constantInputHeight1_u_32x4 = vdupq_n_u32(inputHeight - 1u);
5259
5260 unsigned int validPixels[4];
5261
5262 unsigned int topLeftOffsetsElements[4];
5263 unsigned int topRightOffsetsElements[4];
5264 unsigned int bottomLeftOffsetsElements[4];
5265 unsigned int bottomRightOffsetsElements[4];
5266
5267 for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
5268 {
5269 PixelType* outputPixelData = (PixelType*)(output + y * outputStrideElements);
5270
5271 input_LT_output->bilinearValues<VectorF2>(y, rowLookupData, useOptimizedBilinearValuesAndFactorCalculation);
5272
5273 float32x4_t additionalInputOffsetX_f_32x4 = conststant0123_f_32x4;
5274 const float32x4_t additionalInputOffsetY_f_32x4 = vdupq_n_f32(float(y));
5275
5276 for (unsigned int x = 0u; x < outputWidth; x += 4u)
5277 {
5278 if (x + 4u > outputWidth)
5279 {
5280 // the last iteration will not fit into the output frame,
5281 // so we simply shift x left by some pixels (at most 3) and we will calculate some pixels again
5282
5283 ocean_assert(x >= 4u && outputWidth > 4u);
5284 const unsigned int newX = outputWidth - 4u;
5285
5286 ocean_assert(x > newX);
5287 const unsigned int xOffset = x - newX;
5288
5289 outputPixelData -= xOffset;
5290
5291 if (offset)
5292 {
5293 additionalInputOffsetX_f_32x4 = vsubq_f32(additionalInputOffsetX_f_32x4, vdupq_n_f32(float(xOffset)));
5294 }
5295
5296 x = newX;
5297
5298 // the for loop will stop after this iteration
5299 ocean_assert(!(x + 4u < outputWidth));
5300 }
5301
5302 const float32x4x2_t inputPositions_f_32x4x2 = vld2q_f32((const float*)(rowLookupData + x));
5303
5304 float32x4_t inputPositionsX_f_32x4 = inputPositions_f_32x4x2.val[0];
5305 float32x4_t inputPositionsY_f_32x4 = inputPositions_f_32x4x2.val[1];
5306
5307 if (offset)
5308 {
5309 inputPositionsX_f_32x4 = vaddq_f32(inputPositionsX_f_32x4, additionalInputOffsetX_f_32x4);
5310 inputPositionsY_f_32x4 = vaddq_f32(inputPositionsY_f_32x4, additionalInputOffsetY_f_32x4);
5311
5312 additionalInputOffsetX_f_32x4 = vaddq_f32(additionalInputOffsetX_f_32x4, constantFour_f_32x4);
5313 }
5314
5315 // now we check whether we are inside the input frame
5316 const uint32x4_t validPixelsX_u_32x4 = vandq_u32(vcleq_f32(inputPositionsX_f_32x4, constantInputWidth1_f_32x4), vcgeq_f32(inputPositionsX_f_32x4, constantZero_f_32x4)); // inputPosition.x() >= 0 && inputPosition.x() <= (inputWidth - 1) ? 0xFFFFFF : 0x000000
5317 const uint32x4_t validPixelsY_u_32x4 = vandq_u32(vcleq_f32(inputPositionsY_f_32x4, constantInputHeight1_f_32x4), vcgeq_f32(inputPositionsY_f_32x4, constantZero_f_32x4)); // inputPosition.y() >= 0 && inputPosition.y() <= (inputHeight - 1) ? 0xFFFFFF : 0x000000
5318
5319 const uint32x4_t validPixels_u_32x4 = vandq_u32(validPixelsX_u_32x4, validPixelsY_u_32x4); // is_inside_input_frame(inputPosition) ? 0xFFFFFF : 0x000000
5320
5321 vst1q_u32(validPixels, validPixels_u_32x4);
5322
5323 const uint32x4_t inputPositionsLeft_u_32x4 = vcvtq_u32_f32(inputPositionsX_f_32x4);
5324 const uint32x4_t inputPositionsTop_u_32x4 = vcvtq_u32_f32(inputPositionsY_f_32x4);
5325
5326 const uint32x4_t inputPositionsRight_u_32x4 = vminq_u32(vaddq_u32(inputPositionsLeft_u_32x4, constantOne_u_32x4), constantInputWidth1_u_32x4);
5327 const uint32x4_t inputPositionsBottom_u_32x4 = vminq_u32(vaddq_u32(inputPositionsTop_u_32x4, constantOne_u_32x4), constantInputHeight1_u_32x4);
5328
5329 const uint32x4_t topLeftOffsetsElements_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsLeft_u_32x4, constantChannels_u_32x4), inputPositionsTop_u_32x4, constantInputStrideElements_u_32x4);
5330 const uint32x4_t bottomLeftOffsetsElements_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsLeft_u_32x4, constantChannels_u_32x4), inputPositionsBottom_u_32x4, constantInputStrideElements_u_32x4);
5331
5332 // we determine the fractional portions of the x' and y':
5333 uint32x4_t tx_128_u_32x4;
5334 uint32x4_t ty_128_u_32x4;
5335
5336#if defined(__aarch64__)
5337 // vrndmq_f32 / vcvtaq_u32_f32 are AArch64-only intrinsics; ARMv7 falls through to the original sequence.
5338 if (useOptimizedBilinearValuesAndFactorCalculation)
5339 {
5340 // Compute fractional part independently of left/top, allowing parallel execution with bounds
5341 const float32x4_t tx_f_32x4 = vmulq_f32(vsubq_f32(inputPositionsX_f_32x4, vrndmq_f32(inputPositionsX_f_32x4)), constant128_f_32x4);
5342 const float32x4_t ty_f_32x4 = vmulq_f32(vsubq_f32(inputPositionsY_f_32x4, vrndmq_f32(inputPositionsY_f_32x4)), constant128_f_32x4);
5343
5344 tx_128_u_32x4 = vcvtaq_u32_f32(tx_f_32x4);
5345 ty_128_u_32x4 = vcvtaq_u32_f32(ty_f_32x4);
5346 }
5347 else
5348#endif
5349 {
5350 // Original: subtract, scale, round
5351 float32x4_t tx_f_32x4 = vsubq_f32(inputPositionsX_f_32x4, vcvtq_f32_u32(inputPositionsLeft_u_32x4));
5352 float32x4_t ty_f_32x4 = vsubq_f32(inputPositionsY_f_32x4, vcvtq_f32_u32(inputPositionsTop_u_32x4));
5353
5354 // we use integer interpolation [0.0, 1.0] -> [0, 128]
5355 tx_f_32x4 = vmulq_f32(tx_f_32x4, vdupq_n_f32(128.0f));
5356 ty_f_32x4 = vmulq_f32(ty_f_32x4, vdupq_n_f32(128.0f));
5357
5358 tx_128_u_32x4 = vcvtq_u32_f32(vaddq_f32(tx_f_32x4, vdupq_n_f32(0.5)));
5359 ty_128_u_32x4 = vcvtq_u32_f32(vaddq_f32(ty_f_32x4, vdupq_n_f32(0.5)));
5360 }
5361
5362 if constexpr (tChannels == 4u)
5363 {
5364 if (useOptimizedNEON)
5365 {
5366#if defined(__aarch64__) && defined(__APPLE__)
5367 if (use4ChannelSamplingFastPaths)
5368 {
5369 const uint32x4_t pairValid_u_32x4 = vandq_u32(validPixels_u_32x4, vcltq_u32(inputPositionsLeft_u_32x4, constantInputWidth1_u_32x4));
5370 if (vminvq_u32(pairValid_u_32x4) == 0xffffffffu)
5371 {
5372 const auto loadPair = [](const uint8_t* p) -> uint64_t
5373 {
5374 uint64_t value;
5375 __builtin_memcpy(&value, p, sizeof(value));
5376 return value;
5377 };
5378 const uint64x2_t top01_u_64x2 = {loadPair(input + vgetq_lane_u32(topLeftOffsetsElements_u_32x4, 0)), loadPair(input + vgetq_lane_u32(topLeftOffsetsElements_u_32x4, 1))};
5379 const uint64x2_t top23_u_64x2 = {loadPair(input + vgetq_lane_u32(topLeftOffsetsElements_u_32x4, 2)), loadPair(input + vgetq_lane_u32(topLeftOffsetsElements_u_32x4, 3))};
5380 const uint64x2_t bottom01_u_64x2 = {loadPair(input + vgetq_lane_u32(bottomLeftOffsetsElements_u_32x4, 0)), loadPair(input + vgetq_lane_u32(bottomLeftOffsetsElements_u_32x4, 1))};
5381 const uint64x2_t bottom23_u_64x2 = {loadPair(input + vgetq_lane_u32(bottomLeftOffsetsElements_u_32x4, 2)), loadPair(input + vgetq_lane_u32(bottomLeftOffsetsElements_u_32x4, 3))};
5382 const uint8x16_t topLeftPixels_u_8x16 = vreinterpretq_u8_u32(vuzp1q_u32(vreinterpretq_u32_u64(top01_u_64x2), vreinterpretq_u32_u64(top23_u_64x2)));
5383 const uint8x16_t topRightPixels_u_8x16 = vreinterpretq_u8_u32(vuzp2q_u32(vreinterpretq_u32_u64(top01_u_64x2), vreinterpretq_u32_u64(top23_u_64x2)));
5384 const uint8x16_t bottomLeftPixels_u_8x16 = vreinterpretq_u8_u32(vuzp1q_u32(vreinterpretq_u32_u64(bottom01_u_64x2), vreinterpretq_u32_u64(bottom23_u_64x2)));
5385 const uint8x16_t bottomRightPixels_u_8x16 = vreinterpretq_u8_u32(vuzp2q_u32(vreinterpretq_u32_u64(bottom01_u_64x2), vreinterpretq_u32_u64(bottom23_u_64x2)));
5386 interpolate4Pixels4Channel8BitPerChannelNEON(topLeftPixels_u_8x16, topRightPixels_u_8x16, bottomLeftPixels_u_8x16, bottomRightPixels_u_8x16, tx_128_u_32x4, ty_128_u_32x4, outputPixelData, useOptimizedNEONFactorReplication);
5387 outputPixelData += 4;
5388 continue;
5389 }
5390 if (borderColor == nullptr && vmaxvq_u32(validPixels_u_32x4) == 0u)
5391 {
5392 static_assert(sizeof(PixelType) == 4u, "Invalid pixel size!");
5393 vst1q_u8((uint8_t*)outputPixelData, vdupq_n_u8(0u));
5394 outputPixelData += 4;
5395 continue;
5396 }
5397 }
5398#endif
5399
5400 // Optimized 4-channel path: inline pixel gather + widening-multiply interpolation.
5401 // Eliminates NEON->stack->scalar->stack->NEON roundtrip by extracting offsets
5402 // directly from NEON registers via vgetq_lane_u32 (AArch64 umov).
5403
5404 const uint32x4_t topRightOffsetsElements_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsRight_u_32x4, constantChannels_u_32x4), inputPositionsTop_u_32x4, constantInputStrideElements_u_32x4);
5405 const uint32x4_t bottomRightOffsetsElements_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsRight_u_32x4, constantChannels_u_32x4), inputPositionsBottom_u_32x4, constantInputStrideElements_u_32x4);
5406
5407 PixelType topLeftPixels[4];
5408 PixelType topRightPixels[4];
5409 PixelType bottomLeftPixels[4];
5410 PixelType bottomRightPixels[4];
5411
5412 topLeftPixels[0] = validPixels[0] ? *((const PixelType*)(input + vgetq_lane_u32(topLeftOffsetsElements_u_32x4, 0))) : *bColor;
5413 topLeftPixels[1] = validPixels[1] ? *((const PixelType*)(input + vgetq_lane_u32(topLeftOffsetsElements_u_32x4, 1))) : *bColor;
5414 topLeftPixels[2] = validPixels[2] ? *((const PixelType*)(input + vgetq_lane_u32(topLeftOffsetsElements_u_32x4, 2))) : *bColor;
5415 topLeftPixels[3] = validPixels[3] ? *((const PixelType*)(input + vgetq_lane_u32(topLeftOffsetsElements_u_32x4, 3))) : *bColor;
5416
5417 topRightPixels[0] = validPixels[0] ? *((const PixelType*)(input + vgetq_lane_u32(topRightOffsetsElements_u_32x4, 0))) : *bColor;
5418 topRightPixels[1] = validPixels[1] ? *((const PixelType*)(input + vgetq_lane_u32(topRightOffsetsElements_u_32x4, 1))) : *bColor;
5419 topRightPixels[2] = validPixels[2] ? *((const PixelType*)(input + vgetq_lane_u32(topRightOffsetsElements_u_32x4, 2))) : *bColor;
5420 topRightPixels[3] = validPixels[3] ? *((const PixelType*)(input + vgetq_lane_u32(topRightOffsetsElements_u_32x4, 3))) : *bColor;
5421
5422 bottomLeftPixels[0] = validPixels[0] ? *((const PixelType*)(input + vgetq_lane_u32(bottomLeftOffsetsElements_u_32x4, 0))) : *bColor;
5423 bottomLeftPixels[1] = validPixels[1] ? *((const PixelType*)(input + vgetq_lane_u32(bottomLeftOffsetsElements_u_32x4, 1))) : *bColor;
5424 bottomLeftPixels[2] = validPixels[2] ? *((const PixelType*)(input + vgetq_lane_u32(bottomLeftOffsetsElements_u_32x4, 2))) : *bColor;
5425 bottomLeftPixels[3] = validPixels[3] ? *((const PixelType*)(input + vgetq_lane_u32(bottomLeftOffsetsElements_u_32x4, 3))) : *bColor;
5426
5427 bottomRightPixels[0] = validPixels[0] ? *((const PixelType*)(input + vgetq_lane_u32(bottomRightOffsetsElements_u_32x4, 0))) : *bColor;
5428 bottomRightPixels[1] = validPixels[1] ? *((const PixelType*)(input + vgetq_lane_u32(bottomRightOffsetsElements_u_32x4, 1))) : *bColor;
5429 bottomRightPixels[2] = validPixels[2] ? *((const PixelType*)(input + vgetq_lane_u32(bottomRightOffsetsElements_u_32x4, 2))) : *bColor;
5430 bottomRightPixels[3] = validPixels[3] ? *((const PixelType*)(input + vgetq_lane_u32(bottomRightOffsetsElements_u_32x4, 3))) : *bColor;
5431
5432 const uint8x16_t topLeftPixels_u8x16 = vld1q_u8((const uint8_t*)topLeftPixels);
5433 const uint8x16_t topRightPixels_u8x16 = vld1q_u8((const uint8_t*)topRightPixels);
5434 const uint8x16_t bottomLeftPixels_u8x16 = vld1q_u8((const uint8_t*)bottomLeftPixels);
5435 const uint8x16_t bottomRightPixels_u8x16 = vld1q_u8((const uint8_t*)bottomRightPixels);
5436
5437 interpolate4Pixels4Channel8BitPerChannelNEON(topLeftPixels_u8x16, topRightPixels_u8x16, bottomLeftPixels_u8x16, bottomRightPixels_u8x16, tx_128_u_32x4, ty_128_u_32x4, outputPixelData, useOptimizedNEONFactorReplication);
5438 }
5439 else
5440 {
5441 const uint32x4_t topRightOffsetsElements_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsRight_u_32x4, constantChannels_u_32x4), inputPositionsTop_u_32x4, constantInputStrideElements_u_32x4);
5442 const uint32x4_t bottomRightOffsetsElements_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsRight_u_32x4, constantChannels_u_32x4), inputPositionsBottom_u_32x4, constantInputStrideElements_u_32x4);
5443
5444 vst1q_u32(topLeftOffsetsElements, topLeftOffsetsElements_u_32x4);
5445 vst1q_u32(topRightOffsetsElements, topRightOffsetsElements_u_32x4);
5446 vst1q_u32(bottomLeftOffsetsElements, bottomLeftOffsetsElements_u_32x4);
5447 vst1q_u32(bottomRightOffsetsElements, bottomRightOffsetsElements_u_32x4);
5448
5449 interpolate4Pixels8BitPerChannelNEON<tChannels>(input, topLeftOffsetsElements, topRightOffsetsElements, bottomLeftOffsetsElements, bottomRightOffsetsElements, validPixels, *bColor, tx_128_u_32x4, ty_128_u_32x4, outputPixelData);
5450 }
5451 }
5452 else
5453 {
5454 const uint32x4_t topRightOffsetsElements_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsRight_u_32x4, constantChannels_u_32x4), inputPositionsTop_u_32x4, constantInputStrideElements_u_32x4);
5455 const uint32x4_t bottomRightOffsetsElements_u_32x4 = vmlaq_u32(vmulq_u32(inputPositionsRight_u_32x4, constantChannels_u_32x4), inputPositionsBottom_u_32x4, constantInputStrideElements_u_32x4);
5456
5457 vst1q_u32(topLeftOffsetsElements, topLeftOffsetsElements_u_32x4);
5458 vst1q_u32(topRightOffsetsElements, topRightOffsetsElements_u_32x4);
5459 vst1q_u32(bottomLeftOffsetsElements, bottomLeftOffsetsElements_u_32x4);
5460 vst1q_u32(bottomRightOffsetsElements, bottomRightOffsetsElements_u_32x4);
5461
5462 interpolate4Pixels8BitPerChannelNEON<tChannels>(input, topLeftOffsetsElements, topRightOffsetsElements, bottomLeftOffsetsElements, bottomRightOffsetsElements, validPixels, *bColor, tx_128_u_32x4, ty_128_u_32x4, outputPixelData);
5463 }
5464
5465 outputPixelData += 4;
5466 }
5467 }
5468}
5469
5470#endif // defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
5471
5472template <unsigned int tChannels>
5473void FrameInterpolatorBilinear::lookupMask8BitPerChannelSubset(const uint8_t* input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable* input_LT_output, const bool offset, uint8_t* output, uint8_t* outputMask, const uint8_t maskValue, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, const unsigned int firstRow, const unsigned int numberRows)
5474{
5475 ocean_assert(input_LT_output != nullptr);
5476 ocean_assert(input != nullptr && output != nullptr);
5477
5478 ocean_assert(inputWidth != 0u && inputHeight != 0u);
5479 ocean_assert(firstRow + numberRows <= input_LT_output->sizeY());
5480
5481 using PixelType = typename DataType<uint8_t, tChannels>::Type;
5482
5483 // sizeX() is a size_t, but everything it feeds here is unsigned int: the output
5484 // stride, the loop counters, and Ocean's frame API. So narrow it once, and verify
5485 // the cast round-trips - bilinearValues() always fills the full sizeX(), so a
5486 // truncated count would size the row lookup buffer too small.
5487 const unsigned int columns = (unsigned int)(input_LT_output->sizeX());
5488
5489 if (size_t(columns) != input_LT_output->sizeX())
5490 {
5491 ocean_assert(false && "Lookup table is wider than 32 bits!");
5492 return;
5493 }
5494
5495 const unsigned int outputStrideElements = tChannels * columns + outputPaddingElements;
5496 const unsigned int outputMaskStrideElements = columns + outputMaskPaddingElements;
5497
5498 static_assert(std::is_same<Vector2, LookupTable::Type>::value, "Invalid data type!");
5499
5500 const Scalar inputWidth1 = Scalar(inputWidth - 1u);
5501 const Scalar inputHeight1 = Scalar(inputHeight - 1u);
5502
5503 Memory rowLookupMemory = Memory::create<Vector2>(columns);
5504 Vector2* const rowLookupData = rowLookupMemory.data<Vector2>();
5505
5506 if (rowLookupData == nullptr)
5507 {
5508 // Memory leaves the buffer nullptr if the allocation fails, while
5509 // bilinearValues() below writes values[0] unconditionally
5510 ocean_assert(false && "Failed to allocate the row lookup buffer!");
5511 return;
5512 }
5513
5514 for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
5515 {
5516 input_LT_output->bilinearValues(y, rowLookupData);
5517
5518 PixelType* outputData = (PixelType*)(output + y * outputStrideElements);
5519 uint8_t* outputMaskData = outputMask + y * outputMaskStrideElements;
5520
5521 for (unsigned int x = 0u; x < columns; ++x)
5522 {
5523 const Vector2& lookupValue = rowLookupData[x];
5524
5525 const Vector2 inputPosition = offset ? Vector2(Scalar(x) + lookupValue.x(), Scalar(y) + lookupValue.y()) : lookupValue;
5526
5527 if (inputPosition.x() >= 0 && inputPosition.y() >= 0 && inputPosition.x() <= inputWidth1 && inputPosition.y() <= inputHeight1)
5528 {
5529 interpolatePixel8BitPerChannel<tChannels, PC_TOP_LEFT>(input, inputWidth, inputHeight, inputPaddingElements, inputPosition, (uint8_t*)(outputData));
5530 *outputMaskData = maskValue;
5531 }
5532 else
5533 {
5534 *outputMaskData = 0xFFu - maskValue;
5535 }
5536
5537 outputData++;
5538 outputMaskData++;
5539 }
5540 }
5541}
5542
5543template <unsigned int tChannels>
5544void FrameInterpolatorBilinear::scale8BitPerChannel(const uint8_t* source, uint8_t* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker)
5545{
5546 ocean_assert(source != nullptr && target != nullptr);
5547 ocean_assert(sourceWidth >= 1u && sourceHeight >= 1u);
5548 ocean_assert(targetWidth >= 1u && targetHeight >= 1u);
5549 ocean_assert(sourceX_s_targetX > 0.0);
5550 ocean_assert(sourceY_s_targetY > 0.0);
5551
5552 if (sourceWidth == targetWidth && sourceHeight == targetHeight && sourceX_s_targetX == 1.0 && sourceY_s_targetY == 1.0)
5553 {
5554 FrameConverter::subFrame<uint8_t>(source, target, sourceWidth, sourceHeight, targetWidth, targetHeight, tChannels, 0u, 0u, 0u, 0u, sourceWidth, sourceHeight, sourcePaddingElements, targetPaddingElements);
5555 return;
5556 }
5557
5558 if (worker && sourceWidth * tChannels >= 16u && targetWidth >= 8u)
5559 {
5560#if defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
5561 if (sourceWidth <= 65535u && sourceHeight <= 65535u && targetWidth <= 65535u && targetHeight <= 65535u)
5562 {
5563 worker->executeFunction(Worker::Function::createStatic(&scale8BitPerChannelSubset7BitPrecisionNEON, source, target, sourceWidth, sourceHeight, targetWidth, targetHeight, tChannels, sourceX_s_targetX, sourceY_s_targetY, sourcePaddingElements, targetPaddingElements, 0u, 0u), 0u, targetHeight);
5564 return;
5565 }
5566#else
5567 worker->executeFunction(Worker::Function::createStatic(&scale8BitPerChannelSubset<tChannels>, source, target, sourceWidth, sourceHeight, targetWidth, targetHeight, sourceX_s_targetX, sourceY_s_targetY, sourcePaddingElements, targetPaddingElements, 0u, 0u), 0u, targetHeight);
5568#endif
5569 }
5570 else
5571 {
5572 if (sourceWidth * tChannels >= 16u && targetWidth >= 8u)
5573 {
5574#if defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
5575 if (sourceWidth <= 65535u && sourceHeight <= 65535u && targetWidth <= 65535u && targetHeight <= 65535u)
5576 {
5577 scale8BitPerChannelSubset7BitPrecisionNEON(source, target, sourceWidth, sourceHeight, targetWidth, targetHeight, tChannels, sourceX_s_targetX, sourceY_s_targetY, sourcePaddingElements, targetPaddingElements, 0u, targetHeight);
5578 return;
5579 }
5580#endif
5581 }
5582
5583 scale8BitPerChannelSubset<tChannels>(source, target, sourceWidth, sourceHeight, targetWidth, targetHeight, sourceX_s_targetX, sourceY_s_targetY, sourcePaddingElements, targetPaddingElements, 0u, targetHeight);
5584 }
5585}
5586
5587template <unsigned int tChannels>
5588void FrameInterpolatorBilinear::scale8BitPerChannelSubset(const uint8_t* source, uint8_t* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
5589{
5590 ocean_assert(source != nullptr && target != nullptr);
5591 ocean_assert(sourceWidth != 0u && sourceHeight != 0u);
5592 ocean_assert_and_suppress_unused(targetWidth >= 1u && targetHeight >= 1u, targetHeight);
5593 ocean_assert(sourceX_s_targetX > 0.0 && sourceY_s_targetY > 0.0);
5594
5595 const Scalar sourceX_T_targetX = Scalar(sourceX_s_targetX);
5596 const Scalar sourceY_T_targetY = Scalar(sourceY_s_targetY);
5597
5598 /*
5599 * We determine the sub-pixel accurate source location for each target pixel as follows:
5600 *
5601 * Example with a downsampling by factor 4:
5602 * sourceRow with 12 pixels: | 0 1 2 3 4 5 6 7 8 9 A B |
5603 * targetRow with 3 pixels: | 0 1 2 |
5604 *
5605 * Thus, the source row can be separated into three blocks;
5606 * and we want to extract the color information from the center of the blocks:
5607 * sourceRow with 12 pixels: | 0 1 2 3 | 4 5 6 7 | 8 9 A B |
5608 * targetRow with 3 pixels: | 0 | 1 | 2 | (sourceX_s_targetX = 4)
5609 *
5610 * Thus, we add 0.5 to each target coordinate before converting it to a source location;
5611 * and subtract 0.5 again afterwards:
5612 * sourceX = (targetX + 0.5) * sourceX_s_targetX - 0.5
5613 *
5614 * e.g., (0 + 0.5) * 4 - 0.5 = 1.5
5615 * (1 + 0.5) * 4 - 0.5 = 5.5
5616 *
5617 *
5618 * Example with a downsampling by factor 3:
5619 * sourceRow with 9 pixels: | 0 1 2 3 4 5 6 7 8 |
5620 * targetRow with 3 pixels: | 0 1 2 |
5621 *
5622 * sourceRow with 9 pixels: | 0 1 2 | 3 4 5 | 6 7 8 |
5623 * targetRow with 3 pixels: | 0 | 1 | 2 | (sourceX_s_targetX = 3)
5624 *
5625 * e.g., (0 + 0.5) * 3 - 0.5 = 1
5626 * (1 + 0.5) * 3 - 0.5 = 4
5627 *
5628 *
5629 * Example with a downsampling by factor 2:
5630 * sourceRow with 6 pixels: | 0 1 2 3 4 5 |
5631 * targetRow with 3 pixels: | 0 1 2 |
5632 *
5633 * sourceRow with 6 pixels: | 0 1 | 2 3 | 4 5 |
5634 * targetRow with 3 pixels: | 0 | 1 | 2 | (sourceX_s_targetX = 2)
5635 *
5636 * e.g., (0 + 0.5) * 2 - 0.5 = 0.5
5637 * (1 + 0.5) * 2 - 0.5 = 2.5
5638 *
5639 *
5640 * we can simplify the calculation (as we have a constant term):
5641 * sourceX = (sourceX_s_targetX * targetX) + (sourceX_s_targetX * 0.5 - 0.5)
5642 */
5643
5644 const unsigned int sourceStrideElements = sourceWidth * tChannels + sourcePaddingElements;
5645
5646 const Scalar sourceX_T_targetXOffset = sourceX_T_targetX * Scalar(0.5) - Scalar(0.5);
5647 const Scalar sourceY_T_targetYOffset = sourceY_T_targetY * Scalar(0.5) - Scalar(0.5);
5648
5649 const Scalar sourceWidth_1 = Scalar(sourceWidth - 1u);
5650 const Scalar sourceHeight_1 = Scalar(sourceHeight - 1u);
5651
5652 target += (targetWidth * tChannels + targetPaddingElements) * firstTargetRow;
5653
5654 for (unsigned int y = firstTargetRow; y < firstTargetRow + numberTargetRows; ++y)
5655 {
5656 const Scalar sy = minmax(Scalar(0), sourceY_T_targetYOffset + sourceY_T_targetY * Scalar(y), sourceHeight_1);
5657 ocean_assert(sy >= Scalar(0) && sy < Scalar(sourceHeight));
5658
5659 const unsigned int sTop = (unsigned int)sy;
5660 ocean_assert(sy >= Scalar(sTop));
5661
5662 const Scalar ty = sy - Scalar(sTop);
5663 ocean_assert(ty >= 0 && ty <= 1);
5664
5665 const unsigned int factorBottom = (unsigned int)(ty * Scalar(128) + Scalar(0.5));
5666 const unsigned int factorTop = 128u - factorBottom;
5667
5668 const uint8_t* const sourceTop = source + sourceStrideElements * sTop;
5669 const uint8_t* const sourceBottom = (sTop + 1u < sourceHeight) ? sourceTop + sourceStrideElements : sourceTop;
5670
5671 for (unsigned int x = 0; x < targetWidth; ++x)
5672 {
5673 const Scalar sx = minmax(Scalar(0), sourceX_T_targetXOffset + sourceX_T_targetX * Scalar(x), sourceWidth_1);
5674 ocean_assert(sx >= Scalar(0) && sx < Scalar(sourceWidth));
5675
5676 const unsigned int sLeft = (unsigned int)sx;
5677 ocean_assert(sx >= Scalar(sLeft));
5678
5679 const Scalar tx = sx - Scalar(sLeft);
5680 ocean_assert(tx >= 0 && tx <= 1);
5681
5682 const unsigned int factorRight = (unsigned int)(tx * Scalar(128) + Scalar(0.5));
5683 const unsigned int factorLeft = 128u - factorRight;
5684
5685 const unsigned int sourceRightOffset = sLeft + 1u < sourceWidth ? tChannels : 0u;
5686
5687 const uint8_t* const sourceTopLeft = sourceTop + sLeft * tChannels;
5688 const uint8_t* const sourceBottomLeft = sourceBottom + sLeft * tChannels;
5689
5690 const unsigned int factorTopLeft = factorTop * factorLeft;
5691 const unsigned int factorTopRight = factorTop * factorRight;
5692 const unsigned int factorBottomLeft = factorBottom * factorLeft;
5693 const unsigned int factorBottomRight = factorBottom * factorRight;
5694
5695 for (unsigned int n = 0u; n < tChannels; ++n)
5696 {
5697 target[n] = (uint8_t)((sourceTopLeft[n] * factorTopLeft + sourceTopLeft[sourceRightOffset + n] * factorTopRight
5698 + sourceBottomLeft[n] * factorBottomLeft + sourceBottomLeft[sourceRightOffset + n] * factorBottomRight + 8192u) >> 14u);
5699 }
5700
5701 target += tChannels;
5702 }
5703
5704 target += targetPaddingElements;
5705 }
5706}
5707
5708template <typename T>
5709void FrameInterpolatorBilinear::interpolateRowVertical(const T* sourceRowTop, const T* sourceRowBottom, T* targetRow, const unsigned int elements, const float factorBottom)
5710{
5711 ocean_assert(sourceRowTop != nullptr);
5712 ocean_assert(sourceRowBottom != nullptr);
5713 ocean_assert(targetRow != nullptr);
5714 ocean_assert(elements >= 1u);
5715 ocean_assert(factorBottom >= 0.0f && factorBottom <= 1.0f);
5716
5717 using FloatType = typename FloatTyper<T>::Type;
5718
5719 const FloatType internalFactorBottom = FloatType(factorBottom);
5720 const FloatType internalFactorTop = FloatType(1.0f - factorBottom);
5721
5722 for (unsigned int n = 0u; n < elements; ++n)
5723 {
5724 targetRow[n] = T(FloatType(sourceRowTop[n]) * internalFactorTop + FloatType(sourceRowBottom[n]) * internalFactorBottom);
5725 }
5726}
5727
5728template <typename T, unsigned int tChannels>
5729void FrameInterpolatorBilinear::interpolateRowHorizontal(const T* extendedSourceRow, T* targetRow, const unsigned int targetWidth, const unsigned int channels, const unsigned int* interpolationLocations, const float* interpolationFactorsRight)
5730{
5731 static_assert(tChannels != 0u, "Invalid channel number!");
5732
5733 ocean_assert(extendedSourceRow != nullptr);
5734 ocean_assert(targetRow != nullptr);
5735 ocean_assert(targetWidth >= 1u);
5736 ocean_assert(interpolationLocations != nullptr);
5737 ocean_assert(interpolationFactorsRight != nullptr);
5738 ocean_assert_and_suppress_unused(channels == tChannels, channels);
5739
5740 using FloatType = typename FloatTyper<T>::Type;
5741
5742 for (unsigned int x = 0u; x < targetWidth; ++x)
5743 {
5744 const FloatType internalFactorRight = FloatType(interpolationFactorsRight[x]);
5745 ocean_assert(internalFactorRight >= FloatType(0) && internalFactorRight <= FloatType(1));
5746
5747 const FloatType internalFactorLeft = FloatType(1.0f - interpolationFactorsRight[x]);
5748
5749 const unsigned int& leftLocation = interpolationLocations[x];
5750 const unsigned int rightLocation = leftLocation + tChannels; // location is defined in relation to elements, not to pixels
5751
5752 for (unsigned int n = 0u; n < tChannels; ++n)
5753 {
5754 targetRow[x * tChannels + n] = T(FloatType(extendedSourceRow[leftLocation + n]) * internalFactorLeft + FloatType(extendedSourceRow[rightLocation + n]) * internalFactorRight);
5755 }
5756 }
5757}
5758
5759#if defined(OCEAN_HARDWARE_NEON_VERSION) && OCEAN_HARDWARE_NEON_VERSION >= 10
5760
5761#ifdef OCEAN_WE_KEEP_THIS_IMPLEMENTATION_AS_WE_NEED_THIS_TO_FOR_A_NEW_NEON_IMPLEMENTATION
5762
5763template <>
5764inline void FrameInterpolatorBilinear::scale8BitPerChannelSubset7BitPrecisionNEON<2u, 8u>(const uint8_t* source, uint8_t* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
5765{
5766 ocean_assert(source != nullptr && target != nullptr);
5767 ocean_assert(sourceWidth >= 2u && sourceWidth <= 65535u);
5768 ocean_assert(sourceHeight >= 1u && sourceHeight <= 65535u);
5769 ocean_assert(targetWidth >= 8u && targetWidth <= 65535u);
5770 ocean_assert(targetHeight >= 1u && targetHeight <= 65535u);
5771 ocean_assert(sourceX_s_targetX > 0.0 && sourceY_s_targetY > 0.0);
5772
5773 ocean_assert(sourcePaddingElements == 0u); // not supported
5774 ocean_assert(targetPaddingElements == 0u);
5775
5776 using PixelType = typename DataType<uint8_t, 2u>::Type;
5777
5778 PixelType* targetPixelData = (PixelType*)target + firstTargetRow * targetWidth;
5779 const PixelType* const sourcePixelData = (const PixelType*)source;
5780
5781 // our offset values for the eight left pixels in relation to the first pixel of the row
5782 unsigned int leftOffsets[8];
5783
5784 // this function uses fixed point numbers with 16 bit for the calculation of the interpolation positions and factors:
5785 // fixedPointLocation = floatLocation * 2^16
5786 //
5787 // [FEDCBA98, 76543210]
5788 // [pixel , subpixel]
5789 //
5790 // fixedPointLocation = pixel + subpixel / 2^16
5791 //
5792 // Thus, the upper 16 bit represent the location of e.g., the left pixel (for the linear interpolation)
5793 // while the lower 16 bit represent one of both interpolation factors (and 2^16 - subpixel represents the second interpolation factor)
5794
5795 const unsigned int sourceX_T_targetX_fixed16 = (unsigned int)(double(0x10000u) * sourceX_s_targetX + 0.5);
5796 const unsigned int sourceY_T_targetY_fixed16 = (unsigned int)(double(0x10000u) * sourceY_s_targetY + 0.5);
5797
5798 const int targetOffsetX_fixed16 = (int)(double(0x10000u) * ((sourceX_s_targetX * 0.5) - 0.5) + 0.5);
5799 const int targetOffsetY_fixed16 = (int)(double(0x10000u) * ((sourceY_s_targetY * 0.5) - 0.5) + 0.5);
5800
5801 // we store 4 integers: [sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16]
5802 const uint32x4_t m128_u_sourceX_T_targetX_fixed16 = vdupq_n_u32(sourceX_T_targetX_fixed16);
5803
5804 // we store 4 integers: [sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16]
5805 const int32x4_t m128_s_targetOffsetX_fixed16 = vdupq_n_s32(targetOffsetX_fixed16);
5806
5807 // we store 4 integers: [sourceWidth - 2, sourceWidth - 2, sourceWidth - 2, sourceWidth - 2]
5808 const uint32x4_t m128_u_sourceWidth_2 = vdupq_n_u32(sourceWidth - 2u);
5809
5810 // we store 4 integers: [0, 0, 0, 0]
5811 const int32x4_t m128_s_zero = vdupq_n_s32(0);
5812
5813 const unsigned int u_0123[4] = {0u, 1u, 2u, 3u};
5814 const uint32x4_t m128_u_0123 = vld1q_u32(u_0123);
5815
5816 for (unsigned int y = firstTargetRow; y < firstTargetRow + numberTargetRows; ++y)
5817 {
5818 const unsigned int sourceY_fixed16 = minmax<int>(0, targetOffsetY_fixed16 + int(sourceY_T_targetY_fixed16 * y), (sourceHeight - 1u) << 16u);
5819
5820 const unsigned int sourceRowTop = sourceY_fixed16 >> 16u; // we must not round here
5821 const unsigned int factorBottom_fixed16 = sourceY_fixed16 & 0x0000FFFFu;
5822 const unsigned int factorBottom = factorBottom_fixed16 >> 9u;
5823
5824 const uint8x8_t m64_u_factorsBottom = vdup_n_u8(factorBottom);
5825 // factorTop = 128 - factorBottom
5826 const uint8x8_t m64_u_factorsTop = vdup_n_u8(128u - factorBottom);
5827
5828 const unsigned int sourceRowBottom = min(sourceRowTop + 1u, sourceHeight - 1u);
5829
5830 const PixelType* const sourceTopRowPixelData = sourcePixelData + sourceRowTop * sourceWidth;
5831 const PixelType* const sourceBottomRowPixelData = sourcePixelData + sourceRowBottom * sourceWidth;
5832
5833 for (unsigned int x = 0; x < targetWidth; x += 8u)
5834 {
5835 if (x + 8u > targetWidth)
5836 {
5837 // the last iteration will not fit into the output frame,
5838 // so we simply shift x left by some pixels (at most 7) and we will calculate some pixels again
5839
5840 ocean_assert(x >= 8u && targetWidth > 8u);
5841 const unsigned int newX = targetWidth - 8u;
5842
5843 ocean_assert(x > newX);
5844 targetPixelData -= x - newX;
5845
5846 x = newX;
5847
5848 // the for loop will stop after this iteration
5849 ocean_assert(!(x + 8u < targetWidth));
5850 }
5851
5852
5853 // we need four successive x coordinate floats:
5854 // [x + 3, x + 2, x + 1; x + 0]
5855 const uint32x4_t m128_u_x_0123 = vaddq_u32(vdupq_n_u32(x), m128_u_0123);
5856 const uint32x4_t m128_u_x_4567 = vaddq_u32(vdupq_n_u32(x + 4u), m128_u_0123);
5857
5858 // we calculate the four source locations for our four target locations
5859 const int32x4_t m128_s_sourceX_0123_fixed16 = vmaxq_s32(m128_s_zero, vaddq_s32(m128_s_targetOffsetX_fixed16, vreinterpretq_s32_u32(vmulq_u32(m128_u_sourceX_T_targetX_fixed16, m128_u_x_0123))));
5860 const uint32x4_t m128_u_sourceX_0123_fixed16 = vreinterpretq_u32_s32(m128_s_sourceX_0123_fixed16);
5861
5862 const int32x4_t m128_s_sourceX_4567_fixed16 = vmaxq_s32(m128_s_zero, vaddq_s32(m128_s_targetOffsetX_fixed16, vreinterpretq_s32_u32(vmulq_u32(m128_u_sourceX_T_targetX_fixed16, m128_u_x_4567))));
5863 const uint32x4_t m128_u_sourceX_4567_fixed16 = vreinterpretq_u32_s32(m128_s_sourceX_4567_fixed16);
5864
5865 // now we determine the pixel/integer accurate source locations
5866 // m128_u_left = min(floor(m128_f_sourceX), sourceWidth - 2)
5867 const uint32x4_t m128_u_left_0123 = vminq_u32(vshrq_n_u32(m128_u_sourceX_0123_fixed16, 16), m128_u_sourceWidth_2); // not vrshrq_n_u32 as we must not round here
5868 const uint32x4_t m128_u_left_4567 = vminq_u32(vshrq_n_u32(m128_u_sourceX_4567_fixed16, 16), m128_u_sourceWidth_2);
5869
5870 // we store the offsets we have calculated
5871 vst1q_u32(leftOffsets + 0, m128_u_left_0123);
5872 vst1q_u32(leftOffsets + 4, m128_u_left_4567);
5873
5874
5875
5876 // we load the individal pixels to our four (de-interleaved) 8x8 bit registers (we do this for the top-left and top-right pixels)
5877 // note: loading of each pixel individually is significantly slower than loading two neighboring pixels within one iteration
5878
5879 uint8x8x2_t topLeftPixels;
5880 uint8x8x2_t topRightPixels;
5881
5882 topLeftPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[0u] + 0), topLeftPixels, 0);
5883 topRightPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[0u] + 1), topRightPixels, 0);
5884
5885 topLeftPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[1u] + 0), topLeftPixels, 1);
5886 topRightPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[1u] + 1), topRightPixels, 1);
5887
5888 topLeftPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[2u] + 0), topLeftPixels, 2);
5889 topRightPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[2u] + 1), topRightPixels, 2);
5890
5891 topLeftPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[3u] + 0), topLeftPixels, 3);
5892 topRightPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[3u] + 1), topRightPixels, 3);
5893
5894 topLeftPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[4u] + 0), topLeftPixels, 4);
5895 topRightPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[4u] + 1), topRightPixels, 4);
5896
5897 topLeftPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[5u] + 0), topLeftPixels, 5);
5898 topRightPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[5u] + 1), topRightPixels, 5);
5899
5900 topLeftPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[6u] + 0), topLeftPixels, 6);
5901 topRightPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[6u] + 1), topRightPixels, 6);
5902
5903 topLeftPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[7u] + 0), topLeftPixels, 7);
5904 topRightPixels = vld2_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[7u] + 1), topRightPixels, 7);
5905
5906
5907 // we load the individual pixels to our four (de-interleaved) 8x8 bit registers (we do this for the bottom-left and bottom-right pixels)
5908
5909 uint8x8x2_t bottomLeftPixels;
5910 uint8x8x2_t bottomRightPixels;
5911
5912 bottomLeftPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[0u] + 0), bottomLeftPixels, 0);
5913 bottomRightPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[0u] + 1), bottomRightPixels, 0);
5914
5915 bottomLeftPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[1u] + 0), bottomLeftPixels, 1);
5916 bottomRightPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[1u] + 1), bottomRightPixels, 1);
5917
5918 bottomLeftPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[2u] + 0), bottomLeftPixels, 2);
5919 bottomRightPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[2u] + 1), bottomRightPixels, 2);
5920
5921 bottomLeftPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[3u] + 0), bottomLeftPixels, 3);
5922 bottomRightPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[3u] + 1), bottomRightPixels, 3);
5923
5924 bottomLeftPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[4u] + 0), bottomLeftPixels, 4);
5925 bottomRightPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[4u] + 1), bottomRightPixels, 4);
5926
5927 bottomLeftPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[5u] + 0), bottomLeftPixels, 5);
5928 bottomRightPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[5u] + 1), bottomRightPixels, 5);
5929
5930 bottomLeftPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[6u] + 0), bottomLeftPixels, 6);
5931 bottomRightPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[6u] + 1), bottomRightPixels, 6);
5932
5933 bottomLeftPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[7u] + 0), bottomLeftPixels, 7);
5934 bottomRightPixels = vld2_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[7u] + 1), bottomRightPixels, 7);
5935
5936
5937
5938 // we determine the multiplication factors for the right pixels - which are already stored in the lower 16 bits
5939 // we need an accuracy of 7 bits (values between 0 and 128):
5940 // 76 54 32 10
5941 // [F3 F2 F1 F0]
5942 const uint16x4_t m64_u_factorsRight_0123 = vrshrn_n_u32(vandq_u32(m128_u_sourceX_0123_fixed16, vdupq_n_u32(0x0000FFFFu)), 9);
5943 const uint16x4_t m64_u_factorsRight_4567 = vrshrn_n_u32(vandq_u32(m128_u_sourceX_4567_fixed16, vdupq_n_u32(0x0000FFFFu)), 9);
5944
5945 // as we have the pixel information de-interleaved, we can store all 8 interpolation factors together into one 8x8 bit register:
5946 const uint16x8_t m128_u_factorsRight = vcombine_u16(m64_u_factorsRight_0123, m64_u_factorsRight_4567);
5947 const uint8x8_t m64_u_factorsRight = vqmovn_u16(m128_u_factorsRight);
5948 const uint8x8_t m64_u_factorsLeft = vsub_u8(vdup_n_u8(128u), m64_u_factorsRight);
5949
5950
5951
5952 // we determine the intermediate interpolation results for the top row (and we narrow down the 16 bit results 8 bit results)
5953 uint16x8_t m128_muliplicationChannel_0 = vmull_u8(topLeftPixels.val[0], m64_u_factorsLeft);
5954 uint16x8_t m128_muliplicationChannel_1 = vmull_u8(topLeftPixels.val[1], m64_u_factorsLeft);
5955
5956 m128_muliplicationChannel_0 = vmlal_u8(m128_muliplicationChannel_0, topRightPixels.val[0], m64_u_factorsRight);
5957 m128_muliplicationChannel_1 = vmlal_u8(m128_muliplicationChannel_1, topRightPixels.val[1], m64_u_factorsRight);
5958
5959 uint8x8_t m64_topRowChannel_0 = vrshrn_n_u16(m128_muliplicationChannel_0, 7);
5960 uint8x8_t m64_topRowChannel_1 = vrshrn_n_u16(m128_muliplicationChannel_1, 7);
5961
5962
5963
5964 // we determine the intermediate interpolation results for the bottom row (and we narrow down the 16 bit results 8 bit results)
5965 m128_muliplicationChannel_0 = vmull_u8(bottomLeftPixels.val[0], m64_u_factorsLeft);
5966 m128_muliplicationChannel_1 = vmull_u8(bottomLeftPixels.val[1], m64_u_factorsLeft);
5967
5968 m128_muliplicationChannel_0 = vmlal_u8(m128_muliplicationChannel_0, bottomRightPixels.val[0], m64_u_factorsRight);
5969 m128_muliplicationChannel_1 = vmlal_u8(m128_muliplicationChannel_1, bottomRightPixels.val[1], m64_u_factorsRight);
5970
5971 uint8x8_t m64_bottomRowChannel_0 = vrshrn_n_u16(m128_muliplicationChannel_0, 7);
5972 uint8x8_t m64_bottomRowChannel_1 = vrshrn_n_u16(m128_muliplicationChannel_1, 7);
5973
5974
5975
5976 // finnally we determine the interpolation result between top and bottom row
5977 m128_muliplicationChannel_0 = vmull_u8(m64_topRowChannel_0, m64_u_factorsTop);
5978 m128_muliplicationChannel_1 = vmull_u8(m64_topRowChannel_1, m64_u_factorsTop);
5979
5980 m128_muliplicationChannel_0 = vmlal_u8(m128_muliplicationChannel_0, m64_bottomRowChannel_0, m64_u_factorsBottom);
5981 m128_muliplicationChannel_1 = vmlal_u8(m128_muliplicationChannel_1, m64_bottomRowChannel_1, m64_u_factorsBottom);
5982
5983
5984 // we narrow down the interpolation results and we store them
5985 uint8x8x2_t result;
5986 result.val[0] = vrshrn_n_u16(m128_muliplicationChannel_0, 7);
5987 result.val[1] = vrshrn_n_u16(m128_muliplicationChannel_1, 7);
5988
5989 // we write back the results and interleave them automatically
5990 vst2_u8((uint8_t*)targetPixelData, result);
5991
5992 targetPixelData += 8;
5993 }
5994
5995 // we need to process the last pixel again, as this pixel may have received wrong interpolation factors as we always load two successive pixels into our NEON registers
5996 // **TODO** this is just a temporary solution, check how we can avoid this additional step
5997
5998 const unsigned int firstInvalidTargetX = (((sourceWidth - 1u) << 16u) - targetOffsetX_fixed16) / sourceX_T_targetX_fixed16;
5999
6000 for (unsigned int x = firstInvalidTargetX; x < targetWidth; ++x)
6001 {
6002 const unsigned int lastSourcePixelPosition_fixed16 = minmax<int>(0, targetOffsetX_fixed16 + int(sourceX_T_targetX_fixed16 * x), (sourceWidth - 1u) << 16u);
6003
6004 const unsigned int lastSourcePixelLeft = lastSourcePixelPosition_fixed16 >> 16u;
6005 ocean_assert(lastSourcePixelLeft < sourceWidth);
6006 const unsigned int lastSourcePixelRight = min(lastSourcePixelLeft + 1u, sourceWidth - 1u);
6007
6008 const unsigned int factorRight_fixed16 = lastSourcePixelPosition_fixed16 & 0x0000FFFFu;
6009
6010 const unsigned int factorRight = factorRight_fixed16 >> 9u;
6011 const unsigned int factorLeft = 128u - factorRight;
6012
6013 for (unsigned int c = 0u; c < 2u; ++c)
6014 {
6015 ((uint8_t*)(targetPixelData - (targetWidth - x)))[c] = ((((const uint8_t*)(sourceTopRowPixelData + lastSourcePixelLeft))[c] * factorLeft + ((const uint8_t*)(sourceTopRowPixelData + lastSourcePixelRight))[c] * factorRight) * (128u - factorBottom)
6016 + (((const uint8_t*)(sourceBottomRowPixelData + lastSourcePixelLeft))[c] * factorLeft + ((const uint8_t*)(sourceBottomRowPixelData + lastSourcePixelRight))[c] * factorRight) * factorBottom + 8192u) >> 14u;
6017 }
6018 }
6019 }
6020}
6021
6022#endif // OCEAN_WE_KEEP_THIS_IMPLEMENTATION_AS_WE_NEED_THIS_TO_FOR_A_NEW_NEON_IMPLEMENTATION
6023
6024#ifdef OCEAN_WE_KEEP_ALSO_THIS_SLOW_IMPLEMENTATION_SHOWING_A_SLIGHTLY_DIFFERENT_APPROACH
6025
6026template <>
6027inline void FrameInterpolatorBilinear::scale8BitPerChannelSubset7BitPrecisionNEON<2u, 8u>(const uint8_t* source, uint8_t* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
6028{
6029 ocean_assert(source != nullptr && target != nullptr);
6030 ocean_assert(sourceWidth >= 2u && sourceWidth <= 65535u);
6031 ocean_assert(sourceHeight >= 0u && sourceHeight <= 65535u);
6032 ocean_assert(targetWidth >= 8u && targetWidth <= 65535u)
6033 ocean_assert(targetHeight >= 1u && targetHeight <= 65535u);
6034 ocean_assert(sourceX_s_targetX > 0.0 && sourceY_s_targetY > 0.0);
6035
6036 ocean_assert(sourcePaddingElements == 0u); // not supported
6037 ocean_assert(targetPaddingElements == 0u);
6038
6039 using PixelType = typename DataType<uint8_t, 2u>::Type;
6040
6041 PixelType* targetPixelData = (PixelType*)target + firstTargetRow * targetWidth;
6042 const PixelType* const sourcePixelData = (const PixelType*)source;
6043
6044 // our offset values for the four left pixels in relation to the first pixel of the row
6045 unsigned int leftOffsets[8];
6046
6047 // our color values of the eight top and bottom pixels (32 bit = 16 bit left and 16 bit right)
6048 unsigned int topPixels[8];
6049 unsigned int bottomPixels[8];
6050
6051 // this function uses fixed point numbers with 16 bit for the calculation of the interpolation positions and factors:
6052 // fixedPointLocation = floatLocation * 2^16
6053 //
6054 // [FEDCBA98, 76543210]
6055 // [pixel , subpixel]
6056 //
6057 // fixedPointLocation = pixel + subpixel / 2^16
6058 //
6059 // Thus, the upper 16 bit represent the location of e.g., the left pixel (for the linear interpolation)
6060 // while the lower 16 bit represent one of both interpolation factors (and 2^16 - subpixel represents the second interpolation factor)
6061
6062 const unsigned int sourceX_T_targetX_fixed16 = (unsigned int)(double(0x10000u) * sourceX_s_targetX + 0.5);
6063 const unsigned int sourceY_T_targetY_fixed16 = (unsigned int)(double(0x10000u) * sourceY_s_targetY + 0.5);
6064
6065 const int targetOffsetX_fixed16 = (int)(double(0x10000u) * ((sourceX_s_targetX * 0.5) - 0.5) + 0.5);
6066 const int targetOffsetY_fixed16 = (int)(double(0x10000u) * ((sourceY_s_targetY * 0.5) - 0.5) + 0.5);
6067
6068 // we store 4 integers: [sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16]
6069 const uint32x4_t m128_u_sourceX_T_targetX_fixed16 = vdupq_n_u32(sourceX_T_targetX_fixed16);
6070
6071 // we store 4 integers: [sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16]
6072 const int32x4_t m128_s_targetOffsetX_fixed16 = vdupq_n_s32(targetOffsetX_fixed16);
6073
6074 // we store 4 integers: [sourceWidth - 2, sourceWidth - 2, sourceWidth - 2, sourceWidth - 2]
6075 const uint32x4_t m128_u_sourceWidth_2 = vdupq_n_u32(sourceWidth - 2u);
6076
6077 // we store 4 integers: [0, 0, 0, 0]
6078 const int32x4_t m128_s_zero = vdupq_n_s32(0);
6079
6080 const unsigned int u_0123[4] = {0u, 1u, 2u, 3u};
6081 const uint32x4_t m128_u_0123 = vld1q_u32(u_0123);
6082
6083 for (unsigned int y = firstTargetRow; y < firstTargetRow + numberTargetRows; ++y)
6084 {
6085 const unsigned int sourceY_fixed16 = minmax<int>(0, targetOffsetY_fixed16 + int(sourceY_T_targetY_fixed16 * y), (sourceHeight - 1u) << 16u);
6086
6087 const unsigned int sourceRowTop = sourceY_fixed16 >> 16u; // we must not round here
6088 const unsigned int factorBottom_fixed16 = sourceY_fixed16 & 0x0000FFFFu;
6089 const unsigned int factorBottom = factorBottom_fixed16 >> 9u;
6090
6091 const uint8x8_t m64_u_factorsBottom = vdup_n_u8(factorBottom);
6092 // factorTop = 128 - factorBottom
6093 const uint8x8_t m64_u_factorsTop = vdup_n_u8(128u - factorBottom);
6094
6095 const unsigned int sourceRowBottom = min(sourceRowTop + 1u, sourceHeight - 1u);
6096
6097 const PixelType* const sourceTopRowPixelData = sourcePixelData + sourceRowTop * sourceWidth;
6098 const PixelType* const sourceBottomRowPixelData = sourcePixelData + sourceRowBottom * sourceWidth;
6099
6100 for (unsigned int x = 0; x < targetWidth; x += 8u)
6101 {
6102 if (x + 8u > targetWidth)
6103 {
6104 // the last iteration will not fit into the output frame,
6105 // so we simply shift x left by some pixels (at most 7) and we will calculate some pixels again
6106
6107 ocean_assert(x >= 8u && targetWidth > 8u);
6108 const unsigned int newX = targetWidth - 8u;
6109
6110 ocean_assert(x > newX);
6111 targetPixelData -= x - newX;
6112
6113 x = newX;
6114
6115 // the for loop will stop after this iteration
6116 ocean_assert(!(x + 8u < targetWidth));
6117 }
6118
6119
6120 // we need four successive x coordinate floats:
6121 // [x + 3, x + 2, x + 1; x + 0]
6122 const uint32x4_t m128_u_x_0123 = vaddq_u32(vdupq_n_u32(x), m128_u_0123);
6123 const uint32x4_t m128_u_x_4567 = vaddq_u32(vdupq_n_u32(x + 4u), m128_u_0123);
6124
6125 // we calculate the four source locations for our four target locations
6126 const int32x4_t m128_s_sourceX_0123_fixed16 = vmaxq_s32(m128_s_zero, vaddq_s32(m128_s_targetOffsetX_fixed16, vreinterpretq_s32_u32(vmulq_u32(m128_u_sourceX_T_targetX_fixed16, m128_u_x_0123))));
6127 const uint32x4_t m128_u_sourceX_0123_fixed16 = vreinterpretq_u32_s32(m128_s_sourceX_0123_fixed16);
6128
6129 const int32x4_t m128_s_sourceX_4567_fixed16 = vmaxq_s32(m128_s_zero, vaddq_s32(m128_s_targetOffsetX_fixed16, vreinterpretq_s32_u32(vmulq_u32(m128_u_sourceX_T_targetX_fixed16, m128_u_x_4567))));
6130 const uint32x4_t m128_u_sourceX_4567_fixed16 = vreinterpretq_u32_s32(m128_s_sourceX_4567_fixed16);
6131
6132 // now we determine the pixel/integer accurate source locations
6133 // m128_u_left = min(floor(m128_f_sourceX), sourceWidth - 2)
6134 const uint32x4_t m128_u_left_0123 = vminq_u32(vshrq_n_u32(m128_u_sourceX_0123_fixed16, 16), m128_u_sourceWidth_2); // not vrshrq_n_u32 as we must not round here
6135 const uint32x4_t m128_u_left_4567 = vminq_u32(vshrq_n_u32(m128_u_sourceX_4567_fixed16, 16), m128_u_sourceWidth_2);
6136
6137 // we store the offsets we have calculated
6138 vst1q_u32(leftOffsets + 0, m128_u_left_0123);
6139 vst1q_u32(leftOffsets + 4, m128_u_left_4567);
6140
6141
6142
6143 // we load the left and the right pixels into an intermediate buffer
6144 // with following pattern (with top-left TL, and top-right TR):
6145 // F E D C B A 9 8 7 6 5 4 3 2 1 0
6146 // [TR3 TR3 TL3 TL3 TR2 TR2 TL2 TL2 TR1 TR1 TL1 TL1 TR0 TR0 TL0 TL0]
6147 // [TR7 TR7 TL7 TL7 TR6 TR6 TL6 TL6 TR5 TR5 TL5 TL5 TR4 TR4 TL4 TL4]
6148
6149 for (unsigned int n = 0u; n < 8u; ++n)
6150 {
6151 topPixels[n] = *(unsigned int*)(sourceTopRowPixelData + leftOffsets[n]);
6152 }
6153
6154 const uint16x8_t m128_topPixels_0123 = vreinterpretq_u16_u32(vld1q_u32(topPixels + 0));
6155 const uint16x8_t m128_topPixels_4567 = vreinterpretq_u16_u32(vld1q_u32(topPixels + 4));
6156
6157 for (unsigned int n = 0u; n < 8u; ++n)
6158 {
6159 bottomPixels[n] = *(unsigned int*)(sourceBottomRowPixelData + leftOffsets[n]);
6160 }
6161
6162 const uint16x8_t m128_bottomPixels_0123 = vreinterpretq_u16_u32(vld1q_u32(bottomPixels + 0));
6163 const uint16x8_t m128_bottomPixels_4567 = vreinterpretq_u16_u32(vld1q_u32(bottomPixels + 4));
6164
6165
6166 // we determine the multiplication factors for the right pixels - which are already stored in the lower 16 bits
6167 // we need an accuracy of 7 bits (values between 0 and 128):
6168 // 76 54 32 10
6169 // [F3 F2 F1 F0]
6170 const uint16x4_t m64_u_factorsRight_0123 = vrshrn_n_u32(vandq_u32(m128_u_sourceX_0123_fixed16, vdupq_n_u32(0x0000FFFFu)), 9);
6171 const uint16x4_t m64_u_factorsRight_4567 = vrshrn_n_u32(vandq_u32(m128_u_sourceX_4567_fixed16, vdupq_n_u32(0x0000FFFFu)), 9);
6172
6173 // as we will have the pixel information de-interleaved, we can store all 8 interpolation factors together into one 8x8 bit register:
6174 const uint16x8_t m128_u_factorsRight = vcombine_u16(m64_u_factorsRight_0123, m64_u_factorsRight_4567);
6175 const uint8x8_t m64_u_factorsRight = vqmovn_u16(m128_u_factorsRight);
6176
6177 // nw we have the interpolation factors for 8 left and 8 right pixels:
6178 // 7 6 5 4 3 2 1 0
6179 // [F7 F6 F5 F4 F3 F2 F1 F0]
6180 const uint8x8_t m64_u_factorsLeft = vsub_u8(vdup_n_u8(128u), m64_u_factorsRight);
6181
6182
6183 // we de-interleave the top pixels to left and right pixels:
6184 // F E D C B A 9 8 7 6 5 4 3 2 1 0
6185 // [TL7 TL7 TL6 TL6 TL5 TL5 TL4 TL4 TL3 TL3 TL2 TL2 TL1 TL1 TL0 TL0]
6186 // [TR7 TR7 TR6 TR6 TR5 TR5 TR4 TR4 TR3 TR3 TR2 TR2 TR1 TR1 TR0 TR0]
6187 const uint16x8x2_t m2_128_topPixelsLeftRight = vuzpq_u16(m128_topPixels_0123, m128_topPixels_4567);
6188
6189 // we de-interleave the pixels again to separate channel 0 and channel 1:
6190 // 7 6 5 4 3 2 1 0
6191 // channel 0: [TL7 TL6 TL5 TL4 TL3 TL2 TL1 TL0]
6192 // channel 1: [TL7 TL6 TL5 TL4 TL3 TL2 TL1 TL0]
6193 const uint8x8x2_t m2_64_topPixelsLeft_channels_01 = vuzp_u8(vget_low_u8(vreinterpretq_u8_u16(m2_128_topPixelsLeftRight.val[0])), vget_high_u8(vreinterpretq_u8_u16(m2_128_topPixelsLeftRight.val[0])));
6194 const uint8x8x2_t m2_64_topPixelsRight_channels_01 = vuzp_u8(vget_low_u8(vreinterpretq_u8_u16(m2_128_topPixelsLeftRight.val[1])), vget_high_u8(vreinterpretq_u8_u16(m2_128_topPixelsLeftRight.val[1])));
6195
6196 const uint8x8_t& m64_topPixelsLeft_channel_0 = m2_64_topPixelsLeft_channels_01.val[0];
6197 const uint8x8_t& m64_topPixelsLeft_channel_1 = m2_64_topPixelsLeft_channels_01.val[1];
6198
6199 const uint8x8_t& m64_topPixelsRight_channel_0 = m2_64_topPixelsRight_channels_01.val[0];
6200 const uint8x8_t& m64_topPixelsRight_channel_1 = m2_64_topPixelsRight_channels_01.val[1];
6201
6202
6203 // we determine the intermediate interpolation results for the top row (and we narrow down the 16 bit results 8 bit results)
6204 uint16x8_t m128_muliplication_channel_0 = vmull_u8(m64_topPixelsLeft_channel_0, m64_u_factorsLeft);
6205 uint16x8_t m128_muliplication_channel_1 = vmull_u8(m64_topPixelsLeft_channel_1, m64_u_factorsLeft);
6206
6207 m128_muliplication_channel_0 = vmlal_u8(m128_muliplication_channel_0, m64_topPixelsRight_channel_0, m64_u_factorsRight);
6208 m128_muliplication_channel_1 = vmlal_u8(m128_muliplication_channel_1, m64_topPixelsRight_channel_1, m64_u_factorsRight);
6209
6210 const uint8x8_t m64_topRow_channel_0 = vrshrn_n_u16(m128_muliplication_channel_0, 7);
6211 const uint8x8_t m64_topRow_channel_1 = vrshrn_n_u16(m128_muliplication_channel_1, 7);
6212
6213
6214 // we proceed with the bottom pixels (as we did with the top pixels)
6215 const uint16x8x2_t m2_128_bottomPixelsLeftRight = vuzpq_u16(m128_bottomPixels_0123, m128_bottomPixels_4567);
6216
6217 const uint8x8x2_t m2_64_bottomPixelsLeft_channels_01 = vuzp_u8(vget_low_u8(vreinterpretq_u8_u16(m2_128_bottomPixelsLeftRight.val[0])), vget_high_u8(vreinterpretq_u8_u16(m2_128_bottomPixelsLeftRight.val[0])));
6218 const uint8x8x2_t m2_64_bottomPixelsRight_channels_01 = vuzp_u8(vget_low_u8(vreinterpretq_u8_u16(m2_128_bottomPixelsLeftRight.val[1])), vget_high_u8(vreinterpretq_u8_u16(m2_128_bottomPixelsLeftRight.val[1])));
6219
6220 const uint8x8_t& m64_bottomPixelsLeft_channel_0 = m2_64_bottomPixelsLeft_channels_01.val[0];
6221 const uint8x8_t& m64_bottomPixelsLeft_channel_1 = m2_64_bottomPixelsLeft_channels_01.val[1];
6222
6223 const uint8x8_t& m64_bottomPixelsRight_channel_0 = m2_64_bottomPixelsRight_channels_01.val[0];
6224 const uint8x8_t& m64_bottomPixelsRight_channel_1 = m2_64_bottomPixelsRight_channels_01.val[1];
6225
6226
6227 // we determine the intermediate interpolation results for the bottom row (and we narrow down the 16 bit results 8 bit results)
6228 m128_muliplication_channel_0 = vmull_u8(m64_bottomPixelsLeft_channel_0, m64_u_factorsLeft);
6229 m128_muliplication_channel_1 = vmull_u8(m64_bottomPixelsLeft_channel_1, m64_u_factorsLeft);
6230
6231 m128_muliplication_channel_0 = vmlal_u8(m128_muliplication_channel_0, m64_bottomPixelsRight_channel_0, m64_u_factorsRight);
6232 m128_muliplication_channel_1 = vmlal_u8(m128_muliplication_channel_1, m64_bottomPixelsRight_channel_1, m64_u_factorsRight);
6233
6234 const uint8x8_t m64_bottomRow_channel_0 = vrshrn_n_u16(m128_muliplication_channel_0, 7);
6235 const uint8x8_t m64_bottomRow_channel_1 = vrshrn_n_u16(m128_muliplication_channel_1, 7);
6236
6237
6238 // finnally we determine the interpolation result between top and bottom row
6239 m128_muliplication_channel_0 = vmull_u8(m64_topRow_channel_0, m64_u_factorsTop);
6240 m128_muliplication_channel_1 = vmull_u8(m64_topRow_channel_1, m64_u_factorsTop);
6241
6242 m128_muliplication_channel_0 = vmlal_u8(m128_muliplication_channel_0, m64_bottomRow_channel_0, m64_u_factorsBottom);
6243 m128_muliplication_channel_1 = vmlal_u8(m128_muliplication_channel_1, m64_bottomRow_channel_1, m64_u_factorsBottom);
6244
6245
6246 // we narrow down the interpolation results and we store them
6247 uint8x8x2_t m2_64_result;
6248 m2_64_result.val[0] = vrshrn_n_u16(m128_muliplication_channel_0, 7);
6249 m2_64_result.val[1] = vrshrn_n_u16(m128_muliplication_channel_1, 7);
6250
6251 // we write back the results and interleave them automatically
6252 vst2_u8((uint8_t*)targetPixelData, m2_64_result);
6253
6254 targetPixelData += 8;
6255 }
6256
6257 // we need to process the last pixel again, as this pixel may have received wrong interpolation factors as we always load two successive pixels into our NEON registers
6258 // **TODO** this is just a temporary solution, check how we can avoid this additional step
6259
6260 const unsigned int firstInvalidTargetX = (((sourceWidth - 1u) << 16u) - targetOffsetX_fixed16) / sourceX_T_targetX_fixed16;
6261
6262 for (unsigned int x = firstInvalidTargetX; x < targetWidth; ++x)
6263 {
6264 const unsigned int lastSourcePixelPosition_fixed16 = minmax<int>(0, targetOffsetX_fixed16 + int(sourceX_T_targetX_fixed16 * x), (sourceWidth - 1u) << 16u);
6265
6266 const unsigned int lastSourcePixelLeft = lastSourcePixelPosition_fixed16 >> 16u;
6267 ocean_assert(lastSourcePixelLeft < sourceWidth);
6268 const unsigned int lastSourcePixelRight = min(lastSourcePixelLeft + 1u, sourceWidth - 1u);
6269
6270 const unsigned int factorRight_fixed16 = lastSourcePixelPosition_fixed16 & 0x0000FFFFu;
6271
6272 const unsigned int factorRight = factorRight_fixed16 >> 9u;
6273 const unsigned int factorLeft = 128u - factorRight;
6274
6275 for (unsigned int c = 0u; c < 2u; ++c)
6276 {
6277 ((uint8_t*)(targetPixelData - (targetWidth - x)))[c] = ((((const uint8_t*)(sourceTopRowPixelData + lastSourcePixelLeft))[c] * factorLeft + ((const uint8_t*)(sourceTopRowPixelData + lastSourcePixelRight))[c] * factorRight) * (128u - factorBottom)
6278 + (((const uint8_t*)(sourceBottomRowPixelData + lastSourcePixelLeft))[c] * factorLeft + ((const uint8_t*)(sourceBottomRowPixelData + lastSourcePixelRight))[c] * factorRight) * factorBottom + 8192u) >> 14u;
6279 }
6280 }
6281 }
6282}
6283
6284#endif // OCEAN_WE_KEEP_ALSO_THIS_SLOW_IMPLEMENTATION_SHOWING_A_SLIGHTLY_DIFFERENT_APPROACH
6285
6286#ifdef OCEAN_WE_KEEP_THIS_IMPLEMENTATION_AS_WE_NEED_THIS_TO_FOR_A_NEW_NEON_IMPLEMENTATION
6287
6288template <>
6289inline void FrameInterpolatorBilinear::scale8BitPerChannelSubset7BitPrecisionNEON<3u, 8u>(const uint8_t* source, uint8_t* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
6290{
6291 ocean_assert(source != nullptr && target != nullptr);
6292 ocean_assert(sourceWidth >= 2u && sourceWidth <= 65535u);
6293 ocean_assert(sourceHeight >= 1u && sourceHeight <= 65535u);
6294 ocean_assert(targetWidth >= 8u && targetWidth <= 65535u);
6295 ocean_assert(targetHeight >= 1u && targetHeight <= 65535u);
6296 ocean_assert(sourceX_s_targetX > 0.0 && sourceY_s_targetY > 0.0);
6297
6298 ocean_assert(sourcePaddingElements == 0u); // not supported
6299 ocean_assert(targetPaddingElements == 0u);
6300
6301 using PixelType = typename DataType<uint8_t, 3u>::Type;
6302
6303 PixelType* targetPixelData = (PixelType*)target + firstTargetRow * targetWidth;
6304 const PixelType* const sourcePixelData = (const PixelType*)source;
6305
6306 // our offset values for the eight left pixels in relation to the first pixel of the row
6307 unsigned int leftOffsets[8];
6308
6309 // this function uses fixed point numbers with 16 bit for the calculation of the interpolation positions and factors:
6310 // fixedPointLocation = floatLocation * 2^16
6311 //
6312 // [FEDCBA98, 76543210]
6313 // [pixel , subpixel]
6314 //
6315 // fixedPointLocation = pixel + subpixel / 2^16
6316 //
6317 // Thus, the upper 16 bit represent the location of e.g., the left pixel (for the linear interpolation)
6318 // while the lower 16 bit represent one of both interpolation factors (and 2^16 - subpixel represents the second interpolation factor)
6319
6320 const unsigned int sourceX_T_targetX_fixed16 = (unsigned int)(double(0x10000u) * sourceX_s_targetX + 0.5);
6321 const unsigned int sourceY_T_targetY_fixed16 = (unsigned int)(double(0x10000u) * sourceY_s_targetY + 0.5);
6322
6323 const int targetOffsetX_fixed16 = (int)(double(0x10000u) * ((sourceX_s_targetX * 0.5) - 0.5) + 0.5);
6324 const int targetOffsetY_fixed16 = (int)(double(0x10000u) * ((sourceY_s_targetY * 0.5) - 0.5) + 0.5);
6325
6326 // we store 4 integers: [sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16]
6327 const uint32x4_t m128_u_sourceX_T_targetX_fixed16 = vdupq_n_u32(sourceX_T_targetX_fixed16);
6328
6329 // we store 4 integers: [sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16]
6330 const int32x4_t m128_s_targetOffsetX_fixed16 = vdupq_n_s32(targetOffsetX_fixed16);
6331
6332 // we store 4 integers: [sourceWidth - 2, sourceWidth - 2, sourceWidth - 2, sourceWidth - 2]
6333 const uint32x4_t m128_u_sourceWidth_2 = vdupq_n_u32(sourceWidth - 2u);
6334
6335 // we store 4 integers: [0, 0, 0, 0]
6336 const int32x4_t m128_s_zero = vdupq_n_s32(0);
6337
6338 const unsigned int u_0123[4] = {0u, 1u, 2u, 3u};
6339 const uint32x4_t m128_u_0123 = vld1q_u32(u_0123);
6340
6341 for (unsigned int y = firstTargetRow; y < firstTargetRow + numberTargetRows; ++y)
6342 {
6343 const unsigned int sourceY_fixed16 = minmax<int>(0, targetOffsetY_fixed16 + int(sourceY_T_targetY_fixed16 * y), (sourceHeight - 1u) << 16u);
6344
6345 const unsigned int sourceRowTop = sourceY_fixed16 >> 16u; // we must not round here
6346 const unsigned int factorBottom_fixed16 = sourceY_fixed16 & 0x0000FFFFu;
6347 const unsigned int factorBottom = factorBottom_fixed16 >> 9u;
6348
6349 const uint8x8_t m64_u_factorsBottom = vdup_n_u8(factorBottom);
6350 // factorTop = 128 - factorBottom
6351 const uint8x8_t m64_u_factorsTop = vdup_n_u8(128u - factorBottom);
6352
6353 const unsigned int sourceRowBottom = min(sourceRowTop + 1u, sourceHeight - 1u);
6354
6355 const PixelType* const sourceTopRowPixelData = sourcePixelData + sourceRowTop * sourceWidth;
6356 const PixelType* const sourceBottomRowPixelData = sourcePixelData + sourceRowBottom * sourceWidth;
6357
6358 for (unsigned int x = 0; x < targetWidth; x += 8u)
6359 {
6360 if (x + 8u > targetWidth)
6361 {
6362 // the last iteration will not fit into the output frame,
6363 // so we simply shift x left by some pixels (at most 7) and we will calculate some pixels again
6364
6365 ocean_assert(x >= 8u && targetWidth > 8u);
6366 const unsigned int newX = targetWidth - 8u;
6367
6368 ocean_assert(x > newX);
6369 targetPixelData -= x - newX;
6370
6371 x = newX;
6372
6373 // the for loop will stop after this iteration
6374 ocean_assert(!(x + 8u < targetWidth));
6375 }
6376
6377
6378 // we need four successive x coordinate floats:
6379 // [x + 3, x + 2, x + 1; x + 0]
6380 const uint32x4_t m128_u_x_0123 = vaddq_u32(vdupq_n_u32(x), m128_u_0123);
6381 const uint32x4_t m128_u_x_4567 = vaddq_u32(vdupq_n_u32(x + 4u), m128_u_0123);
6382
6383 // we calculate the four source locations for our four target locations
6384 const int32x4_t m128_s_sourceX_0123_fixed16 = vmaxq_s32(m128_s_zero, vaddq_s32(m128_s_targetOffsetX_fixed16, vreinterpretq_s32_u32(vmulq_u32(m128_u_sourceX_T_targetX_fixed16, m128_u_x_0123))));
6385 const uint32x4_t m128_u_sourceX_0123_fixed16 = vreinterpretq_u32_s32(m128_s_sourceX_0123_fixed16);
6386
6387 const int32x4_t m128_s_sourceX_4567_fixed16 = vmaxq_s32(m128_s_zero, vaddq_s32(m128_s_targetOffsetX_fixed16, vreinterpretq_s32_u32(vmulq_u32(m128_u_sourceX_T_targetX_fixed16, m128_u_x_4567))));
6388 const uint32x4_t m128_u_sourceX_4567_fixed16 = vreinterpretq_u32_s32(m128_s_sourceX_4567_fixed16);
6389
6390 // now we determine the pixel/integer accurate source locations
6391 // m128_u_left = min(floor(m128_f_sourceX), sourceWidth - 2)
6392 const uint32x4_t m128_u_left_0123 = vminq_u32(vshrq_n_u32(m128_u_sourceX_0123_fixed16, 16), m128_u_sourceWidth_2); // not vrshrq_n_u32 as we must not round here
6393 const uint32x4_t m128_u_left_4567 = vminq_u32(vshrq_n_u32(m128_u_sourceX_4567_fixed16, 16), m128_u_sourceWidth_2);
6394
6395 // we store the offsets we have calculated
6396 vst1q_u32(leftOffsets + 0, m128_u_left_0123);
6397 vst1q_u32(leftOffsets + 4, m128_u_left_4567);
6398
6399
6400
6401 // we load the individal pixels to our four (de-interleaved) 8x8 bit registers (we do this for the top-left and top-right pixels)
6402 // note: loading of each pixel individually is significantly slower than loading two neighboring pixels within one iteration
6403
6404 uint8x8x3_t topLeftPixels;
6405 uint8x8x3_t topRightPixels;
6406
6407 topLeftPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[0u] + 0), topLeftPixels, 0);
6408 topRightPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[0u] + 1), topRightPixels, 0);
6409
6410 topLeftPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[1u] + 0), topLeftPixels, 1);
6411 topRightPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[1u] + 1), topRightPixels, 1);
6412
6413 topLeftPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[2u] + 0), topLeftPixels, 2);
6414 topRightPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[2u] + 1), topRightPixels, 2);
6415
6416 topLeftPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[3u] + 0), topLeftPixels, 3);
6417 topRightPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[3u] + 1), topRightPixels, 3);
6418
6419 topLeftPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[4u] + 0), topLeftPixels, 4);
6420 topRightPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[4u] + 1), topRightPixels, 4);
6421
6422 topLeftPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[5u] + 0), topLeftPixels, 5);
6423 topRightPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[5u] + 1), topRightPixels, 5);
6424
6425 topLeftPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[6u] + 0), topLeftPixels, 6);
6426 topRightPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[6u] + 1), topRightPixels, 6);
6427
6428 topLeftPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[7u] + 0), topLeftPixels, 7);
6429 topRightPixels = vld3_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[7u] + 1), topRightPixels, 7);
6430
6431
6432 // we load the individal pixels to our four (de-interleaved) 8x8 bit registers (we do this for the bottom-left and bottom-right pixels)
6433
6434 uint8x8x3_t bottomLeftPixels;
6435 uint8x8x3_t bottomRightPixels;
6436
6437 bottomLeftPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[0u] + 0), bottomLeftPixels, 0);
6438 bottomRightPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[0u] + 1), bottomRightPixels, 0);
6439
6440 bottomLeftPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[1u] + 0), bottomLeftPixels, 1);
6441 bottomRightPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[1u] + 1), bottomRightPixels, 1);
6442
6443 bottomLeftPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[2u] + 0), bottomLeftPixels, 2);
6444 bottomRightPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[2u] + 1), bottomRightPixels, 2);
6445
6446 bottomLeftPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[3u] + 0), bottomLeftPixels, 3);
6447 bottomRightPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[3u] + 1), bottomRightPixels, 3);
6448
6449 bottomLeftPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[4u] + 0), bottomLeftPixels, 4);
6450 bottomRightPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[4u] + 1), bottomRightPixels, 4);
6451
6452 bottomLeftPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[5u] + 0), bottomLeftPixels, 5);
6453 bottomRightPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[5u] + 1), bottomRightPixels, 5);
6454
6455 bottomLeftPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[6u] + 0), bottomLeftPixels, 6);
6456 bottomRightPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[6u] + 1), bottomRightPixels, 6);
6457
6458 bottomLeftPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[7u] + 0), bottomLeftPixels, 7);
6459 bottomRightPixels = vld3_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[7u] + 1), bottomRightPixels, 7);
6460
6461
6462
6463 // we determine the multiplication factors for the right pixels - which are already stored in the lower 16 bits
6464 // we need an accuracy of 7 bits (values between 0 and 128):
6465 // 76 54 32 10
6466 // [F3 F2 F1 F0]
6467 const uint16x4_t m64_u_factorsRight_0123 = vrshrn_n_u32(vandq_u32(m128_u_sourceX_0123_fixed16, vdupq_n_u32(0x0000FFFFu)), 9);
6468 const uint16x4_t m64_u_factorsRight_4567 = vrshrn_n_u32(vandq_u32(m128_u_sourceX_4567_fixed16, vdupq_n_u32(0x0000FFFFu)), 9);
6469
6470 // as we have the pixel information de-interleaved, we can store all 8 interpolation factors together into one 8x8 bit register:
6471 const uint16x8_t m128_u_factorsRight = vcombine_u16(m64_u_factorsRight_0123, m64_u_factorsRight_4567);
6472 const uint8x8_t m64_u_factorsRight = vqmovn_u16(m128_u_factorsRight);
6473 const uint8x8_t m64_u_factorsLeft = vsub_u8(vdup_n_u8(128u), m64_u_factorsRight);
6474
6475
6476
6477 // we determine the intermediate interpolation results for the top row (and we narrow down the 16 bit results 8 bit results)
6478 uint16x8_t m128_muliplicationChannel_0 = vmull_u8(topLeftPixels.val[0], m64_u_factorsLeft);
6479 uint16x8_t m128_muliplicationChannel_1 = vmull_u8(topLeftPixels.val[1], m64_u_factorsLeft);
6480 uint16x8_t m128_muliplicationChannel_2 = vmull_u8(topLeftPixels.val[2], m64_u_factorsLeft);
6481
6482 m128_muliplicationChannel_0 = vmlal_u8(m128_muliplicationChannel_0, topRightPixels.val[0], m64_u_factorsRight);
6483 m128_muliplicationChannel_1 = vmlal_u8(m128_muliplicationChannel_1, topRightPixels.val[1], m64_u_factorsRight);
6484 m128_muliplicationChannel_2 = vmlal_u8(m128_muliplicationChannel_2, topRightPixels.val[2], m64_u_factorsRight);
6485
6486 uint8x8_t m64_topRowChannel_0 = vrshrn_n_u16(m128_muliplicationChannel_0, 7);
6487 uint8x8_t m64_topRowChannel_1 = vrshrn_n_u16(m128_muliplicationChannel_1, 7);
6488 uint8x8_t m64_topRowChannel_2 = vrshrn_n_u16(m128_muliplicationChannel_2, 7);
6489
6490
6491
6492 // we determine the intermediate interpolation results for the bottom row (and we narrow down the 16 bit results 8 bit results)
6493 m128_muliplicationChannel_0 = vmull_u8(bottomLeftPixels.val[0], m64_u_factorsLeft);
6494 m128_muliplicationChannel_1 = vmull_u8(bottomLeftPixels.val[1], m64_u_factorsLeft);
6495 m128_muliplicationChannel_2 = vmull_u8(bottomLeftPixels.val[2], m64_u_factorsLeft);
6496
6497 m128_muliplicationChannel_0 = vmlal_u8(m128_muliplicationChannel_0, bottomRightPixels.val[0], m64_u_factorsRight);
6498 m128_muliplicationChannel_1 = vmlal_u8(m128_muliplicationChannel_1, bottomRightPixels.val[1], m64_u_factorsRight);
6499 m128_muliplicationChannel_2 = vmlal_u8(m128_muliplicationChannel_2, bottomRightPixels.val[2], m64_u_factorsRight);
6500
6501 uint8x8_t m64_bottomRowChannel_0 = vrshrn_n_u16(m128_muliplicationChannel_0, 7);
6502 uint8x8_t m64_bottomRowChannel_1 = vrshrn_n_u16(m128_muliplicationChannel_1, 7);
6503 uint8x8_t m64_bottomRowChannel_2 = vrshrn_n_u16(m128_muliplicationChannel_2, 7);
6504
6505
6506
6507 // finnally we determine the interpolation result between top and bottom row
6508 m128_muliplicationChannel_0 = vmull_u8(m64_topRowChannel_0, m64_u_factorsTop);
6509 m128_muliplicationChannel_1 = vmull_u8(m64_topRowChannel_1, m64_u_factorsTop);
6510 m128_muliplicationChannel_2 = vmull_u8(m64_topRowChannel_2, m64_u_factorsTop);
6511
6512 m128_muliplicationChannel_0 = vmlal_u8(m128_muliplicationChannel_0, m64_bottomRowChannel_0, m64_u_factorsBottom);
6513 m128_muliplicationChannel_1 = vmlal_u8(m128_muliplicationChannel_1, m64_bottomRowChannel_1, m64_u_factorsBottom);
6514 m128_muliplicationChannel_2 = vmlal_u8(m128_muliplicationChannel_2, m64_bottomRowChannel_2, m64_u_factorsBottom);
6515
6516
6517 // we narrow down the interpolation results and we store them
6518 uint8x8x3_t result;
6519 result.val[0] = vrshrn_n_u16(m128_muliplicationChannel_0, 7);
6520 result.val[1] = vrshrn_n_u16(m128_muliplicationChannel_1, 7);
6521 result.val[2] = vrshrn_n_u16(m128_muliplicationChannel_2, 7);
6522
6523 // we write back the results and interleave them automatically
6524 vst3_u8((uint8_t*)targetPixelData, result);
6525
6526 targetPixelData += 8;
6527 }
6528
6529 // we need to process the last pixel again, as this pixel may have received wrong interpolation factors as we always load two successive pixels into our NEON registers
6530 // **TODO** this is just a temporary solution, check how we can avoid this additional step
6531
6532 const unsigned int firstInvalidTargetX = (((sourceWidth - 1u) << 16u) - targetOffsetX_fixed16) / sourceX_T_targetX_fixed16;
6533
6534 for (unsigned int x = firstInvalidTargetX; x < targetWidth; ++x)
6535 {
6536 const unsigned int lastSourcePixelPosition_fixed16 = minmax<int>(0, targetOffsetX_fixed16 + int(sourceX_T_targetX_fixed16 * x), (sourceWidth - 1u) << 16u);
6537
6538 const unsigned int lastSourcePixelLeft = lastSourcePixelPosition_fixed16 >> 16u;
6539 ocean_assert(lastSourcePixelLeft < sourceWidth);
6540 const unsigned int lastSourcePixelRight = min(lastSourcePixelLeft + 1u, sourceWidth - 1u);
6541
6542 const unsigned int factorRight_fixed16 = lastSourcePixelPosition_fixed16 & 0x0000FFFFu;
6543
6544 const unsigned int factorRight = factorRight_fixed16 >> 9u;
6545 const unsigned int factorLeft = 128u - factorRight;
6546
6547 for (unsigned int c = 0u; c < 3u; ++c)
6548 {
6549 ((uint8_t*)(targetPixelData - (targetWidth - x)))[c] = ((((const uint8_t*)(sourceTopRowPixelData + lastSourcePixelLeft))[c] * factorLeft + ((const uint8_t*)(sourceTopRowPixelData + lastSourcePixelRight))[c] * factorRight) * (128u - factorBottom)
6550 + (((const uint8_t*)(sourceBottomRowPixelData + lastSourcePixelLeft))[c] * factorLeft + ((const uint8_t*)(sourceBottomRowPixelData + lastSourcePixelRight))[c] * factorRight) * factorBottom + 8192u) >> 14u;
6551 }
6552 }
6553 }
6554}
6555
6556#endif // OCEAN_WE_KEEP_THIS_IMPLEMENTATION_AS_WE_NEED_THIS_TO_FOR_A_NEW_NEON_IMPLEMENTATION
6557
6558#ifdef OCEAN_WE_KEEP_ALSO_THIS_SLOW_IMPLEMENTATION_SHOWING_A_MORE_GENERIC_APPROACH
6559
6560/// \cond DOXYGEN_DO_NOT_DOCUMENT
6561
6562template <>
6563inline void FrameInterpolatorBilinear::resize8BitPerChannelSubset7BitPrecisionNEON<4u, 8u>(const uint8_t* source, uint8_t* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
6564{
6565 ocean_assert(source != nullptr && target != nullptr);
6566 ocean_assert(sourceWidth >= 2u && sourceWidth <= 65535u);
6567 ocean_assert(sourceHeight >= 1u && sourceHeight <= 65535u);
6568 ocean_assert(targetWidth >= 8u && targetWidth <= 65535u);
6569 ocean_assert(targetHeight >= 1u && targetHeight <= 65535u);
6570 ocean_assert(sourceX_s_targetX > 0.0 && sourceY_s_targetY > 0.0);
6571
6572 ocean_assert(sourcePaddingElements == 0u); // not supported
6573 ocean_assert(targetPaddingElements == 0u);
6574
6575 using PixelType = typename DataType<uint8_t, 4u>::Type;
6576
6577 PixelType* targetPixelData = (PixelType*)target + firstTargetRow * targetWidth;
6578 const PixelType* const sourcePixelData = (const PixelType*)source;
6579
6580 // our offset values for the eight left pixels in relation to the first pixel of the row
6581 unsigned int leftOffsets[8];
6582
6583 // this function uses fixed point numbers with 16 bit for the calculation of const unsigned int sourceX_T_targetX_fixed16 = (unsigned int)(double(0x10000u) * sourceX_s_targetX + 0.5);
6584 const unsigned int sourceY_T_targetY_fixed16 = (unsigned int)(double(0x10000u) * sourceY_s_targetY + 0.5);
6585
6586 // this function uses fixed point numbers with 16 bit for the calculation of the interpolation positions and factors:
6587 // fixedPointLocation = floatLocation * 2^16
6588 //
6589 // [FEDCBA98, 76543210]
6590 // [pixel , subpixel]
6591 //
6592 // fixedPointLocation = pixel + subpixel / 2^16
6593 //
6594 // Thus, the upper 16 bit represent the location of e.g., the left pixel (for the linear interpolation)
6595 // while the lower 16 bit represent one of both interpolation factors (and 2^16 - subpixel represents the second interpolation factor)
6596
6597 const unsigned int sourceX_T_targetX_fixed16 = (unsigned int)(double(0x10000u) * sourceX_s_targetX + 0.5);
6598 const unsigned int sourceY_T_targetY_fixed16 = (unsigned int)(double(0x10000u) * sourceY_s_targetY + 0.5);
6599
6600 const int targetOffsetX_fixed16 = (int)(double(0x10000u) * ((sourceX_s_targetX * 0.5) - 0.5) + 0.5);
6601 const int targetOffsetY_fixed16 = (int)(double(0x10000u) * ((sourceY_s_targetY * 0.5) - 0.5) + 0.5);
6602
6603 // we store 4 integers: [sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16]
6604 const uint32x4_t m128_u_sourceX_T_targetX_fixed16 = vdupq_n_u32(sourceX_T_targetX_fixed16);
6605
6606 // we store 4 integers: [sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16, sourceX_T_targetX_fixed16]
6607 const int32x4_t m128_s_targetOffsetX_fixed16 = vdupq_n_s32(targetOffsetX_fixed16);
6608
6609 // we store 4 integers: [sourceWidth - 2, sourceWidth - 2, sourceWidth - 2, sourceWidth - 2]
6610 const uint32x4_t m128_u_sourceWidth_2 = vdupq_n_u32(sourceWidth - 2u);
6611
6612 // we store 4 integers: [0, 0, 0, 0]
6613 const int32x4_t m128_s_zero = vdupq_n_s32(0);
6614
6615 const unsigned int u_0123[4] = {0u, 1u, 2u, 3u};
6616 const uint32x4_t m128_u_0123 = vld1q_u32(u_0123);
6617
6618 for (unsigned int y = firstTargetRow; y < firstTargetRow + numberTargetRows; ++y)
6619 {
6620 const unsigned int sourceY_fixed16 = minmax<int>(0, targetOffsetY_fixed16 + int(sourceY_T_targetY_fixed16 * y), (sourceHeight - 1u) << 16u);
6621
6622 const unsigned int sourceRowTop = sourceY_fixed16 >> 16u; // we must not round here
6623 const unsigned int factorBottom_fixed16 = sourceY_fixed16 & 0x0000FFFFu;
6624 const unsigned int factorBottom = factorBottom_fixed16 >> 9u;
6625
6626 const uint8x8_t m64_u_factorsBottom = vdup_n_u8(factorBottom);
6627 // factorTop = 128 - factorBottom
6628 const uint8x8_t m64_u_factorsTop = vdup_n_u8(128u - factorBottom);
6629
6630 const unsigned int sourceRowBottom = min(sourceRowTop + 1u, sourceHeight - 1u);
6631
6632 const PixelType* const sourceTopRowPixelData = sourcePixelData + sourceRowTop * sourceWidth;
6633 const PixelType* const sourceBottomRowPixelData = sourcePixelData + sourceRowBottom * sourceWidth;
6634
6635 for (unsigned int x = 0; x < targetWidth; x += 8u)
6636 {
6637 if (x + 8u > targetWidth)
6638 {
6639 // the last iteration will not fit into the output frame,
6640 // so we simply shift x left by some pixels (at most 7) and we will calculate some pixels again
6641
6642 ocean_assert(x >= 8u && targetWidth > 8u);
6643 const unsigned int newX = targetWidth - 8u;
6644
6645 ocean_assert(x > newX);
6646 targetPixelData -= x - newX;
6647
6648 x = newX;
6649
6650 // the for loop will stop after this iteration
6651 ocean_assert(!(x + 8u < targetWidth));
6652 }
6653
6654
6655 // we need four successive x coordinate floats:
6656 // [x + 3, x + 2, x + 1; x + 0]
6657 const uint32x4_t m128_u_x_0123 = vaddq_u32(vdupq_n_u32(x), m128_u_0123);
6658 const uint32x4_t m128_u_x_4567 = vaddq_u32(vdupq_n_u32(x + 4u), m128_u_0123);
6659
6660 // we calculate the four source locations for our four target locations
6661 const int32x4_t m128_s_sourceX_0123_fixed16 = vmaxq_s32(m128_s_zero, vaddq_s32(m128_s_targetOffsetX_fixed16, vreinterpretq_s32_u32(vmulq_u32(m128_u_sourceX_T_targetX_fixed16, m128_u_x_0123))));
6662 const uint32x4_t m128_u_sourceX_0123_fixed16 = vreinterpretq_u32_s32(m128_s_sourceX_0123_fixed16);
6663
6664 const int32x4_t m128_s_sourceX_4567_fixed16 = vmaxq_s32(m128_s_zero, vaddq_s32(m128_s_targetOffsetX_fixed16, vreinterpretq_s32_u32(vmulq_u32(m128_u_sourceX_T_targetX_fixed16, m128_u_x_4567))));
6665 const uint32x4_t m128_u_sourceX_4567_fixed16 = vreinterpretq_u32_s32(m128_s_sourceX_4567_fixed16);
6666
6667 // now we determine the pixel/integer accurate source locations
6668 // m128_u_left = min(floor(m128_f_sourceX), sourceWidth - 2)
6669 const uint32x4_t m128_u_left_0123 = vminq_u32(vshrq_n_u32(m128_u_sourceX_0123_fixed16, 16), m128_u_sourceWidth_2); // not vrshrq_n_u32 as we must not round here
6670 const uint32x4_t m128_u_left_4567 = vminq_u32(vshrq_n_u32(m128_u_sourceX_4567_fixed16, 16), m128_u_sourceWidth_2);
6671
6672 // we store the offsets we have calculated
6673 vst1q_u32(leftOffsets + 0, m128_u_left_0123);
6674 vst1q_u32(leftOffsets + 4, m128_u_left_4567);
6675
6676
6677
6678 // we load the individal pixels to our four (de-interleaved) 8x8 bit registers (we do this for the top-left and top-right pixels)
6679 // note: loading of each pixel individually is significantly slower than loading two neighboring pixels within one iteration
6680
6681 uint8x8x4_t topLeftPixels;
6682 uint8x8x4_t topRightPixels;
6683
6684 topLeftPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[0u] + 0), topLeftPixels, 0);
6685 topRightPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[0u] + 1), topRightPixels, 0);
6686
6687 topLeftPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[1u] + 0), topLeftPixels, 1);
6688 topRightPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[1u] + 1), topRightPixels, 1);
6689
6690 topLeftPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[2u] + 0), topLeftPixels, 2);
6691 topRightPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[2u] + 1), topRightPixels, 2);
6692
6693 topLeftPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[3u] + 0), topLeftPixels, 3);
6694 topRightPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[3u] + 1), topRightPixels, 3);
6695
6696 topLeftPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[4u] + 0), topLeftPixels, 4);
6697 topRightPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[4u] + 1), topRightPixels, 4);
6698
6699 topLeftPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[5u] + 0), topLeftPixels, 5);
6700 topRightPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[5u] + 1), topRightPixels, 5);
6701
6702 topLeftPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[6u] + 0), topLeftPixels, 6);
6703 topRightPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[6u] + 1), topRightPixels, 6);
6704
6705 topLeftPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[7u] + 0), topLeftPixels, 7);
6706 topRightPixels = vld4_lane_u8((uint8_t*)(sourceTopRowPixelData + leftOffsets[7u] + 1), topRightPixels, 7);
6707
6708
6709 // we load the individal pixels to our four (de-interleaved) 8x8 bit registers (we do this for the bottom-left and bottom-right pixels)
6710
6711 uint8x8x4_t bottomLeftPixels;
6712 uint8x8x4_t bottomRightPixels;
6713
6714 bottomLeftPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[0u] + 0), bottomLeftPixels, 0);
6715 bottomRightPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[0u] + 1), bottomRightPixels, 0);
6716
6717 bottomLeftPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[1u] + 0), bottomLeftPixels, 1);
6718 bottomRightPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[1u] + 1), bottomRightPixels, 1);
6719
6720 bottomLeftPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[2u] + 0), bottomLeftPixels, 2);
6721 bottomRightPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[2u] + 1), bottomRightPixels, 2);
6722
6723 bottomLeftPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[3u] + 0), bottomLeftPixels, 3);
6724 bottomRightPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[3u] + 1), bottomRightPixels, 3);
6725
6726 bottomLeftPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[4u] + 0), bottomLeftPixels, 4);
6727 bottomRightPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[4u] + 1), bottomRightPixels, 4);
6728
6729 bottomLeftPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[5u] + 0), bottomLeftPixels, 5);
6730 bottomRightPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[5u] + 1), bottomRightPixels, 5);
6731
6732 bottomLeftPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[6u] + 0), bottomLeftPixels, 6);
6733 bottomRightPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[6u] + 1), bottomRightPixels, 6);
6734
6735 bottomLeftPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[7u] + 0), bottomLeftPixels, 7);
6736 bottomRightPixels = vld4_lane_u8((uint8_t*)(sourceBottomRowPixelData + leftOffsets[7u] + 1), bottomRightPixels, 7);
6737
6738
6739
6740 // we determine the multiplication factors for the right pixels - which are already stored in the lower 16 bits
6741 // we need an accuracy of 7 bits (values between 0 and 128):
6742 // 76 54 32 10
6743 // [F3 F2 F1 F0]
6744 const uint16x4_t m64_u_factorsRight_0123 = vrshrn_n_u32(vandq_u32(m128_u_sourceX_0123_fixed16, vdupq_n_u32(0x0000FFFFu)), 9);
6745 const uint16x4_t m64_u_factorsRight_4567 = vrshrn_n_u32(vandq_u32(m128_u_sourceX_4567_fixed16, vdupq_n_u32(0x0000FFFFu)), 9);
6746
6747 // as we have the pixel information de-interleaved, we can store all 8 interpolation factors together into one 8x8 bit register:
6748 const uint16x8_t m128_u_factorsRight = vcombine_u16(m64_u_factorsRight_0123, m64_u_factorsRight_4567);
6749 const uint8x8_t m64_u_factorsRight = vqmovn_u16(m128_u_factorsRight);
6750 const uint8x8_t m64_u_factorsLeft = vsub_u8(vdup_n_u8(128u), m64_u_factorsRight);
6751
6752
6753
6754 // we determine the intermediate interpolation results for the top row (and we narrow down the 16 bit results 8 bit results)
6755 uint16x8_t m128_muliplicationChannel_0 = vmull_u8(topLeftPixels.val[0], m64_u_factorsLeft);
6756 uint16x8_t m128_muliplicationChannel_1 = vmull_u8(topLeftPixels.val[1], m64_u_factorsLeft);
6757 uint16x8_t m128_muliplicationChannel_2 = vmull_u8(topLeftPixels.val[2], m64_u_factorsLeft);
6758 uint16x8_t m128_muliplicationChannel_3 = vmull_u8(topLeftPixels.val[3], m64_u_factorsLeft);
6759
6760 m128_muliplicationChannel_0 = vmlal_u8(m128_muliplicationChannel_0, topRightPixels.val[0], m64_u_factorsRight);
6761 m128_muliplicationChannel_1 = vmlal_u8(m128_muliplicationChannel_1, topRightPixels.val[1], m64_u_factorsRight);
6762 m128_muliplicationChannel_2 = vmlal_u8(m128_muliplicationChannel_2, topRightPixels.val[2], m64_u_factorsRight);
6763 m128_muliplicationChannel_3 = vmlal_u8(m128_muliplicationChannel_3, topRightPixels.val[3], m64_u_factorsRight);
6764
6765 uint8x8_t m64_topRowChannel_0 = vrshrn_n_u16(m128_muliplicationChannel_0, 7);
6766 uint8x8_t m64_topRowChannel_1 = vrshrn_n_u16(m128_muliplicationChannel_1, 7);
6767 uint8x8_t m64_topRowChannel_2 = vrshrn_n_u16(m128_muliplicationChannel_2, 7);
6768 uint8x8_t m64_topRowChannel_3 = vrshrn_n_u16(m128_muliplicationChannel_3, 7);
6769
6770
6771
6772 // we determine the intermediate interpolation results for the bottom row (and we narrow down the 16 bit results 8 bit results)
6773 m128_muliplicationChannel_0 = vmull_u8(bottomLeftPixels.val[0], m64_u_factorsLeft);
6774 m128_muliplicationChannel_1 = vmull_u8(bottomLeftPixels.val[1], m64_u_factorsLeft);
6775 m128_muliplicationChannel_2 = vmull_u8(bottomLeftPixels.val[2], m64_u_factorsLeft);
6776 m128_muliplicationChannel_3 = vmull_u8(bottomLeftPixels.val[3], m64_u_factorsLeft);
6777
6778 m128_muliplicationChannel_0 = vmlal_u8(m128_muliplicationChannel_0, bottomRightPixels.val[0], m64_u_factorsRight);
6779 m128_muliplicationChannel_1 = vmlal_u8(m128_muliplicationChannel_1, bottomRightPixels.val[1], m64_u_factorsRight);
6780 m128_muliplicationChannel_2 = vmlal_u8(m128_muliplicationChannel_2, bottomRightPixels.val[2], m64_u_factorsRight);
6781 m128_muliplicationChannel_3 = vmlal_u8(m128_muliplicationChannel_3, bottomRightPixels.val[3], m64_u_factorsRight);
6782
6783 uint8x8_t m64_bottomRowChannel_0 = vrshrn_n_u16(m128_muliplicationChannel_0, 7);
6784 uint8x8_t m64_bottomRowChannel_1 = vrshrn_n_u16(m128_muliplicationChannel_1, 7);
6785 uint8x8_t m64_bottomRowChannel_2 = vrshrn_n_u16(m128_muliplicationChannel_2, 7);
6786 uint8x8_t m64_bottomRowChannel_3 = vrshrn_n_u16(m128_muliplicationChannel_3, 7);
6787
6788
6789
6790 // finnally we determine the interpolation result between top and bottom row
6791 m128_muliplicationChannel_0 = vmull_u8(m64_topRowChannel_0, m64_u_factorsTop);
6792 m128_muliplicationChannel_1 = vmull_u8(m64_topRowChannel_1, m64_u_factorsTop);
6793 m128_muliplicationChannel_2 = vmull_u8(m64_topRowChannel_2, m64_u_factorsTop);
6794 m128_muliplicationChannel_3 = vmull_u8(m64_topRowChannel_3, m64_u_factorsTop);
6795
6796 m128_muliplicationChannel_0 = vmlal_u8(m128_muliplicationChannel_0, m64_bottomRowChannel_0, m64_u_factorsBottom);
6797 m128_muliplicationChannel_1 = vmlal_u8(m128_muliplicationChannel_1, m64_bottomRowChannel_1, m64_u_factorsBottom);
6798 m128_muliplicationChannel_2 = vmlal_u8(m128_muliplicationChannel_2, m64_bottomRowChannel_2, m64_u_factorsBottom);
6799 m128_muliplicationChannel_3 = vmlal_u8(m128_muliplicationChannel_3, m64_bottomRowChannel_3, m64_u_factorsBottom);
6800
6801
6802 // we narrow down the interpolation results and we store them
6803 uint8x8x4_t result;
6804 result.val[0] = vrshrn_n_u16(m128_muliplicationChannel_0, 7);
6805 result.val[1] = vrshrn_n_u16(m128_muliplicationChannel_1, 7);
6806 result.val[2] = vrshrn_n_u16(m128_muliplicationChannel_2, 7);
6807 result.val[3] = vrshrn_n_u16(m128_muliplicationChannel_3, 7);
6808
6809 // we write back the results and interleave them automatically
6810 vst4_u8((uint8_t*)targetPixelData, result);
6811
6812 targetPixelData += 8;
6813 }
6814
6815 // we need to process the last pixel again, as this pixel may have received wrong interpolation factors as we always load two successive pixels into our NEON registers
6816 // **TODO** this is just a temporary solution, check how we can avoid this additional step
6817
6818 const unsigned int firstInvalidTargetX = (((sourceWidth - 1u) << 16u) - targetOffsetX_fixed16) / sourceX_T_targetX_fixed16;
6819
6820 for (unsigned int x = firstInvalidTargetX; x < targetWidth; ++x)
6821 {
6822 const unsigned int lastSourcePixelPosition_fixed16 = minmax<int>(0, targetOffsetX_fixed16 + int(sourceX_T_targetX_fixed16 * x), (sourceWidth - 1u) << 16u);
6823
6824 const unsigned int lastSourcePixelLeft = lastSourcePixelPosition_fixed16 >> 16u;
6825 ocean_assert(lastSourcePixelLeft < sourceWidth);
6826 const unsigned int lastSourcePixelRight = min(lastSourcePixelLeft + 1u, sourceWidth - 1u);
6827
6828 const unsigned int factorRight_fixed16 = lastSourcePixelPosition_fixed16 & 0x0000FFFFu;
6829
6830 const unsigned int factorRight = factorRight_fixed16 >> 9u;
6831 const unsigned int factorLeft = 128u - factorRight;
6832
6833 for (unsigned int c = 0u; c < 4u; ++c)
6834 {
6835 ((uint8_t*)(targetPixelData - (targetWidth - x)))[c] = ((((const uint8_t*)(sourceTopRowPixelData + lastSourcePixelLeft))[c] * factorLeft + ((const uint8_t*)(sourceTopRowPixelData + lastSourcePixelRight))[c] * factorRight) * factorTop
6836 + (((const uint8_t*)(sourceBottomRowPixelData + lastSourcePixelLeft))[c] * factorLeft + ((const uint8_t*)(sourceBottomRowPixelData + lastSourcePixelRight))[c] * factorRight) * factorBottom + 8192u) >> 14u;
6837 }
6838 }
6839 }
6840}
6841
6842/// \endcond
6843
6844#endif // OCEAN_WE_KEEP_ALSO_THIS_SLOW_IMPLEMENTATION_SHOWING_A_MORE_GENERIC_APPROACH
6845
6846template <>
6847inline void FrameInterpolatorBilinear::interpolateRowVerticalNEON<float>(const float* sourceRowTop, const float* sourceRowBottom, float* targetRow, const unsigned int elements, const float factorBottom)
6848{
6849 ocean_assert(sourceRowTop != nullptr);
6850 ocean_assert(sourceRowBottom != nullptr);
6851 ocean_assert(targetRow != nullptr);
6852 ocean_assert(elements >= 16u);
6853 ocean_assert(factorBottom >= 0.0f && factorBottom <= 1.0f);
6854
6855 // [1.0f, 1.0f, 1.0f, 1.0f]
6856 const float32x4_t constant_1_f_32x4 = vdupq_n_f32(1.0f);
6857
6858 const float32x4_t factorsBottom_f_32x4 = vdupq_n_f32(factorBottom);
6859 const float32x4_t factorsTop_f_32x4 = vsubq_f32(constant_1_f_32x4, factorsBottom_f_32x4); // factorTop = 1 - factorBottom
6860
6861 for (unsigned int n = 0u; n < elements; n += 16u)
6862 {
6863 if (n + 16u > elements)
6864 {
6865 // the last iteration will not fit into the output frame,
6866 // so we simply shift x left by some elements (at most 15) and we will calculate some elements again
6867
6868 ocean_assert(n >= 16u && elements > 16u);
6869 const unsigned int offset = n - (elements - 16u);
6870 ocean_assert(offset < 16u);
6871
6872 sourceRowTop -= offset;
6873 sourceRowBottom -= offset;
6874 targetRow -= offset;
6875
6876 // the for loop will stop after this iteration
6877 ocean_assert(!(n + 16u < elements));
6878 }
6879
6880 // loading the next four 32 bit values from the top and bottom row
6881 const float32x4_t top_03_32x4 = vld1q_f32(sourceRowTop + 0);
6882 const float32x4_t top_47_32x4 = vld1q_f32(sourceRowTop + 4);
6883 const float32x4_t top_8B_32x4 = vld1q_f32(sourceRowTop + 8);
6884 const float32x4_t top_CF_32x4 = vld1q_f32(sourceRowTop + 12);
6885
6886 const float32x4_t bottom_03_32x4 = vld1q_f32(sourceRowBottom + 0);
6887 const float32x4_t bottom_47_32x4 = vld1q_f32(sourceRowBottom + 4);
6888 const float32x4_t bottom_8B_32x4 = vld1q_f32(sourceRowBottom + 8);
6889 const float32x4_t bottom_CF_32x4 = vld1q_f32(sourceRowBottom + 12);
6890
6891 // interpolatedRow_32x4 = top_32x4 * factorsTop + bottom_32x4 * factorsBottom
6892 float32x4_t interpolatedRow_03_32x4 = vmulq_f32(top_03_32x4, factorsTop_f_32x4);
6893 float32x4_t interpolatedRow_47_32x4 = vmulq_f32(top_47_32x4, factorsTop_f_32x4);
6894 float32x4_t interpolatedRow_8B_32x4 = vmulq_f32(top_8B_32x4, factorsTop_f_32x4);
6895 float32x4_t interpolatedRow_CF_32x4 = vmulq_f32(top_CF_32x4, factorsTop_f_32x4);
6896
6897 interpolatedRow_03_32x4 = vmlaq_f32(interpolatedRow_03_32x4, bottom_03_32x4, factorsBottom_f_32x4);
6898 interpolatedRow_47_32x4 = vmlaq_f32(interpolatedRow_47_32x4, bottom_47_32x4, factorsBottom_f_32x4);
6899 interpolatedRow_8B_32x4 = vmlaq_f32(interpolatedRow_8B_32x4, bottom_8B_32x4, factorsBottom_f_32x4);
6900 interpolatedRow_CF_32x4 = vmlaq_f32(interpolatedRow_CF_32x4, bottom_CF_32x4, factorsBottom_f_32x4);
6901
6902 // writing back the four interpolated 32 bit results
6903 vst1q_f32(targetRow + 0, interpolatedRow_03_32x4);
6904 vst1q_f32(targetRow + 4, interpolatedRow_47_32x4);
6905 vst1q_f32(targetRow + 8, interpolatedRow_8B_32x4);
6906 vst1q_f32(targetRow + 12, interpolatedRow_CF_32x4);
6907
6908 sourceRowTop += 16;
6909 sourceRowBottom += 16;
6910 targetRow += 16;
6911 }
6912}
6913
6914template <>
6915inline void FrameInterpolatorBilinear::interpolateRowHorizontalNEON<float, 1u>(const float* extendedSourceRow, float* targetRow, const unsigned int targetWidth, const unsigned int channels, const unsigned int* interpolationLocations, const float* interpolationFactorsRight)
6916{
6917 ocean_assert(extendedSourceRow != nullptr);
6918 ocean_assert(targetRow != nullptr);
6919 ocean_assert(targetWidth >= 8u);
6920 ocean_assert(interpolationLocations != nullptr);
6921 ocean_assert(interpolationFactorsRight != nullptr);
6922
6923 ocean_assert_and_suppress_unused(channels == 1u, channels);
6924
6925 // [1.0f, 1.0f, 1.0f, 1.0f]
6926 const float32x4_t constant_1_f_32x4 = vdupq_n_f32(1.0f);
6927
6928 for (unsigned int x = 0; x < targetWidth; x += 8u)
6929 {
6930 if (x + 8u > targetWidth)
6931 {
6932 // the last iteration will not fit into the output frame,
6933 // so we simply shift x left by some pixels (at most 7) and we will calculate some pixels again
6934
6935 ocean_assert(x >= 8u && targetWidth > 8u);
6936 const unsigned int newX = targetWidth - 8u;
6937
6938 ocean_assert(x > newX);
6939 const unsigned int offset = x - newX;
6940
6941 targetRow -= offset;
6942 interpolationLocations -= offset;
6943 interpolationFactorsRight -= offset;
6944
6945 x = newX;
6946
6947 // the for loop will stop after this iteration
6948 ocean_assert(!(x + 8u < targetWidth));
6949 }
6950
6951 // we load the left and the right pixels (for four resulting target pixels)
6952
6953 const float32x2_t pixel_0_f_32x2 = vld1_f32(extendedSourceRow + interpolationLocations[0]);
6954 const float32x2_t pixel_1_f_32x2 = vld1_f32(extendedSourceRow + interpolationLocations[1]);
6955 const float32x4_t pixel_01_f_32x4 = vcombine_f32(pixel_0_f_32x2, pixel_1_f_32x2);
6956
6957 const float32x2_t pixel_2_f_32x2 = vld1_f32(extendedSourceRow + interpolationLocations[2]);
6958 const float32x2_t pixel_3_f_32x2 = vld1_f32(extendedSourceRow + interpolationLocations[3]);
6959 const float32x4_t pixel_23_f_32x4 = vcombine_f32(pixel_2_f_32x2, pixel_3_f_32x2);
6960
6961 const float32x2_t pixel_4_f_32x2 = vld1_f32(extendedSourceRow + interpolationLocations[4]);
6962 const float32x2_t pixel_5_f_32x2 = vld1_f32(extendedSourceRow + interpolationLocations[5]);
6963 const float32x4_t pixel_45_f_32x4 = vcombine_f32(pixel_4_f_32x2, pixel_5_f_32x2);
6964
6965 const float32x2_t pixel_6_f_32x2 = vld1_f32(extendedSourceRow + interpolationLocations[6]);
6966 const float32x2_t pixel_7_f_32x2 = vld1_f32(extendedSourceRow + interpolationLocations[7]);
6967 const float32x4_t pixel_67_f_32x4 = vcombine_f32(pixel_6_f_32x2, pixel_7_f_32x2);
6968
6969 const float32x4_t factorsRight_0123_f_32x4 = vld1q_f32(interpolationFactorsRight + 0);
6970 const float32x4_t factorsLeft_0123_f_32x4 = vsubq_f32(constant_1_f_32x4, factorsRight_0123_f_32x4);
6971 const float32x4x2_t factorsLeftRight_0123_f_32x4_2 = vzipq_f32(factorsLeft_0123_f_32x4, factorsRight_0123_f_32x4);
6972
6973 const float32x4_t factorsRight_4567_f_32x4 = vld1q_f32(interpolationFactorsRight + 4);
6974 const float32x4_t factorsLeft_4567_f_32x4 = vsubq_f32(constant_1_f_32x4, factorsRight_4567_f_32x4);
6975 const float32x4x2_t factorsLeftRight_4567_f_32x4_2 = vzipq_f32(factorsLeft_4567_f_32x4, factorsRight_4567_f_32x4);
6976
6977 const float32x4_t multiplied_01_f_32x4 = vmulq_f32(pixel_01_f_32x4, factorsLeftRight_0123_f_32x4_2.val[0]);
6978 const float32x4_t multiplied_23_f_32x4 = vmulq_f32(pixel_23_f_32x4, factorsLeftRight_0123_f_32x4_2.val[1]);
6979
6980 const float32x4_t multiplied_45_f_32x4 = vmulq_f32(pixel_45_f_32x4, factorsLeftRight_4567_f_32x4_2.val[0]);
6981 const float32x4_t multiplied_67_f_32x4 = vmulq_f32(pixel_67_f_32x4, factorsLeftRight_4567_f_32x4_2.val[1]);
6982
6983 const float32x2_t result_01_f_32x2 = vpadd_f32(vget_low_f32(multiplied_01_f_32x4), vget_high_f32(multiplied_01_f_32x4));
6984 const float32x2_t result_23_f_32x2 = vpadd_f32(vget_low_f32(multiplied_23_f_32x4), vget_high_f32(multiplied_23_f_32x4));
6985
6986 const float32x2_t result_45_f_32x2 = vpadd_f32(vget_low_f32(multiplied_45_f_32x4), vget_high_f32(multiplied_45_f_32x4));
6987 const float32x2_t result_67_f_32x2 = vpadd_f32(vget_low_f32(multiplied_67_f_32x4), vget_high_f32(multiplied_67_f_32x4));
6988
6989 const float32x4_t result_0123_f_32x4 = vcombine_f32(result_01_f_32x2, result_23_f_32x2);
6990 const float32x4_t result_4567_f_32x4 = vcombine_f32(result_45_f_32x2, result_67_f_32x2);
6991
6992 vst1q_f32(targetRow + 0, result_0123_f_32x4);
6993 vst1q_f32(targetRow + 4, result_4567_f_32x4);
6994
6995 targetRow += 8;
6996 interpolationLocations += 8;
6997 interpolationFactorsRight += 8;
6998 }
6999}
7000
7001template <>
7002inline void FrameInterpolatorBilinear::scaleSubset<float, float, 1u>(const float* source, float* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
7003{
7004 ocean_assert(source != nullptr && target != nullptr);
7005 ocean_assert(sourceWidth >= 1u && sourceHeight >= 1u);
7006 ocean_assert(targetWidth >= 1u && targetHeight >= 1u);
7007 ocean_assert(sourceX_s_targetX > 0.0 && sourceY_s_targetY > 0.0);
7008
7009 ocean_assert(sourceWidth != targetWidth || sourceHeight != targetHeight || sourceX_s_targetX != 1.0 || sourceY_s_targetY != 1.0);
7010
7011 const unsigned int sourceStrideElements = sourceWidth * 1u + sourcePaddingElements;
7012 const unsigned int targetStrideElements = targetWidth * 1u + targetPaddingElements;
7013
7014 using InterpolateRowVerticalFunction = void (*)(const float*, const float*, float*, const unsigned int, const float);
7015 using InterpolateRowHorizontalFunction = void (*)(const float*, float*, const unsigned int, const unsigned int, const unsigned int*, const float*);
7016
7017 InterpolateRowVerticalFunction interpolateRowVerticalFunction = interpolateRowVertical<float>;
7018 InterpolateRowHorizontalFunction interpolateRowHorizontalFunction = interpolateRowHorizontal<float, 1u>;
7019
7020 if (sourceWidth * 1u >= 16u)
7021 {
7022 interpolateRowVerticalFunction = interpolateRowVerticalNEON<float>;
7023 }
7024
7025 if (targetWidth >= 8u)
7026 {
7027 interpolateRowHorizontalFunction = interpolateRowHorizontalNEON<float, 1u>;
7028 }
7029
7030 target += targetStrideElements * firstTargetRow;
7031
7032 const float sourceX_T_targetX = float(sourceX_s_targetX);
7033 const float sourceY_T_targetY = float(sourceY_s_targetY);
7034
7035 // See the generic template function for a detailed documentation regarding interpolation factors.
7036
7037 Memory memoryIntermediateExtendedRow;
7038 Memory memoryHorizontalInterpolationLocations;
7039 Memory memoryHorizontalInterpolationFactorsRight;
7040
7041 if (sourceWidth != targetWidth || sourceX_T_targetX != 1.0f)
7042 {
7043 // in case we are scaling the width of the frame, we use an intermediate buffer and pre-calculated interpolation locations and factors
7044
7045 memoryIntermediateExtendedRow = Memory::create<float>(sourceWidth + 1u); // one additional pixel
7046
7047 memoryHorizontalInterpolationLocations = Memory::create<unsigned int>(targetWidth); // one offset for each target pixel
7048
7049 memoryHorizontalInterpolationFactorsRight = Memory::create<float>(targetWidth); // one factors (right) for each target pixel
7050 }
7051
7052 if (memoryHorizontalInterpolationLocations)
7053 {
7054 ocean_assert(memoryHorizontalInterpolationFactorsRight);
7055
7056 if (targetWidth >= 4u)
7057 {
7058 const float32x4_t sourceX_T_targetX_f_32x4 = vdupq_n_f32(sourceX_T_targetX);
7059 const float32x4_t targetOffsetX_f_32x4 = vdupq_n_f32(sourceX_T_targetX * 0.5f - 0.5f);
7060
7061 // [0.0f, 0.0f, 0.0f, 0.0f]
7062 const float32x4_t constant_0_f_32x4 = vdupq_n_f32(0);
7063
7064 // [4.0f, 4.0f, 4.0f, 4.0f]
7065 const float32x4_t constant_4_f_32x4 = vdupq_n_f32(4.0f);
7066
7067 // we store 4 integers: [sourceWidth - 1, sourceWidth - 1, sourceWidth - 1, sourceWidth - 1]
7068 const uint32x4_t sourceWidth_1_u_32x4 = vdupq_n_u32(sourceWidth - 1u);
7069
7070 // [0.0f, 1.0f, 2.0f, 3.0f]
7071 const float f_0123[4] = {0.0f, 1.0f, 2.0f, 3.0f};
7072 float32x4_t x_0123_f_32x4 = vld1q_f32(f_0123);
7073
7074 // we pre-calculate the interpolation factors and pixel locations in horizontal direction
7075
7076 for (unsigned int x = 0u; x < targetWidth; x += 4u)
7077 {
7078 if (x + 4u > targetWidth)
7079 {
7080 // the last iteration will not fit into the output frame,
7081 // so we simply shift x left by some pixels (at most 3) and we will calculate some pixels again
7082
7083 ocean_assert(x >= 4u && targetWidth > 4u);
7084 const unsigned int newX = targetWidth - 4u;
7085
7086 ocean_assert(x > newX);
7087 const unsigned int offset = x - newX;
7088
7089 x = newX;
7090
7091 x_0123_f_32x4 = vsubq_f32(x_0123_f_32x4, vdupq_n_f32(float(offset)));
7092
7093 // the for loop will stop after this iteration
7094 ocean_assert(!(x + 4u < targetWidth));
7095 }
7096
7097 // we calculate the four source locations for our four target locations
7098 const float32x4_t sourceX_0123_f_32x4 = vmaxq_f32(constant_0_f_32x4, vaddq_f32(targetOffsetX_f_32x4, vmulq_f32(sourceX_T_targetX_f_32x4, x_0123_f_32x4)));
7099
7100 // now we determine the pixel/integer accurate source locations
7101 // left = min(floor(sourceX), sourceWidth - 1)
7102 uint32x4_t left_0123_u_32x4 = vminq_u32(vcvtq_u32_f32(sourceX_0123_f_32x4), sourceWidth_1_u_32x4); // no rounding here
7103
7104 // we store the offsets we have calculated
7105 vst1q_u32(memoryHorizontalInterpolationLocations.data<unsigned int>() + x, left_0123_u_32x4);
7106
7107 // factorRight = sourcceX - float(left)
7108 const float32x4_t factorsRight_f_32x4 = vsubq_f32(sourceX_0123_f_32x4, vcvtq_f32_u32(left_0123_u_32x4));
7109
7110 vst1q_f32(memoryHorizontalInterpolationFactorsRight.data<float>() + x, factorsRight_f_32x4);
7111
7112 // [x + 0, x + 1, x + 2, x + 3] + [4, 4, 4, 4]
7113 x_0123_f_32x4 = vaddq_f32(x_0123_f_32x4, constant_4_f_32x4);
7114 }
7115 }
7116 else
7117 {
7118 const float targetOffsetX = sourceX_T_targetX * 0.5f - 0.5f;
7119
7120 // we pre-calculate the interpolation factors and pixel locations in horizontal direction
7121
7122 for (unsigned int x = 0u; x < targetWidth; ++x)
7123 {
7124 const float sourceX = max(0.0f, targetOffsetX + float(x) * sourceX_T_targetX);
7125
7126 const unsigned int left = min((unsigned int)sourceX, sourceWidth - 1u); // no rounding here
7127
7128 memoryHorizontalInterpolationLocations.data<unsigned int>()[x] = left;
7129
7130 const float factorRight = sourceX - float(left);
7131 ocean_assert(factorRight >= 0.0f && factorRight <= 1.0f);
7132
7133 memoryHorizontalInterpolationFactorsRight.data<float>()[x] = factorRight;
7134 }
7135 }
7136 }
7137
7138 const float targetOffsetY = sourceY_T_targetY * 0.5f - 0.5f;
7139
7140 for (unsigned int y = firstTargetRow; y < firstTargetRow + numberTargetRows; ++y)
7141 {
7142 const float sourceY = minmax<float>(0.0f, targetOffsetY + sourceY_T_targetY * float(y), float(sourceHeight) - 1.0f);
7143
7144 const unsigned int sourceRowTop = (unsigned int)sourceY; // we must not round here
7145 const float factorBottom = sourceY - float(sourceRowTop);
7146 ocean_assert(factorBottom >= 0.0f && factorBottom <= 1.0f);
7147
7148 const unsigned int sourceRowBottom = min(sourceRowTop + 1u, sourceHeight - 1u);
7149
7150 const float* const sourceTopRow = source + sourceStrideElements * sourceRowTop;
7151 const float* const sourceBottomRow = source + sourceStrideElements * sourceRowBottom;
7152
7153 float* targetRow = nullptr;
7154
7155 if (memoryIntermediateExtendedRow && factorBottom == 0.0f) // we can simply use the top row
7156 {
7157 ocean_assert(sourceWidth != targetWidth);
7158
7159 // we do not need to interpolate two lines, thus we simply need to copy the row (as we need an additional pixel at the end)
7160 memcpy(memoryIntermediateExtendedRow.data<float>(), sourceTopRow, sourceWidth * sizeof(float));
7161 }
7162 else
7163 {
7164 // in case we do not scale the width of the frame, we can write the result to the target frame directly
7165 targetRow = memoryIntermediateExtendedRow.isNull() ? target : memoryIntermediateExtendedRow.data<float>();
7166
7167 ocean_assert(targetRow != nullptr);
7168 ocean_assert(interpolateRowVerticalFunction != nullptr);
7169 interpolateRowVerticalFunction(sourceTopRow, sourceBottomRow, targetRow, sourceWidth * 1u, factorBottom);
7170 }
7171
7172 if (memoryIntermediateExtendedRow) // sourceWidth != targetWidth
7173 {
7174 // we use an extended row (with one additional pixel at the end - equal to the last pixel)
7175 // so we have to copy the last pixel
7176 memoryIntermediateExtendedRow.data<float>()[sourceWidth] = memoryIntermediateExtendedRow.data<float>()[sourceWidth - 1u];
7177
7178 interpolateRowHorizontalFunction(memoryIntermediateExtendedRow.data<float>(), target, targetWidth, 1u, memoryHorizontalInterpolationLocations.data<unsigned int>(), memoryHorizontalInterpolationFactorsRight.data<float>());
7179 }
7180
7181 target += targetStrideElements;
7182 }
7183}
7184
7185#endif // OCEAN_HARDWARE_NEON_VERSION >= 10
7186
7187template <typename T, typename TScale, unsigned int tChannels>
7188void FrameInterpolatorBilinear::scaleSubset(const T* source, T* target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
7189{
7190 static_assert((std::is_same<float, TScale>::value || std::is_same<double, TScale>::value), "Invalid TScale type");
7191
7192 ocean_assert(source != nullptr && target != nullptr);
7193 ocean_assert(sourceWidth != 0u && sourceHeight != 0u);
7194 ocean_assert_and_suppress_unused(targetWidth >= 1u && targetHeight >= 1u, targetHeight);
7195 ocean_assert(sourceX_s_targetX > 0.0 && sourceY_s_targetY > 0.0);
7196
7197 const unsigned int sourceStrideElements = sourceWidth * tChannels + sourcePaddingElements;
7198 const unsigned int targetStrideElements = targetWidth * tChannels + targetPaddingElements;
7199
7200 const TScale sourceX_T_targetX = TScale(sourceX_s_targetX);
7201 const TScale sourceY_T_targetY = TScale(sourceY_s_targetY);
7202
7203 /*
7204 * We determine the sub-pixel accurate source location for each target pixel as follows:
7205 *
7206 * Example with a downsampling by factor 4:
7207 * sourceRow with 12 pixels: | 0 1 2 3 4 5 6 7 8 9 A B |
7208 * targetRow with 3 pixels: | 0 1 2 |
7209 *
7210 * Thus, the source row can be separated into three blocks;
7211 * and we want to extract the color information from the center of the blocks:
7212 * sourceRow with 12 pixels: | 0 1 2 3 | 4 5 6 7 | 8 9 A B |
7213 * targetRow with 3 pixels: | 0 | 1 | 2 | (targetTSourceX = 4)
7214 *
7215 * Thus, we add 0.5 to each target coordinate before converting it to a source location;
7216 * and subtract 0.5 again afterwards:
7217 * sourceX = (targetX + 0.5) * targetTSourceX - 0.5
7218 *
7219 * e.g., (0 + 0.5) * 4 - 0.5 = 1.5
7220 * (1 + 0.5) * 4 - 0.5 = 5.5
7221 *
7222 *
7223 * Example with a downsampling by factor 3:
7224 * sourceRow with 9 pixels: | 0 1 2 3 4 5 6 7 8 |
7225 * targetRow with 3 pixels: | 0 1 2 |
7226 *
7227 * sourceRow with 9 pixels: | 0 1 2 | 3 4 5 | 6 7 8 |
7228 * targetRow with 3 pixels: | 0 | 1 | 2 | (targetTSourceX = 3)
7229 *
7230 * e.g., (0 + 0.5) * 3 - 0.5 = 1
7231 * (1 + 0.5) * 3 - 0.5 = 4
7232 *
7233 *
7234 * Example with a downsampling by factor 2:
7235 * sourceRow with 6 pixels: | 0 1 2 3 4 5 |
7236 * targetRow with 3 pixels: | 0 1 2 |
7237 *
7238 * sourceRow with 6 pixels: | 0 1 | 2 3 | 4 5 |
7239 * targetRow with 3 pixels: | 0 | 1 | 2 | (targetTSourceX = 2)
7240 *
7241 * e.g., (0 + 0.5) * 2 - 0.5 = 0.5
7242 * (1 + 0.5) * 2 - 0.5 = 2.5
7243 *
7244 *
7245 * we can simplify the calculation (as we have a constant term):
7246 * sourceX = (targetX * targetTSourceX) + (0.5 * targetTSourceX - 0.5)
7247 */
7248
7249 const TScale sourceX_T_targetXOffset = sourceX_T_targetX * TScale(0.5) - TScale(0.5);
7250 const TScale sourceY_T_targetYOffset = sourceY_T_targetY * TScale(0.5) - TScale(0.5);
7251
7252 const TScale sourceWidth_1 = TScale(sourceWidth - 1u);
7253 const TScale sourceHeight_1 = TScale(sourceHeight - 1u);
7254
7255 target += targetStrideElements * firstTargetRow;
7256
7257 for (unsigned int y = firstTargetRow; y < firstTargetRow + numberTargetRows; ++y)
7258 {
7259 const TScale sy = minmax(TScale(0), sourceY_T_targetYOffset + sourceY_T_targetY * TScale(y), sourceHeight_1);
7260 ocean_assert(sy >= TScale(0) && sy < TScale(sourceHeight));
7261
7262 const unsigned int sTop = (unsigned int)sy;
7263 ocean_assert(sy >= TScale(sTop));
7264
7265 const TScale factorBottom = sy - TScale(sTop);
7266 ocean_assert(factorBottom >= TScale(0) && factorBottom <= TScale(1));
7267
7268 const TScale factorTop = TScale(1) - factorBottom;
7269 ocean_assert(factorTop >= TScale(0) && factorTop <= TScale(1));
7270
7271 const T* const sourceTop = source + sTop * sourceStrideElements;
7272 const T* const sourceBottom = (sTop + 1u < sourceHeight) ? sourceTop + sourceStrideElements : sourceTop;
7273
7274 for (unsigned int x = 0; x < targetWidth; ++x)
7275 {
7276 const TScale sx = minmax(TScale(0), sourceX_T_targetXOffset + sourceX_T_targetX * TScale(x), sourceWidth_1);
7277 ocean_assert(sx >= TScale(0) && sx < TScale(sourceWidth));
7278
7279 const unsigned int sLeft = (unsigned int)sx;
7280 ocean_assert(sx >= TScale(sLeft));
7281
7282 const TScale factorRight = sx - TScale(sLeft);
7283 ocean_assert(factorRight >= TScale(0) && factorRight <= TScale(1));
7284
7285 const TScale factorLeft = TScale(1) - factorRight;
7286 ocean_assert(factorLeft >= TScale(0) && factorLeft <= TScale(1));
7287
7288 const unsigned int sourceRightOffset = sLeft + 1u < sourceWidth ? tChannels : 0u;
7289
7290 const T* const sourceTopLeft = sourceTop + sLeft * tChannels;
7291 const T* const sourceBottomLeft = sourceBottom + sLeft * tChannels;
7292
7293 const TScale factorTopLeft = factorTop * factorLeft;
7294 const TScale factorTopRight = factorTop * factorRight;
7295 const TScale factorBottomLeft = factorBottom * factorLeft;
7296 const TScale factorBottomRight = factorBottom * factorRight;
7297
7298 for (unsigned int n = 0u; n < tChannels; ++n)
7299 {
7300 target[n] = T(TScale(sourceTopLeft[n]) * factorTopLeft + TScale(sourceTopLeft[sourceRightOffset + n]) * factorTopRight
7301 + TScale(sourceBottomLeft[n]) * factorBottomLeft + TScale(sourceBottomLeft[sourceRightOffset + n]) * factorBottomRight);
7302 }
7303
7304 target += tChannels;
7305 }
7306
7307 target += targetPaddingElements;
7308 }
7309}
7310
7311template <unsigned int tChannels>
7312void FrameInterpolatorBilinear::rotate8BitPerChannelSubset(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const Scalar horizontalAnchorPosition, const Scalar verticalAnchorPosition, const Scalar angle, const uint8_t* borderColor, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
7313{
7314 static_assert(tChannels != 0u, "Invalid channel number!");
7315
7316 ocean_assert(firstTargetRow + numberTargetRows <= height);
7317
7318 using PixelType = typename DataType<uint8_t, tChannels>::Type;
7319
7320 const unsigned int targetStrideElements = width * tChannels + targetPaddingElements;
7321
7322 uint8_t zeroColor[tChannels] = {uint8_t(0)};
7323 const PixelType bColor = borderColor ? *(const PixelType*)borderColor : *(const PixelType*)zeroColor;
7324
7325 const SquareMatrix3 rotationMatrix3(Rotation(0, 0, 1, angle));
7326 const SquareMatrix2 rotationMatrix2(rotationMatrix3(0, 0), rotationMatrix3(1, 0), rotationMatrix3(0, 1), rotationMatrix3(1, 1));
7327
7328 const Scalar width_1 = Scalar(width - 1u);
7329 const Scalar height_1 = Scalar(height - 1u);
7330 const Vector2 anchorPosition(horizontalAnchorPosition, verticalAnchorPosition);
7331
7332 for (unsigned int y = firstTargetRow; y < firstTargetRow + numberTargetRows; ++y)
7333 {
7334 PixelType* targetPixel = (PixelType*)(target + y * targetStrideElements);
7335
7336 const Scalar floatY = Scalar(y);
7337
7338 for (unsigned int x = 0; x < width; ++x)
7339 {
7340 const Vector2 sourceLocation(anchorPosition + rotationMatrix2 * (Vector2(Scalar(x), floatY) - anchorPosition));
7341
7342 if (sourceLocation.x() >= 0 && sourceLocation.y() >= 0 && sourceLocation.x() <= width_1 && sourceLocation.y() <= height_1)
7343 {
7344 interpolatePixel8BitPerChannel<tChannels, PC_TOP_LEFT>(source, width, height, sourcePaddingElements, sourceLocation, (uint8_t*)(targetPixel));
7345 }
7346 else
7347 {
7348 *targetPixel = bColor;
7349 }
7350
7351 ++targetPixel;
7352 }
7353 }
7354}
7355
7356} // namespace CV
7357
7358} // namespace Ocean
7359
7360#endif // META_OCEAN_CV_FRAME_INTERPOLATOR_BILINEAR_H
virtual VectorT3< T > vector(const VectorT2< T > &distortedImagePoint, const bool makeUnitVector=true) const =0
Returns a vector starting at the camera's center and intersecting a given 2D point in the image.
virtual unsigned int width() const =0
Returns the width of the camera image.
virtual unsigned int height() const =0
Returns the height of the camera image.
virtual VectorT2< T > projectToImageIF(const VectorT3< T > &objectPoint) const =0
Projects a 3D object point into the camera frame.
virtual bool isValid() const =0
Returns whether this camera is valid.
Helper class allowing to determine the offset that is necessary to access the alpha channel.
Definition FrameBlender.h:60
static constexpr unsigned int data()
Returns the offset that is applied to access the first data channel.
Definition FrameBlender.h:1171
static uint8_t fullTransparent8Bit()
Returns the alpha value for a fully transparent pixel.
Definition FrameBlender.h:906
static bool subFrame(const T *source, T *target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int channels, const unsigned int sourceLeft, const unsigned int sourceTop, const unsigned int targetLeft, const unsigned int targetTop, const unsigned int width, const unsigned int height, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements)
Copies a sub-frame of a given frame into a second frame while both frames might have an individual nu...
Definition FrameConverter.h:3325
The following comfort class provides comfortable functions simplifying prototyping applications but a...
Definition FrameInterpolatorBilinear.h:65
static bool homographies(const Frame &input, Frame &output, const SquareMatrix3 homographies[4], const Vector2 &outputQuadrantCenter, const uint8_t *borderColor=nullptr, Worker *worker=nullptr, const PixelPositionI &outputOrigin=PixelPositionI(0, 0))
Transforms a given input frame into an output frame (with arbitrary frame dimension) by application o...
static bool zoom(const Frame &source, Frame &target, const Scalar zoomFactor, Worker *worker=nullptr)
Zooms into a given input frame (or zooms out) and stores the zoomed image content in an output frame.
static bool homographyMask(const Frame &input, Frame &output, Frame &outputMask, const SquareMatrix3 &input_H_output, Worker *worker=nullptr, const uint8_t maskValue=0xFF, const PixelPositionI &outputOrigin=PixelPositionI(0, 0))
Transforms a given input frame into an output frame (with arbitrary frame dimension) by application o...
static bool lookupMask(const Frame &input, Frame &output, Frame &outputMask, const LookupTable &input_LT_output, const bool offset, Worker *worker=nullptr, const uint8_t maskValue=0xFF)
Transforms a given input frame into an output frame by application of an interpolation lookup table a...
static bool homographyWithCameraMask(const AnyCamera &inputCamera, const AnyCamera &outputCamera, const Frame &input, Frame &output, Frame &outputMask, const SquareMatrix3 &homography, Worker *worker=nullptr, const uint8_t maskValue=0xFFu)
Transforms a given input frame into an output frame by application of a homography.
static bool rotate(const Frame &source, Frame &target, const Scalar horizontalAnchorPosition, const Scalar verticalAnchorPosition, const Scalar angle, Worker *worker=nullptr, const uint8_t *borderColor=nullptr)
Rotates a given frame by a bilinear interpolation.
static bool resize(const Frame &source, Frame &target, Worker *worker=nullptr)
Resizes/rescales a given frame by application of a bilinear interpolation.
static bool homographiesMask(const Frame &input, Frame &output, Frame &outputMask, const SquareMatrix3 *homographies, const Vector2 &outputQuadrantCenter, Worker *worker=nullptr, const uint8_t maskValue=0xFF, const PixelPositionI &outputOrigin=PixelPositionI(0, 0))
Transforms a given input frame into an output frame (with arbitrary frame dimension) by application o...
static bool interpolatePixel(const TSource *frame, const unsigned int channels, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const PixelCenter pixelCenter, const VectorT2< TScalar > &position, TTarget *result, const TIntermediate &resultBias=TIntermediate(0))
Determines the interpolated pixel values for a given pixel position in a frame with arbitrary data ty...
Definition FrameInterpolatorBilinear.h:1552
static bool resampleCameraImage(const Frame &sourceFrame, const AnyCamera &sourceCamera, const SquareMatrix3 &source_R_target, const AnyCamera &targetCamera, Frame &targetFrame, LookupCorner2< Vector2 > *source_OLT_target=nullptr, Worker *worker=nullptr, const unsigned int binSizeInPixel=8u, const void *borderColor=nullptr)
Re-samples a camera image which has been captured with a camera profile as if the image would have be...
static bool homographyWithCamera(const PinholeCamera &inputCamera, const PinholeCamera &outputCamera, const Frame &input, Frame &output, const SquareMatrix3 &homography, const bool useDistortionParameters, const uint8_t *borderColor=nullptr, Worker *worker=nullptr)
Transforms a given input frame into an output frame by application of a homography.
static bool lookup(const Frame &input, Frame &output, const LookupTable &input_LT_output, const bool offset, const void *borderColor, Worker *worker=nullptr)
Transforms a given input frame into an output frame by application of an interpolation lookup table.
static bool affine(const Frame &source, Frame &target, const SquareMatrix3 &source_A_target, const uint8_t *borderColor=nullptr, Worker *worker=nullptr, const PixelPositionI &targetOrigin=PixelPositionI(0, 0))
Applies an affine transformation to an image.
static bool interpolatePixel8BitPerChannel(const uint8_t *frame, const unsigned int channels, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const PixelCenter pixelCenter, const VectorT2< TScalar > &position, uint8_t *result)
Determines the interpolated pixel values for a given pixel position in an 8 bit per channel frame.
Definition FrameInterpolatorBilinear.h:1459
static bool homography(const Frame &input, Frame &output, const SquareMatrix3 &input_H_output, const void *borderColor=nullptr, Worker *worker=nullptr, const PixelPositionI &outputOrigin=PixelPositionI(0, 0))
Transforms a given input frame into an output frame (with arbitrary frame dimension) by application o...
This class implements highly optimized interpolation functions with fixed properties.
Definition FrameInterpolatorBilinear.h:346
static void resize400x400To256x256_8BitPerChannel(const uint8_t *const source, uint8_t *const target, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements)
Resizes a given FORMAT_Y8 frame with resolution 400x400 to a FORMAT_Y8 frame with resolution 256x256 ...
static void resize400x400To224x224_8BitPerChannel(const uint8_t *const source, uint8_t *const target, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements)
Resizes a given FORMAT_Y8 frame with resolution 400x400 to a FORMAT_Y8 frame with resolution 224x224 ...
This class implements bilinear frame interpolator functions.
Definition FrameInterpolatorBilinear.h:47
static OCEAN_FORCE_INLINE void interpolate4Pixels8BitPerChannelNEON(const uint8_t *source, const unsigned int offsetsTopLeftElements[4], const unsigned int offsetsTopRightElements[4], const unsigned int offsetsBottomLeftElements[4], const unsigned int offsetsBottomRightElements[4], const unsigned int validPixels[4], const typename DataType< uint8_t, tChannels >::Type &borderColor, const uint32x4_t &m128_factorsRight, const uint32x4_t &m128_factorsBottom, typename DataType< uint8_t, tChannels >::Type *targetPositionPixels)
Interpolates 4 independent pixels concurrently based on already known locations (top-left,...
Definition FrameInterpolatorBilinear.h:4433
static void homography(const T *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 &input_H_output, const T *borderColor, T *output, const PixelPositionI &outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker *worker=nullptr)
Transforms a input frame with (almost) arbitrary pixel format into an output frame by application of ...
Definition FrameInterpolatorBilinear.h:1770
static void resampleCameraImage(const T *sourceFrame, const AnyCamera &sourceCamera, const SquareMatrix3 &source_R_target, const AnyCamera &targetCamera, T *targetFrame, const unsigned int sourceFramePaddingElements, const unsigned int targetFramePaddingElements, LookupCorner2< Vector2 > *source_OLT_target=nullptr, Worker *worker=nullptr, const unsigned int binSizeInPixel=8u, const T *borderColor=nullptr)
Re-samples a camera image which has been captured with a camera profile as if the image would have be...
Definition FrameInterpolatorBilinear.h:1930
static void interpolateRowVerticalNEON(const T *sourceRowTop, const T *sourceRowBottom, T *targetRow, const unsigned int elements, const float factorBottom)
Applies a (vertical) linear interpolation between two rows with arbitrary data types.
static void homographyWithCamera8BitPerChannel(const PinholeCamera &inputCamera, const PinholeCamera &outputCamera, const uint8_t *input, const SquareMatrix3 &homography, const bool useDistortionParameters, const uint8_t *borderColor, uint8_t *output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker *worker=nullptr)
Transforms a given 8 bit per channel input frame into an output frame by application of a homography.
Definition FrameInterpolatorBilinear.h:1836
static void interpolatePixel8BitPerChannel(const uint8_t *frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const VectorT2< TScalar > &position, uint8_t *result)
Determines the interpolated pixel values for a given pixel position in an 8 bit per channel frame.
Definition FrameInterpolatorBilinear.h:1997
static void affine8BitPerChannelSSESubset(const uint8_t *source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3 *source_A_target, const uint8_t *borderColor, uint8_t *target, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int firstTargetRow, const unsigned int numberTargetRows, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements)
Subset function to apply an affine transform to an N-channel, 8-bit unsigned image (using SSE).
Definition FrameInterpolatorBilinear.h:2501
static Scalar patchIntensitySum1Channel(const uint32_t *linedIntegralFrame, const unsigned int frameWidth, const unsigned int frameHeight, const unsigned int lineIntegralFramePaddingElements, const Vector2 &center, const CV::PixelCenter pixelCenter, const unsigned int patchWidth, const unsigned int patchHeight)
Interpolate the sum of intensity values of an image patch in a frame, while the frame is provided as ...
static void homographyWithCameraMask8BitPerChannel(const PinholeCamera &inputCamera, const PinholeCamera &outputCamera, const uint8_t *input, const unsigned int inputPaddingElements, const SquareMatrix3 &homography, uint8_t *output, uint8_t *outputMask, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker *worker=nullptr, const uint8_t maskValue=0xFF)
Transforms a given 8 bit per channel input frame into an output frame by application of a homography.
Definition FrameInterpolatorBilinear.h:1853
static void homographiesMask8BitPerChannelSubset(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 *homographies, uint8_t *output, uint8_t *outputMask, const uint8_t maskValue, const Scalar outputQuadrantCenterX, const Scalar outputQuadrantCenterY, const int outputOriginX, const int outputOriginY, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
Transforms an 8 bit per channel frame using the given homography.
Definition FrameInterpolatorBilinear.h:4655
static void homographiesMask8BitPerChannel(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 homographies[4], uint8_t *output, uint8_t *outputMask, const Vector2 &outputQuadrantCenter, const PixelPositionI &outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker *worker=nullptr, const uint8_t maskValue=0xFF)
Transforms a given 8 bit per channel input frame into an output frame by application of four homograp...
Definition FrameInterpolatorBilinear.h:1823
static void interpolateRowHorizontal8BitPerChannel7BitPrecision(const uint8_t *extendedSourceRow, uint8_t *targetRow, const unsigned int targetWidth, const unsigned int channels, const unsigned int *interpolationLocations, const uint8_t *interpolationFactors)
Applies a (horizontal) linear interpolation for one row with 8 bit per channel.
static void homography8BitPerChannelNEONSubset(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 *input_H_output, const uint8_t *borderColor, uint8_t *output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
Transforms an 8 bit per channel frame using the given homography.
Definition FrameInterpolatorBilinear.h:3614
static OCEAN_FORCE_INLINE void interpolate4Pixels4Channel8BitPerChannelNEON(const uint8x16_t &topLeftPixels_u8x16, const uint8x16_t &topRightPixels_u8x16, const uint8x16_t &bottomLeftPixels_u8x16, const uint8x16_t &bottomRightPixels_u8x16, const uint32x4_t &m128_factorsRight, const uint32x4_t &m128_factorsBottom, typename DataType< uint8_t, 4u >::Type *targetPositionPixels, const bool useOptimizedNEONFactorReplication=false)
Interpolates 4 independent 4-channel pixels using widening byte multiply.
Definition FrameInterpolatorBilinear.h:4321
static void interpolateRowVertical8BitPerChannel7BitPrecisionNEON(const uint8_t *sourceRowTop, const uint8_t *sourceRowBottom, uint8_t *targetRow, const unsigned int elements, const unsigned int factorBottom)
Applies a (vertical) linear interpolation between two rows with 8 bit per channel.
static void homographySubset(const T *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 *input_H_output, const T *borderColor, T *output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
Transforms a frame with (almost) arbitrary pixel format using the given homography.
Definition FrameInterpolatorBilinear.h:2417
static void homographyMask8BitPerChannel(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 &input_H_output, uint8_t *output, uint8_t *outputMask, const PixelPositionI &outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const uint8_t maskValue, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker *worker=nullptr)
Transforms a given 8 bit per channel input frame into an output frame by application of a homography.
Definition FrameInterpolatorBilinear.h:1807
static OCEAN_FORCE_INLINE void interpolate8Pixels1Channel8BitNEON(const uint8x8_t &topLeft_u_8x8, const uint8x8_t &topRight_u_8x8, const uint8x8_t &bottomLeft_u_8x8, const uint8x8_t &bottomRight_u_8x8, const uint8x16_t &factorsRight_factorsBottom_128_u_8x16, uint8_t *targetPositionPixels)
Interpolates 8 independent pixels concurrently of a 1 channel frame, the source pixel locations must ...
Definition FrameInterpolatorBilinear.h:3994
static void homographies8BitPerChannelSubset(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 *homographies, const uint8_t *borderColor, uint8_t *output, const Scalar outputQuadrantCenterX, const Scalar outputQuadrantCenterY, const int outputOriginX, const int outputOriginY, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
Transforms an 8 bit per channel frame using the given homographies.
Definition FrameInterpolatorBilinear.h:4499
static void homographyWithCamera8BitPerChannelSubset(const PinholeCamera *inputCamera, const PinholeCamera *outputCamera, const PinholeCamera::DistortionLookup *outputCameraDistortionLookup, const uint8_t *input, const SquareMatrix3 *normalizedHomography, const bool useDistortionParameters, const uint8_t *borderColor, uint8_t *output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows)
Transforms an 8 bit per channel frame using the given homography.
Definition FrameInterpolatorBilinear.h:4735
static void affine8BitPerChannel(const uint8_t *source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3 &source_A_target, const uint8_t *borderColor, uint8_t *target, const PixelPositionI &targetOrigin, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr)
Apply an affine transforms to a N-channel, 8-bit frame The target frame must have the same pixel form...
Definition FrameInterpolatorBilinear.h:1694
static void homographyWithCameraMask8BitPerChannelSubset(const PinholeCamera *inputCamera, const PinholeCamera *outputCamera, const PinholeCamera::DistortionLookup *outputCameraDistortionLookup, const uint8_t *input, const unsigned int inputPaddingElements, const SquareMatrix3 *normalizedHomography, uint8_t *output, uint8_t *outputMask, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, const uint8_t maskValue, const unsigned int firstRow, const unsigned int numberRows)
Transforms an 8 bit per channel frame using the given homography.
Definition FrameInterpolatorBilinear.h:4781
static void affine8BitPerChannelNEONSubset(const uint8_t *source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3 *source_A_target, const uint8_t *borderColor, uint8_t *target, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int firstTargetRow, const unsigned int numberTargetRows, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements)
Subset function to apply an affine transform to an N-channel, 8-bit unsigned image (using NEON).
Definition FrameInterpolatorBilinear.h:3364
static void lookup(const T *input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable &input_LT_output, const bool offset, const T *borderColor, T *output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker *worker=nullptr, const bool useOptimizedNEON=false, const bool useOptimizedBilinearValuesAndFactorCalculation=false, const bool useOptimizedNEONFactorReplication=false, const bool use4ChannelSamplingFastPaths=false)
Transforms a given input frame into an output frame by application of an interpolation lookup table.
Definition FrameInterpolatorBilinear.h:1870
static void interpolateRowHorizontalNEON(const T *extendedSourceRow, T *targetRow, const unsigned int targetWidth, const unsigned int channels, const unsigned int *interpolationLocations, const float *interpolationFactorsRight)
Applies a (horizontal) linear interpolation for one row with arbitrary data type.
static void scale8BitPerChannelSubset7BitPrecisionNEON(const uint8_t *source, uint8_t *target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int channels, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
Rescales a subset of a given frame with 8 bit per channel by a bilinear interpolation.
Definition FrameInterpolatorBilinear.h:5764
static OCEAN_FORCE_INLINE __m128i interpolate4Pixels8BitPerChannelSSE(const __m128i &m128_sourcesTopLeft, const __m128i &m128_sourcesTopRight, const __m128i &m128_sourcesBottomLeft, const __m128i &m128_sourcesBottomRight, const __m128i &m128_factorsTopLeft, const __m128i &m128_factorsTopRight, const __m128i &m128_factorsBottomLeft, const __m128i &m128_factorsBottomRight)
Interpolates 4 independent pixels concurrently based on already known locations (top-left,...
static void interpolateRowHorizontal(const T *extendedSourceRow, T *targetRow, const unsigned int targetWidth, const unsigned int channels, const unsigned int *interpolationLocations, const float *interpolationFactorsRight)
Applies a (horizontal) linear interpolation for one row with arbitrary data type.
Definition FrameInterpolatorBilinear.h:5729
static void rotate8BitPerChannelSubset(const uint8_t *source, uint8_t *target, const unsigned int width, const unsigned int height, const Scalar horizontalAnchorPosition, const Scalar verticalAnchorPosition, const Scalar angle, const uint8_t *borderColor, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
Rotates a subset of a given frame by a bilinear interpolation.
Definition FrameInterpolatorBilinear.h:7312
static void lookupMask8BitPerChannel(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable &input_LT_output, const bool offset, uint8_t *output, uint8_t *outputMask, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, Worker *worker=nullptr, const uint8_t maskValue=0xFF)
Transforms a given input frame into an output frame by application of an interpolation lookup table.
Definition FrameInterpolatorBilinear.h:1917
static OCEAN_FORCE_INLINE void interpolate4Pixels8BitPerChannelSSE(const uint8_t *source, const unsigned int offsetsTopLeft[4], const unsigned int offsetsTopRight[4], const unsigned int offsetsBottomLeft[4], const unsigned int offsetsBottomRight[4], const unsigned int validPixels[4], const typename DataType< uint8_t, tChannels >::Type &borderColor, const __m128i &m128_factorsRight, const __m128i &m128_factorsBottom, typename DataType< uint8_t, tChannels >::Type *targetPositionPixels)
Interpolates 4 independent pixels concurrently based on already known locations (top-left,...
Definition FrameInterpolatorBilinear.h:3296
static void homographies8BitPerChannel(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 homographies[4], const uint8_t *borderColor, uint8_t *output, const Vector2 &outputQuadrantCenter, const PixelPositionI &outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker *worker=nullptr)
Transforms a given 8 bit per channel input frame into an output frame by application of four homograp...
Definition FrameInterpolatorBilinear.h:1794
static void lookup8BitPerChannelSubset(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable *input_LT_output, const bool offset, const uint8_t *borderColor, uint8_t *output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows)
Transforms a subset of a given input frame with uint8_t as element type into an output frame by appli...
Definition FrameInterpolatorBilinear.h:4829
static void resize(const T *source, T *target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr)
Resizes a given frame with (almost) arbitrary data type (e.g., float, double, int) by using a bilinea...
Definition FrameInterpolatorBilinear.h:1645
static bool coversHomographyInputFrame(const unsigned int inputWidth, const unsigned int inputHeight, const unsigned int outputWidth, const unsigned int outputHeight, const SquareMatrix3 &input_H_output, const int outputOriginX=0, const int outputOriginY=0)
Checks whether the application of a given homography for a specified input frame and output frame cov...
static void scale(const T *source, T *target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr)
Rescales a given frame with arbitrary data type (e.g., float, double, int) by using a bilinear interp...
Definition FrameInterpolatorBilinear.h:1658
static void lookupSubset(const T *input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable *input_LT_output, const bool offset, const T *borderColor, T *output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows)
Transforms a subset of a given input frame with arbitrary element type into an output frame by applic...
Definition FrameInterpolatorBilinear.h:4901
static void scale8BitPerChannelSubset(const uint8_t *source, uint8_t *target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
Resizes a subset of a given frame with 8 bit per channel by a bilinear interpolation.
Definition FrameInterpolatorBilinear.h:5588
static void rotate8BitPerChannel(const uint8_t *source, uint8_t *target, const unsigned int width, const unsigned int height, const Scalar horizontalAnchorPosition, const Scalar verticalAnchorPosition, const Scalar angle, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr, const uint8_t *borderColor=nullptr)
Rotates a given frame by a bilinear interpolation.
Definition FrameInterpolatorBilinear.h:1979
static void interpolateRowVertical(const T *sourceRowTop, const T *sourceRowBottom, T *targetRow, const unsigned int elements, const float factorBottom)
Applies a (vertical) linear interpolation between two rows with arbitrary data types.
Definition FrameInterpolatorBilinear.h:5709
static void homography8BitPerChannel(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 &input_H_output, const uint8_t *borderColor, uint8_t *output, const PixelPositionI &outputOrigin, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, Worker *worker=nullptr)
Transforms a given 8 bit per channel input frame into an output frame by application of a homography.
Definition FrameInterpolatorBilinear.h:1732
static void interpolate1PixelFullAlphaBorder8BitPerChannel(const uint8_t *frame, const unsigned int width, const unsigned int height, const Vector2 &position, uint8_t *result, const unsigned int framePaddingElements)
Determines the interpolated pixel values for a given pixel position in an 8 bit per channel frame wit...
Definition FrameInterpolatorBilinear.h:2176
static void lookupMask8BitPerChannelSubset(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable *input_LT_output, const bool offset, uint8_t *output, uint8_t *outputMask, const uint8_t maskValue, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, const unsigned int firstRow, const unsigned int numberRows)
Transforms a given input frame into an output frame by application of an interpolation lookup table.
Definition FrameInterpolatorBilinear.h:5473
static void affine8BitPerChannelSubset(const uint8_t *source, const unsigned int sourceWidth, const unsigned int sourceHeight, const SquareMatrix3 *source_A_target, const uint8_t *borderColor, uint8_t *target, const unsigned int targetWidth, const unsigned int targetHeight, const unsigned int firstTargetRow, const unsigned int numberTargetRows, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements)
Subset function to apply an affine transform to an N-channel, 8-bit unsigned image.
Definition FrameInterpolatorBilinear.h:2265
static void homography8BitPerChannelSSESubset(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 *input_H_output, const uint8_t *borderColor, uint8_t *output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
Transforms an 8 bit per channel frame using the given homography.
Definition FrameInterpolatorBilinear.h:2686
static void interpolateRowHorizontal8BitPerChannel7BitPrecisionNEON(const uint8_t *extendedSourceRow, uint8_t *targetRow, const unsigned int targetWidth, const unsigned int channels, const unsigned int *interpolationLocations, const uint8_t *interpolationFactors)
Applies a (horizontal) linear interpolation for one row with 8 bit per channel.
static void scale8BitPerChannel(const uint8_t *source, uint8_t *target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr)
Rescales a given frame with 8 bit per data channel by using a bilinear interpolation with user-define...
Definition FrameInterpolatorBilinear.h:5544
LookupCorner2< Vector2 > LookupTable
Definition of a lookup table for 2D vectors.
Definition FrameInterpolatorBilinear.h:55
static void homographyMask8BitPerChannelSubset(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 *input_H_output, uint8_t *output, uint8_t *outputMask, const uint8_t maskValue, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int outputMaskPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
Transforms an 8 bit per channel frame using the given homography.
Definition FrameInterpolatorBilinear.h:4580
static void scaleSubset(const T *source, T *target, const unsigned int sourceWidth, const unsigned int sourceHeight, const unsigned int targetWidth, const unsigned int targetHeight, const double sourceX_s_targetX, const double sourceY_s_targetY, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const unsigned int firstTargetRow, const unsigned int numberTargetRows)
Resizes a subset of a given frame with arbitrary data type by a bilinear interpolation.
Definition FrameInterpolatorBilinear.h:7188
static void homography8BitPerChannelSubset(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const SquareMatrix3 *input_H_output, const uint8_t *borderColor, uint8_t *output, const unsigned int outputWidth, const unsigned int outputHeight, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstOutputRow, const unsigned int numberOutputRows)
Transforms an 8 bit per channel frame using the given homography.
Definition FrameInterpolatorBilinear.h:2340
static void interpolatePixel(const TSource *frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, const VectorT2< TScalar > &position, TTarget *result, const TIntermediate &resultBias=TIntermediate(0))
Determines the interpolated pixel values for a given pixel position in a frame with arbitrary data ty...
Definition FrameInterpolatorBilinear.h:2090
static void lookup8BitPerChannelSubsetNEON(const uint8_t *input, const unsigned int inputWidth, const unsigned int inputHeight, const LookupTable *input_LT_output, const bool offset, const uint8_t *borderColor, uint8_t *output, const unsigned int inputPaddingElements, const unsigned int outputPaddingElements, const unsigned int firstRow, const unsigned int numberRows, const bool useOptimizedNEON=false, const bool useOptimizedBilinearValuesAndFactorCalculation=false, const bool useOptimizedNEONFactorReplication=false, const bool use4ChannelSamplingFastPaths=false)
Transforms a subset of a given input frame into an output frame by application of an interpolation lo...
Definition FrameInterpolatorBilinear.h:5195
static constexpr uint8x8_t create_uint8x8(const uint8_t v0, const uint8_t v1, const uint8_t v2, const uint8_t v3, const uint8_t v4, const uint8_t v5, const uint8_t v6, const uint8_t v7)
Creates a uint8x8_t vector from 8 individual uint8_t values.
Definition NEON.h:591
T y() const
Returns the vertical coordinate position of this object.
Definition PixelPosition.h:468
T x() const
Returns the horizontal coordinate position of this object.
Definition PixelPosition.h:456
static __m128i set128i(const unsigned long long high64, const unsigned long long low64)
Sets a 128i value by two 64 bit values.
Definition SSE.h:3874
static Caller< void > createStatic(typename StaticFunctionPointerMaker< void, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass >::Type function)
Template class allowing to define an array of data types.
Definition DataType.h:27
This class implements Ocean's image class.
Definition Frame.h:1969
void setRelativeTimestamp(const Timestamp &relative)
Sets the relative timestamp of this frame.
Definition Frame.h:4410
bool isValid() const
Returns whether this frame is valid.
Definition Frame.h:4705
void setTimestamp(const Timestamp &timestamp)
Sets the timestamp of this frame.
Definition Frame.h:4405
const Timestamp & timestamp() const
Returns the timestamp of this frame.
Definition Frame.h:4395
const Timestamp & relativeTimestamp() const
Returns the relative timestamp of this frame.
Definition Frame.h:4400
Definition of a frame type composed by the frame dimension, pixel format and pixel origin.
Definition Frame.h:30
size_t sizeY() const
Returns the vertical dimension of this lookup object.
Definition Lookup2.h:960
size_t sizeX() const
Returns the horizontal dimension of this lookup object.
Definition Lookup2.h:954
size_t binsY() const
Returns the number of vertical bins of this lookup object.
Definition Lookup2.h:972
size_t binsX() const
Returns the number of horizontal bins of this lookup object.
Definition Lookup2.h:966
This class implements a 2D lookup object with values at the bins' corners defining the individual loo...
Definition Lookup2.h:636
Vector2 binTopLeftCornerPosition(const size_t binX, const size_t binY) const
Returns the corner position (the top left corner) of a specific bin in relation to the dimension of t...
Definition Lookup2.h:1799
void setBinTopLeftCornerValue(const size_t binX, const size_t binY, const T &value)
Sets the value of one specific lookup bin's top left corner.
Definition Lookup2.h:2215
void bilinearValues(const size_t y, TTarget *values) const
Applies a lookup for an entire row in this lookup object.
Definition Lookup2.h:1877
This class implements an object able to allocate memory.
Definition base/Memory.h:22
static Memory create(const size_t elements)
Creates a new object and allocates enough memory necessary for 'elements' of type T.
Definition base/Memory.h:436
bool isNull() const
Returns whether this object holds any memory.
Definition base/Memory.h:401
void * data()
Returns the pointer to the writable memory which is allocated by this object.
Definition base/Memory.h:303
static constexpr Scalar eps()
static bool isEqual(const T first, const T second)
Returns whether two values are equal up to a small epsilon.
Definition Numeric.h:2395
static Scalar floor(const Scalar value)
static constexpr bool isEqualEps(const Scalar value)
static constexpr bool isNotEqualEps(const Scalar value)
unsigned int width() const
Returns the width of the camera image.
Definition PinholeCamera.h:1452
const SquareMatrixT3< T > & invertedIntrinsic() const
Returns the inverted intrinsic camera matrix.
Definition PinholeCamera.h:1333
const SquareMatrixT3< T > & intrinsic() const
Returns the intrinsic camera matrix.
Definition PinholeCamera.h:1327
unsigned int height() const
Returns the height of the camera image.
Definition PinholeCamera.h:1458
VectorT2< T > normalizedImagePoint2imagePoint(const VectorT2< T > &normalizedImagePoint, const bool distortImagePoint) const
Calculates the image point corresponding to a given normalized image point.
Definition PinholeCamera.h:1792
bool isNull() const
Returns whether this matrix is a zero matrix.
Definition SquareMatrix3.h:1334
const T * data() const
Returns a pointer to the internal values.
Definition SquareMatrix3.h:1047
bool isOrthonormal(const T epsilon=NumericT< T >::eps()) const
Returns whether this matrix is an orthonormal matrix.
Definition SquareMatrix3.h:1366
This class implements tests for the NEON-accelerated bilinear interpolation functions.
Definition TestFrameInterpolatorBilinearNEON.h:27
This class implements a vector with two elements.
Definition Vector2.h:96
const T & x() const noexcept
Returns the x value.
Definition Vector2.h:703
const T & y() const noexcept
Returns the y value.
Definition Vector2.h:715
bool isEqual(const VectorT2< T > &vector, const T eps) const
Returns whether two vectors are equal up to a specified epsilon.
Definition Vector2.h:751
const T & y() const noexcept
Returns the y value.
Definition Vector3.h:816
const T & x() const noexcept
Returns the x value.
Definition Vector3.h:804
const T & z() const noexcept
Returns the z value.
Definition Vector3.h:828
This class implements a worker able to distribute function calls over different threads.
Definition Worker.h:33
bool executeFunction(const Function &function, const unsigned int first, const unsigned int size, const unsigned int firstIndex=(unsigned int)(-1), const unsigned int sizeIndex=(unsigned int)(-1), const unsigned int minimalIterations=1u, const unsigned int threadIndex=(unsigned int)(-1))
Executes a callback function separable by two function parameters.
T minmax(const T &lowerBoundary, const T &value, const T &upperBoundary)
This function fits a given parameter into a specified value range.
Definition base/Utilities.h:973
PixelCenter
Definition of individual centers of pixels.
Definition CV.h:117
PixelPositionT< int > PixelPositionI
Definition of a PixelPosition object with a data type allowing positive and negative coordinate value...
Definition PixelPosition.h:39
@ PC_TOP_LEFT
The center of a pixel is in the upper-left corner of each pixel's square.
Definition CV.h:133
@ PC_CENTER
The center of a pixel is located in the center of each pixel's square (with an offset of 0....
Definition CV.h:150
PinholeCameraT< Scalar > PinholeCamera
Definition of an pinhole camera object with Scalar precision.
Definition PinholeCamera.h:39
VectorT2< float > VectorF2
Definition of a 2D vector with float values.
Definition Vector2.h:42
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition Vector2.h:28
AnyCameraT< Scalar > AnyCamera
Definition of an AnyCamera object with Scalar precision.
Definition AnyCamera.h:31
SquareMatrixT2< Scalar > SquareMatrix2
Definition of the SquareMatrix2 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with ...
Definition SquareMatrix2.h:29
VectorT3< Scalar > Vector3
Definition of a 3D vector.
Definition Vector3.h:29
RotationT< Scalar > Rotation
Definition of the Rotation object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION flag either with ...
Definition Rotation.h:32
SquareMatrixT3< Scalar > SquareMatrix3
Definition of the SquareMatrix3 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with ...
Definition SquareMatrix3.h:43
float Scalar
Definition of a scalar type.
Definition Math.h:129
Namespace of the CV library.
Definition Advanced.h:17
Namespace of the CV Test library.
Definition LineDetectorHough.h:28
Namespace of the Test library.
Definition LineDetectorHough.h:28
The namespace covering the entire Ocean framework.
Definition Accessor.h:15
Default definition of a type with tBytes bytes.
Definition DataType.h:32
float Type
The 32 bit floating point data type for any data type T but 'double'.
Definition DataType.h:373