From eea7b6b0f7efe9a3aff37a9c589e119d77136006 Mon Sep 17 00:00:00 2001 From: meshtag Date: Mon, 31 May 2021 20:05:10 +0530 Subject: [PATCH] Refactor expected image generation part --- test/extension/numeric/convolve.cpp | 104 +++++++---------------- test/extension/numeric/convolve_cols.cpp | 49 +++-------- test/extension/numeric/convolve_rows.cpp | 49 +++-------- test/extension/numeric/test_fixture.hpp | 50 +++++++++++ 4 files changed, 108 insertions(+), 144 deletions(-) diff --git a/test/extension/numeric/convolve.cpp b/test/extension/numeric/convolve.cpp index bd6a006aa6..c97571c228 100644 --- a/test/extension/numeric/convolve.cpp +++ b/test/extension/numeric/convolve.cpp @@ -131,20 +131,10 @@ struct test_image_5x5_kernel_1x9_boundary_extend_zero image_t img_expected_col(img_view.width(), img_view.height()); unsigned int const kernel_shift_offset = 2; - for (std::ptrdiff_t y = 0; y < img_view.height(); ++y) - { - auto img_it = img_view.row_begin(y); - auto img_expected_row_it = gil::view(img_expected_row).row_begin(y); - for (std::ptrdiff_t x = kernel_shift_offset; x < img_view.width(); ++x) - img_expected_row_it[x] = img_it[x - 2]; - } - for (std::ptrdiff_t x = 0; x < img_view.width(); ++x) - { - auto img_expected_row_it = gil::view(img_expected_row).col_begin(x); - auto img_expected_col_it = gil::view(img_expected_col).col_begin(x); - for (std::ptrdiff_t y = kernel_shift_offset; y < img_view.height(); ++y) - img_expected_col_it[y] = img_expected_row_it[y - 2]; - } + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected_row), + kernel_shift_offset); + fixture::col_conv1D_offset_img_generator(gil::view(img_expected_row), + gil::view(img_expected_col), kernel_shift_offset); auto const kernel_shift_by_two = fixture::create_kernel( {0, 0, 0, 0, 0, 0, 1, 0, 0}); @@ -173,23 +163,17 @@ struct test_image_5x5_kernel_1x9_boundary_extend_constant image_t img_out(img), img_expected_row(img); unsigned int const kernel_shift_offset = 2; - for (std::ptrdiff_t y = 0; y < img_view.height(); ++y) - { - auto img_it = img_view.row_begin(y); - auto img_expected_row_it = gil::view(img_expected_row).row_begin(y); - for (std::ptrdiff_t x = kernel_shift_offset; x < img_view.width(); ++x) - img_expected_row_it[x] = img_it[x - 2]; - img_expected_row_it[1] = img_it[0]; - } + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected_row), + kernel_shift_offset); + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected_row), + 1, 0, 0, img_view.height(), 2); + image_t img_expected_col(img_expected_row); - for (std::ptrdiff_t x = 0; x < img_view.width(); ++x) - { - auto img_expected_row_it = gil::view(img_expected_row).col_begin(x); - auto img_expected_col_it = gil::view(img_expected_col).col_begin(x); - for (std::ptrdiff_t y = kernel_shift_offset; y < img_view.height(); ++y) - img_expected_col_it[y] = img_expected_row_it[y - 2]; - img_expected_col_it[1] = img_expected_row_it[0]; - } + + fixture::col_conv1D_offset_img_generator(gil::view(img_expected_row), + gil::view(img_expected_col), kernel_shift_offset); + fixture::col_conv1D_offset_img_generator(gil::view(img_expected_row), + gil::view(img_expected_col), 1, 0, 0, 2, img_view.width()); auto const kernel_shift_by_two = fixture::create_kernel({0, 0, 0, 0, 0, 0, 1, 0, 0}); gil::detail::convolve_1d(gil::const_view(img_out), kernel_shift_by_two, @@ -218,20 +202,11 @@ struct test_image_5x5_kernel_1x3_boundary_output_zero image_t img_expected_col(img_view.width(), img_view.height()); unsigned int const kernel_shift_offset = 1; - for (std::ptrdiff_t y = 0; y < img_view.height(); ++y) - { - auto img_it = img_view.row_begin(y); - auto img_expected_row_it = gil::view(img_expected_row).row_begin(y); - for (std::ptrdiff_t x = kernel_shift_offset; x < img_view.width() - 1; ++x) - img_expected_row_it[x] = img_it[x - 1]; - } - for (std::ptrdiff_t x = 0; x < img_view.width(); ++x) - { - auto img_expected_row_it = gil::view(img_expected_row).col_begin(x); - auto img_expected_col_it = gil::view(img_expected_col).col_begin(x); - for (std::ptrdiff_t y = kernel_shift_offset; y < img_view.height() - 1; ++y) - img_expected_col_it[y] = img_expected_row_it[y - 1]; - } + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected_row), + kernel_shift_offset, 0, 0, img_view.height(), img_view.width() - 1); + fixture::col_conv1D_offset_img_generator(gil::view(img_expected_row), + gil::view(img_expected_col), kernel_shift_offset, 0, 0, img_view.height() - 1, + img_view.width()); auto const kernel_shift_by_one = fixture::create_kernel({0, 0, 1}); gil::detail::convolve_1d(gil::const_view(img_out), kernel_shift_by_one, @@ -259,21 +234,14 @@ struct test_image_5x5_kernel_1x3_boundary_output_ignore image_t img_out(img), img_expected_row(img); unsigned int const kernel_shift_offset = 1; - for (std::ptrdiff_t y = 0; y < img_view.height(); ++y) - { - auto img_it = img_view.row_begin(y); - auto img_expected_row_it = gil::view(img_expected_row).row_begin(y); - for (std::ptrdiff_t x = kernel_shift_offset; x < img_view.width() - 1; ++x) - img_expected_row_it[x] = img_it[x - 1]; - } + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected_row), + kernel_shift_offset, 0, 0, img_view.height(), img_view.width() - 1); + image_t img_expected_col(img_expected_row); - for (std::ptrdiff_t x = 0; x < img_view.width(); ++x) - { - auto img_expected_row_it = gil::view(img_expected_row).col_begin(x); - auto img_expected_col_it = gil::view(img_expected_col).col_begin(x); - for (std::ptrdiff_t y = kernel_shift_offset; y < img_view.height() - 1; ++y) - img_expected_col_it[y] = img_expected_row_it[y - 1]; - } + + fixture::col_conv1D_offset_img_generator(gil::view(img_expected_row), + gil::view(img_expected_col), kernel_shift_offset, 0, 0, img_view.height() - 1, + img_view.width()); auto const kernel_shift_by_one = fixture::create_kernel({0, 0, 1}); gil::detail::convolve_1d(gil::const_view(img_out), kernel_shift_by_one, @@ -301,21 +269,13 @@ struct test_image_5x5_kernel_1x3_boundary_extend_padded image_t img_out(img), img_expected_row(img); unsigned int const kernel_shift_offset = 1; - for (std::ptrdiff_t y = 0; y < img_view.height(); ++y) - { - auto img_it = img_view.row_begin(y); - auto img_expected_row_it = gil::view(img_expected_row).row_begin(y); - for (std::ptrdiff_t x = kernel_shift_offset; x < img_view.width(); ++x) - img_expected_row_it[x] = img_it[x - 1]; - } + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected_row), + kernel_shift_offset); + image_t img_expected_col(img_expected_row); - for (std::ptrdiff_t x = 0; x < img_view.width(); ++x) - { - auto img_expected_row_it = gil::view(img_expected_row).col_begin(x); - auto img_expected_col_it = gil::view(img_expected_col).col_begin(x); - for (std::ptrdiff_t y = kernel_shift_offset; y < img_view.width(); ++y) - img_expected_col_it[y] = img_expected_row_it[y - 1]; - } + + fixture::col_conv1D_offset_img_generator(gil::view(img_expected_row), + gil::view(img_expected_col), kernel_shift_offset); auto const kernel_shift_by_one = fixture::create_kernel({0, 0, 1}); gil::detail::convolve_1d(gil::const_view(img_out), kernel_shift_by_one, diff --git a/test/extension/numeric/convolve_cols.cpp b/test/extension/numeric/convolve_cols.cpp index a2f40df22d..aa8f28a36b 100644 --- a/test/extension/numeric/convolve_cols.cpp +++ b/test/extension/numeric/convolve_cols.cpp @@ -78,13 +78,8 @@ struct test_image_5x5_kernel_1x9_boundary_extend_zero image_t img_out(img), img_expected(img_view.width(), img_view.height()); unsigned int const kernel_shift_offset = 2; - for (std::ptrdiff_t x = 0; x < img_view.width(); ++x) - { - auto img_it = img_view.col_begin(x); - auto img_expected_it = gil::view(img_expected).col_begin(x); - for (std::ptrdiff_t y = kernel_shift_offset; y < img_view.height(); ++y) - img_expected_it[y] = img_it[y - 2]; - } + fixture::col_conv1D_offset_img_generator(img_view, gil::view(img_expected), + kernel_shift_offset); auto const kernel_shift_by_two = fixture::create_kernel( {0, 0, 0, 0, 0, 0, 1, 0, 0}); @@ -112,14 +107,11 @@ struct test_image_5x5_kernel_1x9_boundary_extend_constant image_t img_out(img), img_expected(img); unsigned int const kernel_shift_offset = 2; - for (std::ptrdiff_t x = 0; x < img_view.width(); ++x) - { - auto img_it = img_view.col_begin(x); - auto img_expected_it = gil::view(img_expected).col_begin(x); - for (std::ptrdiff_t y = kernel_shift_offset; y < img_view.height(); ++y) - img_expected_it[y] = img_it[y - 2]; - img_expected_it[1] = img_it[0]; - } + fixture::col_conv1D_offset_img_generator(img_view, gil::view(img_expected), + kernel_shift_offset); + fixture::col_conv1D_offset_img_generator(img_view, gil::view(img_expected), + 1, 0, 0, 2, img_view.width()); + auto const kernel_shift_by_two = fixture::create_kernel( {0, 0, 0, 0, 0, 0, 1, 0, 0}); gil::convolve_cols(gil::const_view(img_out), kernel_shift_by_two, @@ -147,13 +139,8 @@ struct test_image_5x5_kernel_1x3_boundary_output_zero image_t img_out(img), img_expected(img_view.width(), img_view.height()); unsigned int const kernel_shift_offset = 1; - for (std::ptrdiff_t x = 0; x < img_view.width(); ++x) - { - auto img_it = img_view.col_begin(x); - auto img_expected_it = gil::view(img_expected).col_begin(x); - for (std::ptrdiff_t y = kernel_shift_offset; y < img_view.height() - 1; ++y) - img_expected_it[y] = img_it[y - 1]; - } + fixture::col_conv1D_offset_img_generator(img_view, gil::view(img_expected), + kernel_shift_offset, 0, 0, img_view.height() - 1, img_view.width()); auto const kernel_shift_by_one = fixture::create_kernel({0, 0, 1}); gil::convolve_cols(gil::const_view(img_out), kernel_shift_by_one, @@ -181,13 +168,8 @@ struct test_image_5x5_kernel_1x3_boundary_output_ignore image_t img_out(img), img_expected(img); unsigned int const kernel_shift_offset = 1; - for (std::ptrdiff_t x = 0; x < img_view.width(); ++x) - { - auto img_it = img_view.col_begin(x); - auto img_expected_it = gil::view(img_expected).col_begin(x); - for (std::ptrdiff_t y = kernel_shift_offset; y < img_view.height() - 1; ++y) - img_expected_it[y] = img_it[y - 1]; - } + fixture::col_conv1D_offset_img_generator(img_view, gil::view(img_expected), + kernel_shift_offset, 0, 0, img_view.height() - 1, img_view.width()); auto const kernel_shift_by_one = fixture::create_kernel({0, 0, 1}); gil::convolve_cols(gil::const_view(img_out), kernel_shift_by_one, @@ -215,13 +197,8 @@ struct test_image_5x5_kernel_1x3_boundary_extend_padded image_t img_out(img), img_expected(img); unsigned int const kernel_shift_offset = 1; - for (std::ptrdiff_t x = 0; x < img_view.width(); ++x) - { - auto img_it = img_view.col_begin(x); - auto img_expected_it = gil::view(img_expected).col_begin(x); - for (std::ptrdiff_t y = kernel_shift_offset; y < img_view.width(); ++y) - img_expected_it[y] = img_it[y - 1]; - } + fixture::col_conv1D_offset_img_generator(img_view, gil::view(img_expected), + kernel_shift_offset); auto const kernel_shift_by_one = fixture::create_kernel({0, 0, 1}); gil::convolve_cols(gil::const_view(img_out), kernel_shift_by_one, diff --git a/test/extension/numeric/convolve_rows.cpp b/test/extension/numeric/convolve_rows.cpp index 0b8bf7adef..e25f9b7cb3 100644 --- a/test/extension/numeric/convolve_rows.cpp +++ b/test/extension/numeric/convolve_rows.cpp @@ -78,13 +78,8 @@ struct test_image_5x5_kernel_1x9_boundary_extend_zero image_t img_out(img), img_expected(img_view.width(), img_view.height()); unsigned int const kernel_shift_offset = 2; - for (std::ptrdiff_t y = 0; y < img_view.height(); ++y) - { - auto img_it = img_view.row_begin(y); - auto img_expected_it = gil::view(img_expected).row_begin(y); - for (std::ptrdiff_t x = kernel_shift_offset; x < img_view.width(); ++x) - img_expected_it[x] = img_it[x - 2]; - } + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected), + kernel_shift_offset); auto const kernel_shift_by_two = fixture::create_kernel( {0, 0, 0, 0, 0, 0, 1, 0, 0}); @@ -112,14 +107,11 @@ struct test_image_5x5_kernel_1x9_boundary_extend_constant image_t img_out(img), img_expected(img); unsigned int const kernel_shift_offset = 2; - for (std::ptrdiff_t y = 0; y < img_view.height(); ++y) - { - auto img_it = img_view.row_begin(y); - auto img_expected_it = gil::view(img_expected).row_begin(y); - for (std::ptrdiff_t x = kernel_shift_offset; x < img_view.width(); ++x) - img_expected_it[x] = img_it[x - 2]; - img_expected_it[1] = img_it[0]; - } + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected), + kernel_shift_offset); + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected), + 1, 0, 0, img_view.height(), 2); + auto const kernel_shift_by_two = fixture::create_kernel( {0, 0, 0, 0, 0, 0, 1, 0, 0}); gil::convolve_rows(gil::const_view(img_out), kernel_shift_by_two, @@ -147,13 +139,8 @@ struct test_image_5x5_kernel_1x3_boundary_output_zero image_t img_out(img), img_expected(img_view.width(), img_view.height()); unsigned int const kernel_shift_offset = 1; - for (std::ptrdiff_t y = 0; y < img_view.height(); ++y) - { - auto img_it = img_view.row_begin(y); - auto img_expected_it = gil::view(img_expected).row_begin(y); - for (std::ptrdiff_t x = kernel_shift_offset; x < img_view.width() - 1; ++x) - img_expected_it[x] = img_it[x - 1]; - } + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected), + kernel_shift_offset, 0, 0, img_view.height(), img_view.width() - 1); auto const kernel_shift_by_one = fixture::create_kernel({0, 0, 1}); gil::convolve_rows(gil::const_view(img_out), kernel_shift_by_one, @@ -181,13 +168,8 @@ struct test_image_5x5_kernel_1x3_boundary_output_ignore image_t img_out(img), img_expected(img); unsigned int const kernel_shift_offset = 1; - for (std::ptrdiff_t y = 0; y < img_view.height(); ++y) - { - auto img_it = img_view.row_begin(y); - auto img_expected_it = gil::view(img_expected).row_begin(y); - for (std::ptrdiff_t x = kernel_shift_offset; x < img_view.width() - 1; ++x) - img_expected_it[x] = img_it[x - 1]; - } + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected), + kernel_shift_offset, 0, 0, img_view.height(), img_view.width() - 1); auto const kernel_shift_by_one = fixture::create_kernel({0, 0, 1}); gil::convolve_rows(gil::const_view(img_out), kernel_shift_by_one, @@ -215,13 +197,8 @@ struct test_image_5x5_kernel_1x3_boundary_extend_padded image_t img_out(img), img_expected(img); unsigned int const kernel_shift_offset = 1; - for (std::ptrdiff_t y = 0; y < img_view.height(); ++y) - { - auto img_it = img_view.row_begin(y); - auto img_expected_it = gil::view(img_expected).row_begin(y); - for (std::ptrdiff_t x = kernel_shift_offset; x < img_view.width(); ++x) - img_expected_it[x] = img_it[x - 1]; - } + fixture::row_conv1D_offset_img_generator(img_view, gil::view(img_expected), + kernel_shift_offset); auto const kernel_shift_by_one = fixture::create_kernel({0, 0, 1}); gil::convolve_rows(gil::const_view(img_out), kernel_shift_by_one, diff --git a/test/extension/numeric/test_fixture.hpp b/test/extension/numeric/test_fixture.hpp index bfd5cf6f2c..61c8fe821f 100644 --- a/test/extension/numeric/test_fixture.hpp +++ b/test/extension/numeric/test_fixture.hpp @@ -27,4 +27,54 @@ auto create_kernel(std::initializer_list const& values) return kernel; } +template +void row_conv1D_offset_img_generator(SrcView src_view, DstView dst_view, unsigned int const offset, + std::ptrdiff_t start_row = 0, std::ptrdiff_t start_col = 0, std::ptrdiff_t end_row = -1, + std::ptrdiff_t end_col = -1) +{ + BOOST_ASSERT(src_view.dimensions() == dst_view.dimensions()); + static_assert(color_spaces_are_compatible + < + typename color_space_type::type, + typename color_space_type::type + >::value, "Source and destination views must have pixels with the same color space"); + + if (end_row == -1) + end_row = src_view.height(); + if (end_col == -1) + end_col = src_view.width(); + for (std::ptrdiff_t y = start_row; y < end_row; ++y) + { + auto src_it = src_view.row_begin(y); + auto dst_it = dst_view.row_begin(y); + for (std::ptrdiff_t x = offset + start_col; x < end_col; ++x) + dst_it[x] = src_it[x - offset]; + } +} + +template +void col_conv1D_offset_img_generator(SrcView src_view, DstView dst_view, unsigned int const offset, + std::ptrdiff_t start_row = 0, std::ptrdiff_t start_col = 0, std::ptrdiff_t end_row = -1, + std::ptrdiff_t end_col = -1) +{ + BOOST_ASSERT(src_view.dimensions() == dst_view.dimensions()); + static_assert(color_spaces_are_compatible + < + typename color_space_type::type, + typename color_space_type::type + >::value, "Source and destination views must have pixels with the same color space"); + + if (end_row == -1) + end_row = src_view.height(); + if (end_col == -1) + end_col = src_view.width(); + for (std::ptrdiff_t x = start_col; x < end_col; ++x) + { + auto src_it = src_view.col_begin(x); + auto dst_it = dst_view.col_begin(x); + for (std::ptrdiff_t y = offset + start_row; y < end_row; ++y) + dst_it[y] = src_it[y - offset]; + } +} + }}}} // namespace boost::gil::test::fixture