Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2021 Arm Limited. |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 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" |
| 28 | #include "arm_compute/core/QuantizationInfo.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 | { |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 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, size_t stride, float dx, float dy, |
| 54 | UniformQuantizationInfo iq_info, UniformQuantizationInfo oq_info) |
| 55 | { |
| 56 | ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr); |
| 57 | |
| 58 | const float dx1 = 1.0f - dx; |
| 59 | const float dy1 = 1.0f - dy; |
| 60 | |
| 61 | const float a00 = dequantize_qasymm8(*pixel_ptr, iq_info); |
| 62 | const float a01 = dequantize_qasymm8(*(pixel_ptr + 1), iq_info); |
| 63 | const float a10 = dequantize_qasymm8(*(pixel_ptr + stride), iq_info); |
| 64 | const float a11 = dequantize_qasymm8(*(pixel_ptr + stride + 1), iq_info); |
| 65 | |
| 66 | const float w1 = dx1 * dy1; |
| 67 | const float w2 = dx * dy1; |
| 68 | const float w3 = dx1 * dy; |
| 69 | const float w4 = dx * dy; |
| 70 | float res = a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4; |
| 71 | return static_cast<uint8_t>(quantize_qasymm8(res, oq_info)); |
| 72 | } |
| 73 | |
| 74 | /** Computes bilinear interpolation for quantized input and output, using the pointer to the top-left pixel and the pixel's distance between |
| 75 | * the real coordinates and the smallest following integer coordinates. Input must be QASYMM8_SIGNED and in single channel format. |
| 76 | * |
| 77 | * @param[in] pixel_ptr Pointer to the top-left pixel value of a single channel input. |
| 78 | * @param[in] stride Stride to access the bottom-left and bottom-right pixel values |
| 79 | * @param[in] dx Pixel's distance between the X real coordinate and the smallest X following integer |
| 80 | * @param[in] dy Pixel's distance between the Y real coordinate and the smallest Y following integer |
| 81 | * @param[in] iq_info Input QuantizationInfo |
| 82 | * @param[in] oq_info Output QuantizationInfo |
| 83 | * |
| 84 | * @note dx and dy must be in the range [0, 1.0] |
| 85 | * |
| 86 | * @return The bilinear interpolated pixel value |
| 87 | */ |
| 88 | inline int8_t delta_bilinear_c1_quantized(const int8_t *pixel_ptr, size_t stride, float dx, float dy, |
| 89 | UniformQuantizationInfo iq_info, UniformQuantizationInfo oq_info) |
| 90 | { |
| 91 | ARM_COMPUTE_ERROR_ON(pixel_ptr == nullptr); |
| 92 | |
| 93 | const float dx1 = 1.0f - dx; |
| 94 | const float dy1 = 1.0f - dy; |
| 95 | |
| 96 | const float a00 = dequantize_qasymm8_signed(*pixel_ptr, iq_info); |
| 97 | const float a01 = dequantize_qasymm8_signed(*(pixel_ptr + 1), iq_info); |
| 98 | const float a10 = dequantize_qasymm8_signed(*(pixel_ptr + stride), iq_info); |
| 99 | const float a11 = dequantize_qasymm8_signed(*(pixel_ptr + stride + 1), iq_info); |
| 100 | |
| 101 | const float w1 = dx1 * dy1; |
| 102 | const float w2 = dx * dy1; |
| 103 | const float w3 = dx1 * dy; |
| 104 | const float w4 = dx * dy; |
| 105 | float res = a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4; |
| 106 | return static_cast<int8_t>(quantize_qasymm8_signed(res, oq_info)); |
| 107 | } |
| 108 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 109 | /** Return the pixel at (x,y) using area interpolation by clamping when out of borders. The image must be single channel U8 |
| 110 | * |
| 111 | * @note The interpolation area depends on the width and height ration of the input and output images |
| 112 | * @note Currently average of the contributing pixels is calculated |
| 113 | * |
| 114 | * @param[in] first_pixel_ptr Pointer to the first pixel of a single channel U8 image. |
| 115 | * @param[in] stride Stride in bytes of the image |
| 116 | * @param[in] width Width of the image |
| 117 | * @param[in] height Height of the image |
| 118 | * @param[in] wr Width ratio among the input image width and output image width. |
| 119 | * @param[in] hr Height ratio among the input image height and output image height. |
| 120 | * @param[in] x X position of the wanted pixel |
| 121 | * @param[in] y Y position of the wanted pixel |
| 122 | * |
| 123 | * @return The pixel at (x, y) using area interpolation. |
| 124 | */ |
| 125 | inline uint8_t |
| 126 | pixel_area_c1u8_clamp(const uint8_t *first_pixel_ptr, size_t stride, size_t width, size_t height, float wr, |
| 127 | float hr, int x, int y) |
| 128 | { |
| 129 | ARM_COMPUTE_ERROR_ON(first_pixel_ptr == nullptr); |
| 130 | |
| 131 | // Calculate sampling position |
| 132 | float in_x = (x + 0.5f) * wr - 0.5f; |
| 133 | float in_y = (y + 0.5f) * hr - 0.5f; |
| 134 | |
| 135 | // Get bounding box offsets |
| 136 | int x_from = std::floor(x * wr - 0.5f - in_x); |
| 137 | int y_from = std::floor(y * hr - 0.5f - in_y); |
| 138 | int x_to = std::ceil((x + 1) * wr - 0.5f - in_x); |
| 139 | int y_to = std::ceil((y + 1) * hr - 0.5f - in_y); |
| 140 | |
| 141 | // Clamp position to borders |
| 142 | in_x = std::max(-1.f, std::min(in_x, static_cast<float>(width))); |
| 143 | in_y = std::max(-1.f, std::min(in_y, static_cast<float>(height))); |
| 144 | |
| 145 | // Clamp bounding box offsets to borders |
| 146 | x_from = ((in_x + x_from) < -1) ? -1 : x_from; |
| 147 | y_from = ((in_y + y_from) < -1) ? -1 : y_from; |
| 148 | x_to = ((in_x + x_to) > width) ? (width - in_x) : x_to; |
| 149 | y_to = ((in_y + y_to) > height) ? (height - in_y) : y_to; |
| 150 | |
| 151 | // Get pixel index |
| 152 | const int xi = std::floor(in_x); |
| 153 | const int yi = std::floor(in_y); |
| 154 | |
| 155 | // Bounding box elements in each dimension |
| 156 | const int x_elements = (x_to - x_from + 1); |
| 157 | const int y_elements = (y_to - y_from + 1); |
| 158 | ARM_COMPUTE_ERROR_ON(x_elements == 0 || y_elements == 0); |
| 159 | |
| 160 | // Sum pixels in area |
| 161 | int sum = 0; |
| 162 | for(int j = yi + y_from, je = yi + y_to; j <= je; ++j) |
| 163 | { |
| 164 | const uint8_t *ptr = first_pixel_ptr + j * stride + xi + x_from; |
| 165 | sum = std::accumulate(ptr, ptr + x_elements, sum); |
| 166 | } |
| 167 | |
| 168 | // Return average |
| 169 | return sum / (x_elements * y_elements); |
| 170 | } |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 171 | |
| 172 | /** Computes bilinear interpolation using the top-left, top-right, bottom-left, bottom-right pixels and the pixel's distance between |
| 173 | * the real coordinates and the smallest following integer coordinates. |
| 174 | * |
Michele Di Giorgio | bd2c8e1 | 2021-01-19 15:29:02 +0000 | [diff] [blame] | 175 | * @param[in] a00 The top-left pixel value. |
| 176 | * @param[in] a01 The top-right pixel value. |
| 177 | * @param[in] a10 The bottom-left pixel value. |
| 178 | * @param[in] a11 The bottom-right pixel value. |
| 179 | * @param[in] dx_val Pixel's distance between the X real coordinate and the smallest X following integer |
| 180 | * @param[in] dy_val Pixel's distance between the Y real coordinate and the smallest Y following integer |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 181 | * |
| 182 | * @note dx and dy must be in the range [0, 1.0] |
| 183 | * |
| 184 | * @return The bilinear interpolated pixel value |
| 185 | */ |
| 186 | inline float delta_bilinear(float a00, float a01, float a10, float a11, float dx_val, float dy_val) |
| 187 | { |
| 188 | const float dx1_val = 1.0f - dx_val; |
| 189 | const float dy1_val = 1.0f - dy_val; |
| 190 | |
| 191 | const float w1 = dx1_val * dy1_val; |
| 192 | const float w2 = dx_val * dy1_val; |
| 193 | const float w3 = dx1_val * dy_val; |
| 194 | const float w4 = dx_val * dy_val; |
| 195 | return a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4; |
| 196 | } |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 197 | } // namespace scale_helpers |
| 198 | } // namespace arm_compute |
| 199 | |
| 200 | #endif /* SRC_CORE_HELPERS_SCALEHELPERS_H */ |