Compute Library
 23.11
ScaleHelpers.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2020-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef SRC_CORE_HELPERS_SCALEHELPERS_H
25 #define SRC_CORE_HELPERS_SCALEHELPERS_H
26 
27 #include "arm_compute/core/Error.h"
29 
30 #include <algorithm>
31 #include <cmath>
32 #include <cstddef>
33 #include <cstdint>
34 
35 namespace arm_compute
36 {
37 namespace scale_helpers
38 {
39 /** Computes bilinear interpolation for quantized input and output, using the pointer to the top-left pixel and the pixel's distance between
40  * the real coordinates and the smallest following integer coordinates. Input must be QASYMM8 and in single channel format.
41  *
42  * @param[in] pixel_ptr Pointer to the top-left pixel value of a single channel input.
43  * @param[in] stride Stride to access the bottom-left and bottom-right pixel values
44  * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer
45  * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer
46  * @param[in] iq_info Input QuantizationInfo
47  * @param[in] oq_info Output QuantizationInfo
48  *
49  * @note dx and dy must be in the range [0, 1.0]
50  *
51  * @return The bilinear interpolated pixel value
52  */
53 inline uint8_t delta_bilinear_c1_quantized(const uint8_t *pixel_ptr,
54  size_t stride,
55  float dx,
56  float dy,
59 {
60  ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr);
61 
62  const float dx1 = 1.0f - dx;
63  const float dy1 = 1.0f - dy;
64 
65  const float a00 = dequantize_qasymm8(*pixel_ptr, iq_info);
66  const float a01 = dequantize_qasymm8(*(pixel_ptr + 1), iq_info);
67  const float a10 = dequantize_qasymm8(*(pixel_ptr + stride), iq_info);
68  const float a11 = dequantize_qasymm8(*(pixel_ptr + stride + 1), iq_info);
69 
70  const float w1 = dx1 * dy1;
71  const float w2 = dx * dy1;
72  const float w3 = dx1 * dy;
73  const float w4 = dx * dy;
74  float res = a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4;
75  return static_cast<uint8_t>(quantize_qasymm8(res, oq_info));
76 }
77 
78 /** Computes bilinear interpolation for quantized input and output, using the pointer to the top-left pixel and the pixel's distance between
79  * the real coordinates and the smallest following integer coordinates. Input must be QASYMM8_SIGNED and in single channel format.
80  *
81  * @param[in] pixel_ptr Pointer to the top-left pixel value of a single channel input.
82  * @param[in] stride Stride to access the bottom-left and bottom-right pixel values
83  * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer
84  * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer
85  * @param[in] iq_info Input QuantizationInfo
86  * @param[in] oq_info Output QuantizationInfo
87  *
88  * @note dx and dy must be in the range [0, 1.0]
89  *
90  * @return The bilinear interpolated pixel value
91  */
92 inline int8_t delta_bilinear_c1_quantized(const int8_t *pixel_ptr,
93  size_t stride,
94  float dx,
95  float dy,
98 {
99  ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr);
100 
101  const float dx1 = 1.0f - dx;
102  const float dy1 = 1.0f - dy;
103 
104  const float a00 = dequantize_qasymm8_signed(*pixel_ptr, iq_info);
105  const float a01 = dequantize_qasymm8_signed(*(pixel_ptr + 1), iq_info);
106  const float a10 = dequantize_qasymm8_signed(*(pixel_ptr + stride), iq_info);
107  const float a11 = dequantize_qasymm8_signed(*(pixel_ptr + stride + 1), iq_info);
108 
109  const float w1 = dx1 * dy1;
110  const float w2 = dx * dy1;
111  const float w3 = dx1 * dy;
112  const float w4 = dx * dy;
113  float res = a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4;
114  return static_cast<int8_t>(quantize_qasymm8_signed(res, oq_info));
115 }
116 
117 /** Return the pixel at (x,y) using area interpolation by clamping when out of borders. The image must be single channel U8
118  *
119  * @note The interpolation area depends on the width and height ration of the input and output images
120  * @note Currently average of the contributing pixels is calculated
121  *
122  * @param[in] first_pixel_ptr Pointer to the first pixel of a single channel U8 image.
123  * @param[in] stride Stride in bytes of the image
124  * @param[in] width Width of the image
125  * @param[in] height Height of the image
126  * @param[in] wr Width ratio among the input image width and output image width.
127  * @param[in] hr Height ratio among the input image height and output image height.
128  * @param[in] x X position of the wanted pixel
129  * @param[in] y Y position of the wanted pixel
130  *
131  * @return The pixel at (x, y) using area interpolation.
132  */
133 inline uint8_t pixel_area_c1u8_clamp(
134  const uint8_t *first_pixel_ptr, size_t stride, size_t width, size_t height, float wr, float hr, int x, int y)
135 {
136  ARM_COMPUTE_ERROR_ON(first_pixel_ptr == nullptr);
137 
138  // Calculate sampling position
139  float in_x = (x + 0.5f) * wr - 0.5f;
140  float in_y = (y + 0.5f) * hr - 0.5f;
141 
142  // Get bounding box offsets
143  int x_from = std::floor(x * wr - 0.5f - in_x);
144  int y_from = std::floor(y * hr - 0.5f - in_y);
145  int x_to = std::ceil((x + 1) * wr - 0.5f - in_x);
146  int y_to = std::ceil((y + 1) * hr - 0.5f - in_y);
147 
148  // Clamp position to borders
149  in_x = std::max(-1.f, std::min(in_x, static_cast<float>(width)));
150  in_y = std::max(-1.f, std::min(in_y, static_cast<float>(height)));
151 
152  // Clamp bounding box offsets to borders
153  x_from = ((in_x + x_from) < -1) ? -1 : x_from;
154  y_from = ((in_y + y_from) < -1) ? -1 : y_from;
155  x_to = ((in_x + x_to) > width) ? (width - in_x) : x_to;
156  y_to = ((in_y + y_to) > height) ? (height - in_y) : y_to;
157 
158  // Get pixel index
159  const int xi = std::floor(in_x);
160  const int yi = std::floor(in_y);
161 
162  // Bounding box elements in each dimension
163  const int x_elements = (x_to - x_from + 1);
164  const int y_elements = (y_to - y_from + 1);
165  ARM_COMPUTE_ERROR_ON(x_elements == 0 || y_elements == 0);
166 
167  // Sum pixels in area
168  int sum = 0;
169  for (int j = yi + y_from, je = yi + y_to; j <= je; ++j)
170  {
171  const uint8_t *ptr = first_pixel_ptr + j * stride + xi + x_from;
172  sum = std::accumulate(ptr, ptr + x_elements, sum);
173  }
174 
175  // Return average
176  return sum / (x_elements * y_elements);
177 }
178 
179 /** Computes bilinear interpolation using the top-left, top-right, bottom-left, bottom-right pixels and the pixel's distance between
180  * the real coordinates and the smallest following integer coordinates.
181  *
182  * @param[in] a00 The top-left pixel value.
183  * @param[in] a01 The top-right pixel value.
184  * @param[in] a10 The bottom-left pixel value.
185  * @param[in] a11 The bottom-right pixel value.
186  * @param[in] dx_val Pixel's distance between the X real coordinate and the smallest X following integer
187  * @param[in] dy_val Pixel's distance between the Y real coordinate and the smallest Y following integer
188  *
189  * @note dx and dy must be in the range [0, 1.0]
190  *
191  * @return The bilinear interpolated pixel value
192  */
193 inline float delta_bilinear(float a00, float a01, float a10, float a11, float dx_val, float dy_val)
194 {
195  const float dx1_val = 1.0f - dx_val;
196  const float dy1_val = 1.0f - dy_val;
197 
198  const float w1 = dx1_val * dy1_val;
199  const float w2 = dx_val * dy1_val;
200  const float w3 = dx1_val * dy_val;
201  const float w4 = dx_val * dy_val;
202  return a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4;
203 }
204 } // namespace scale_helpers
205 } // namespace arm_compute
206 
207 #endif /* SRC_CORE_HELPERS_SCALEHELPERS_H */
arm_compute::scale_helpers::delta_bilinear
float delta_bilinear(float a00, float a01, float a10, float a11, float dx_val, float dy_val)
Computes bilinear interpolation using the top-left, top-right, bottom-left, bottom-right pixels and t...
Definition: ScaleHelpers.h:193
arm_compute::dequantize_qasymm8
float dequantize_qasymm8(uint8_t value, const INFO_TYPE &qinfo)
Dequantize a value given an unsigned 8-bit asymmetric quantization scheme.
Definition: QuantizationInfo.h:353
arm_compute::quantize_qasymm8
uint8_t quantize_qasymm8(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given an unsigned 8-bit asymmetric quantization scheme.
Definition: QuantizationInfo.h:295
arm_compute::UniformQuantizationInfo
Quantization info when assuming per layer quantization.
Definition: QuantizationInfo.h:42
Error.h
arm_compute::scale_helpers::delta_bilinear_c1_quantized
uint8_t delta_bilinear_c1_quantized(const uint8_t *pixel_ptr, size_t stride, float dx, float dy, UniformQuantizationInfo iq_info, UniformQuantizationInfo oq_info)
Computes bilinear interpolation for quantized input and output, using the pointer to the top-left pix...
Definition: ScaleHelpers.h:53
ARM_COMPUTE_ERROR_ON
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
arm_compute::quantize_qasymm8_signed
int8_t quantize_qasymm8_signed(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a signed 8-bit asymmetric quantization scheme.
Definition: QuantizationInfo.h:309
arm_compute::test::validation::reference::accumulate
SimpleTensor< T2 > accumulate(const SimpleTensor< T1 > &src, DataType output_data_type)
Definition: Accumulate.cpp:38
arm_compute::scale_helpers::pixel_area_c1u8_clamp
uint8_t pixel_area_c1u8_clamp(const uint8_t *first_pixel_ptr, size_t stride, size_t width, size_t height, float wr, float hr, int x, int y)
Return the pixel at (x,y) using area interpolation by clamping when out of borders.
Definition: ScaleHelpers.h:133
arm_compute::dequantize_qasymm8_signed
float dequantize_qasymm8_signed(int8_t value, const INFO_TYPE &qinfo)
Dequantize a value given a signed 8-bit asymmetric quantization scheme.
Definition: QuantizationInfo.h:366
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
QuantizationInfo.h