blob: 47605e73857369b9f19c1f5019186ebf67bf2f7b [file] [log] [blame]
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01001/*
Sheri Zhang23adc4c2021-01-05 12:48:45 +00002* Copyright (c) 2020-2021 Arm Limited.
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01003 *
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"
28#include "arm_compute/core/QuantizationInfo.h"
29
30#include <algorithm>
31#include <cmath>
32#include <cstddef>
33#include <cstdint>
34
35namespace arm_compute
36{
37namespace scale_helpers
38{
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010039/** 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 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053inline uint8_t delta_bilinear_c1_quantized(const uint8_t *pixel_ptr,
54 size_t stride,
55 float dx,
56 float dy,
57 UniformQuantizationInfo iq_info,
58 UniformQuantizationInfo oq_info)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010059{
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 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092inline int8_t delta_bilinear_c1_quantized(const int8_t *pixel_ptr,
93 size_t stride,
94 float dx,
95 float dy,
96 UniformQuantizationInfo iq_info,
97 UniformQuantizationInfo oq_info)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010098{
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
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100117/** 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 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100133inline 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)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100135{
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;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100169 for (int j = yi + y_from, je = yi + y_to; j <= je; ++j)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100170 {
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}
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000178
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 *
Michele Di Giorgiobd2c8e12021-01-19 15:29:02 +0000182 * @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
Sheri Zhang23adc4c2021-01-05 12:48:45 +0000188 *
189 * @note dx and dy must be in the range [0, 1.0]
190 *
191 * @return The bilinear interpolated pixel value
192 */
193inline 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}
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100204} // namespace scale_helpers
205} // namespace arm_compute
206
207#endif /* SRC_CORE_HELPERS_SCALEHELPERS_H */