Skip to content

Commit

Permalink
Refactor expected image generation part
Browse files Browse the repository at this point in the history
  • Loading branch information
meshtag committed May 31, 2021
1 parent 30d5068 commit eea7b6b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 144 deletions.
104 changes: 32 additions & 72 deletions test/extension/numeric/convolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<channel_t>(
{0, 0, 0, 0, 0, 0, 1, 0, 0});
Expand Down Expand Up @@ -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<channel_t>({0, 0, 0, 0, 0, 0, 1, 0, 0});
gil::detail::convolve_1d<pixel_t>(gil::const_view(img_out), kernel_shift_by_two,
Expand Down Expand Up @@ -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<channel_t>({0, 0, 1});
gil::detail::convolve_1d<pixel_t>(gil::const_view(img_out), kernel_shift_by_one,
Expand Down Expand Up @@ -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<channel_t>({0, 0, 1});
gil::detail::convolve_1d<pixel_t>(gil::const_view(img_out), kernel_shift_by_one,
Expand Down Expand Up @@ -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<channel_t>({0, 0, 1});
gil::detail::convolve_1d<pixel_t>(gil::const_view(img_out), kernel_shift_by_one,
Expand Down
49 changes: 13 additions & 36 deletions test/extension/numeric/convolve_cols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<channel_t>(
{0, 0, 0, 0, 0, 0, 1, 0, 0});
Expand Down Expand Up @@ -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<channel_t>(
{0, 0, 0, 0, 0, 0, 1, 0, 0});
gil::convolve_cols<pixel_t>(gil::const_view(img_out), kernel_shift_by_two,
Expand Down Expand Up @@ -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<channel_t>({0, 0, 1});
gil::convolve_cols<pixel_t>(gil::const_view(img_out), kernel_shift_by_one,
Expand Down Expand Up @@ -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<channel_t>({0, 0, 1});
gil::convolve_cols<pixel_t>(gil::const_view(img_out), kernel_shift_by_one,
Expand Down Expand Up @@ -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<channel_t>({0, 0, 1});
gil::convolve_cols<pixel_t>(gil::const_view(img_out), kernel_shift_by_one,
Expand Down
49 changes: 13 additions & 36 deletions test/extension/numeric/convolve_rows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<channel_t>(
{0, 0, 0, 0, 0, 0, 1, 0, 0});
Expand Down Expand Up @@ -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<channel_t>(
{0, 0, 0, 0, 0, 0, 1, 0, 0});
gil::convolve_rows<pixel_t>(gil::const_view(img_out), kernel_shift_by_two,
Expand Down Expand Up @@ -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<channel_t>({0, 0, 1});
gil::convolve_rows<pixel_t>(gil::const_view(img_out), kernel_shift_by_one,
Expand Down Expand Up @@ -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<channel_t>({0, 0, 1});
gil::convolve_rows<pixel_t>(gil::const_view(img_out), kernel_shift_by_one,
Expand Down Expand Up @@ -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<channel_t>({0, 0, 1});
gil::convolve_rows<pixel_t>(gil::const_view(img_out), kernel_shift_by_one,
Expand Down
50 changes: 50 additions & 0 deletions test/extension/numeric/test_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,54 @@ auto create_kernel(std::initializer_list<T> const& values)
return kernel;
}

template <typename SrcView, typename DstView>
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<SrcView>::type,
typename color_space_type<DstView>::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 <typename SrcView, typename DstView>
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<SrcView>::type,
typename color_space_type<DstView>::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

0 comments on commit eea7b6b

Please sign in to comment.