| /* |
| * Copyright 2023 Google LLC |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #include "tests/cv/upsample.h" |
| |
| #include <algorithm> |
| #include <cstdint> |
| #include <cstdio> |
| |
| #include "crt/kelvin.h" |
| #include "tests/cv/test_helper.h" |
| |
| void upsample_test() { |
| constexpr int kNumOutputCols = 640; |
| constexpr int kEdge = 32; // 512 / 16 |
| constexpr int kPadding = kEdge * 2; // left/right |
| |
| uint16_t input0_row[kNumOutputCols / 2 + kPadding]; |
| uint16_t input1_row[kNumOutputCols / 2 + kPadding]; |
| uint16_t output0_row[kNumOutputCols]; |
| uint16_t output1_row[kNumOutputCols]; |
| uint16_t *input0_data = input0_row + kEdge; |
| uint16_t *input1_data = input1_row + kEdge; |
| |
| krand(kNumOutputCols / 2 + kPadding, input0_row); |
| krand(kNumOutputCols / 2 + kPadding, input1_row); |
| |
| kelvin::cv::upsample(kNumOutputCols, input0_data, input1_data, output0_row, |
| output1_row); |
| |
| constexpr int kHalfWidth = kNumOutputCols / 2 - 1; |
| for (int i = 0; i < kNumOutputCols; ++i) { |
| int c1 = std::clamp(i / 2, 0, kHalfWidth); |
| int c2 = std::clamp(i & 1 ? c1 + 1 : c1 - 1, 0, kHalfWidth); |
| |
| const uint32_t a = 3 * input0_data[c1] + input0_data[c2]; |
| const uint32_t b = 3 * input1_data[c1] + input1_data[c2]; |
| |
| const uint16_t ref0_value = (a * 3 + b + 8) / 16; |
| const uint16_t ref1_value = (b * 3 + a + 8) / 16; |
| |
| if (ref0_value != output0_row[i]) { |
| printf("**error::upsample_test[%d,%d] %x %x\n", 0, i, ref0_value, |
| output0_row[i]); |
| exit(1); |
| } |
| |
| if (ref1_value != output1_row[i]) { |
| printf("**error::upsample_test[%d,%d] %x %x\n", 1, i, ref1_value, |
| output1_row[i]); |
| exit(1); |
| } |
| } |
| } |
| |
| int main() { |
| upsample_test(); |
| |
| return 0; |
| } |