| /* |
| * 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 "risp4ml/isp_stages/blc.h" |
| |
| #include "pw_unit_test/framework.h" |
| #include "risp4ml/common/constants.h" |
| #include "risp4ml/common/test_utils.h" |
| |
| static constexpr int kFrameSize = 16; |
| static constexpr int kNumPatterns = 4; |
| |
| class BlcTest : public ::testing::Test { |
| protected: |
| void SetUp() override { |
| in_ = image_new(1, kFrameSize, kFrameSize); |
| InitImageRandom(in_, 0, kRawPipelineMaxVal); |
| // Force max/min values to be included. |
| *image_pixel(in_, 0, 0, 0) = 0; |
| *image_pixel(in_, 0, 0, 1) = kRawPipelineMaxVal; |
| out_ = image_new(1, kFrameSize, kFrameSize); |
| const uint32_t num_bytes = |
| in_->num_channels * in_->height * in_->width * sizeof(pixel_type_t); |
| memcpy(out_->data, in_->data, num_bytes); |
| } |
| void TearDown() override { |
| image_delete(in_); |
| image_delete(out_); |
| } |
| |
| Image* in_; |
| Image* out_; |
| }; |
| |
| TEST_F(BlcTest, Bypass) { |
| BlcParams params = {.enable = false, .offsets = {20, 20, 20, 20}}; |
| set_blc_params(¶ms); |
| |
| blc_process(out_); |
| |
| // Expect no change |
| for (uint16_t c = 0; c < in_->num_channels; ++c) { |
| for (uint16_t y = 0; y < in_->height; ++y) { |
| for (uint16_t x = 0; x < in_->width; ++x) { |
| ASSERT_EQ(image_pixel_val(in_, c, y, x), |
| image_pixel_val(out_, c, y, x)); |
| } |
| } |
| } |
| } |
| |
| TEST_F(BlcTest, NoChangeRandomPixel) { |
| // Set all 4 Bayer channel offsets to 0. |
| BlcParams params = {.enable = true, .offsets = {0, 0, 0, 0}}; |
| set_blc_params(¶ms); |
| |
| blc_process(out_); |
| |
| // Expect no change |
| for (uint16_t c = 0; c < in_->num_channels; ++c) { |
| for (uint16_t y = 0; y < in_->height; ++y) { |
| for (uint16_t x = 0; x < in_->width; ++x) { |
| ASSERT_EQ(image_pixel_val(in_, c, y, x), |
| image_pixel_val(out_, c, y, x)); |
| } |
| } |
| } |
| } |
| |
| TEST_F(BlcTest, MaxOffsetClampToZero) { |
| // Set all 4 Bayer channel offsets to max allowed value. |
| BlcParams params = {.enable = true, |
| .offsets = {kRawPipelineMaxVal, kRawPipelineMaxVal, |
| kRawPipelineMaxVal, kRawPipelineMaxVal}}; |
| set_blc_params(¶ms); |
| |
| blc_process(out_); |
| |
| // Expect correct subtraction |
| for (uint16_t c = 0; c < in_->num_channels; ++c) { |
| for (uint16_t y = 0; y < in_->height; ++y) { |
| for (uint16_t x = 0; x < in_->width; ++x) { |
| ASSERT_EQ(0, image_pixel_val(out_, c, y, x)); |
| } |
| } |
| } |
| } |