diff --git a/include/boost/gil/extension/toolbox/metafunctions.hpp b/include/boost/gil/extension/toolbox/metafunctions.hpp index 16d7ee0184..b76491161c 100644 --- a/include/boost/gil/extension/toolbox/metafunctions.hpp +++ b/include/boost/gil/extension/toolbox/metafunctions.hpp @@ -16,5 +16,6 @@ #include #include #include +#include #endif diff --git a/include/boost/gil/extension/toolbox/metafunctions/shrink.hpp b/include/boost/gil/extension/toolbox/metafunctions/shrink.hpp new file mode 100644 index 0000000000..21a18b57e7 --- /dev/null +++ b/include/boost/gil/extension/toolbox/metafunctions/shrink.hpp @@ -0,0 +1,54 @@ +// Copyright Tom Brinkman 2008. Distributed under the Boost +// Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_GIL_EXTENSION_TOOLBOX_METAFUNCTIONS_SHRINK_HPP +#define BOOST_GIL_EXTENSION_TOOLBOX_METAFUNCTIONS_SHRINK_HPP + +#include + +namespace boost { namespace gil { + +/// \brief Assigns a smaller subimage view of the view given as first parameter to the +/// destination/resultant view given as second parameter to the overloaded '()' operator. +/// The dimensions of the smaller view are specified by struct constructor parameters. +template +struct shrink +{ + double left; + double top; + double right; + double bottom; + + shrink(double left_param = 5, double top_param = 5, double right_param = 5, + double bottom_param = 5) + : left(left_param) + , top(top_param) + , right(right_param) + , bottom(bottom_param) + { + } + + void operator()(View& view, View& dstView) + { + double const view_width = static_cast(view.width()); + double const view_height = static_cast(view.height()); + if (left < 1.0) + left *= view_width; + if (right < 1.0) + right *= view_width; + if (top < 1.0) + top *= view_height; + if (bottom < 1.0) + bottom *= view_height; + + double const width = view_width - left - right; + double const height = view_height - top - bottom; + dstView = boost::gil::subimage_view(view, static_cast(left), + static_cast(top), static_cast(width), + static_cast(height)); + } +}; // shrink +}} // namespace boost::gil + +#endif diff --git a/test/extension/toolbox/CMakeLists.txt b/test/extension/toolbox/CMakeLists.txt index 70df68db42..d0d2e86181 100644 --- a/test/extension/toolbox/CMakeLists.txt +++ b/test/extension/toolbox/CMakeLists.txt @@ -24,6 +24,7 @@ foreach(_name is_bit_aligned is_homogeneous pixel_bit_size + shrink subchroma_image) set(_test t_ext_toolbox_${_name}) set(_target test_ext_toolbox_${_name}) diff --git a/test/extension/toolbox/Jamfile b/test/extension/toolbox/Jamfile index a2a78f7bed..3cbc5c57c6 100644 --- a/test/extension/toolbox/Jamfile +++ b/test/extension/toolbox/Jamfile @@ -27,6 +27,7 @@ run color_convert_lab.cpp ; run color_convert_luminance.cpp ; run color_convert_xyz.cpp ; run indexed_image.cpp ; +run shrink.cpp ; # TODO: Add subchroma_image.cpp after fixing run-time failure, # for details see https://github.com/boostorg/gil/pull/164 diff --git a/test/extension/toolbox/shrink.cpp b/test/extension/toolbox/shrink.cpp new file mode 100644 index 0000000000..b081e22f00 --- /dev/null +++ b/test/extension/toolbox/shrink.cpp @@ -0,0 +1,31 @@ +// +// Copyright 2021 Prathamesh Tagore +// +// Use, modification and distribution are subject to the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +// Reference for test case was taken from +// https://github.com/tuttleofx/TuttleOFX/blob/develop/libraries/boostHack/boost/gil/extension/toolbox/shrink.tests.cpp + +#include +#include + +namespace gil = boost::gil; + +int main() +{ + gil::rgb8_image_t original_img(20, 20), expected_img(12, 8); + gil::rgb8_view_t original_img_view = gil::view(original_img), obtained_img_view; + + gil::fill_pixels(original_img_view, gil::rgb8_pixel_t(255, 255, 255)); + gil::fill_pixels(gil::view(expected_img), gil::rgb8_pixel_t(255, 255, 255)); + + gil::shrink shrink(0.20, 0.30, 0.20, 0.30); + shrink(original_img_view, obtained_img_view); + + BOOST_TEST(gil::equal_pixels(obtained_img_view, gil::view(expected_img))); + + return boost::report_errors(); +}