| 36 | namespace |
| 37 | { |
| 38 | SimpleTensor<float> scale_image(const SimpleTensor<float> &in, |
| 39 | const TensorShape &out_shape, |
| 40 | InterpolationPolicy policy, |
| 41 | float extrapolation_value) |
| 42 | { |
| 43 | ARM_COMPUTE_ERROR_ON(in.data_layout() != DataLayout::NHWC); |
| 44 | |
| 45 | SimpleTensor<float> out{out_shape, DataType::F32, 1, QuantizationInfo(), DataLayout::NHWC}; |
| 46 | // Compute the ratio between source width/height and destination width/height |
| 47 | const auto wr = static_cast<float>(in.shape()[1]) / static_cast<float>(out_shape[1]); |
| 48 | const auto hr = static_cast<float>(in.shape()[2]) / static_cast<float>(out_shape[2]); |
| 49 | |
| 50 | const auto width = static_cast<int>(in.shape().y()); |
| 51 | const auto height = static_cast<int>(in.shape().z()); |
| 52 | |
| 53 | Window win; |
| 54 | win.use_tensor_dimensions(out_shape); |
| 55 | execute_window_loop( |
| 56 | win, |
| 57 | [&](const Coordinates &out_id) |
| 58 | { |
| 59 | Coordinates in_id(out_id); |
| 60 | int idw = in_id.y(); |
| 61 | int idh = in_id.z(); |
| 62 | |
| 63 | switch (policy) |
| 64 | { |
| 65 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 66 | { |
| 67 | //Calculate the source coords without -0.5f is equivalent to round the x_scr/y_src coords |
| 68 | float x_src = std::floor(idw * wr); |
| 69 | float y_src = std::floor(idh * hr); |
| 70 | in_id.set(1, x_src); |
| 71 | in_id.set(2, y_src); |
| 72 | |
| 73 | // If coordinates in range of tensor's width or height |
| 74 | if (is_valid_pixel_index(x_src, y_src, width, height, 0)) |
| 75 | { |
| 76 | *reinterpret_cast<float *>(out(out_id)) = |
| 77 | tensor_elem_at(in, in_id, BorderMode::CONSTANT, extrapolation_value); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | *reinterpret_cast<float *>(out(out_id)) = extrapolation_value; |
| 82 | } |
| 83 | break; |
| 84 | } |
| 85 | case InterpolationPolicy::BILINEAR: |
| 86 | { |
| 87 | float x_src = idw * wr; |
| 88 | float y_src = idh * hr; |
| 89 | in_id.set(1, std::floor(x_src)); |
| 90 | in_id.set(2, std::floor(y_src)); |
| 91 | if (is_valid_pixel_index(x_src, y_src, width, height, 0)) |
| 92 | { |
| 93 | const int id_w = in_id[1]; |
| 94 | const int id_h = in_id[2]; |
| 95 |
no test coverage detected