Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/boost/gil/extension/toolbox/metafunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
#include <boost/gil/extension/toolbox/metafunctions/is_homogeneous.hpp>
#include <boost/gil/extension/toolbox/metafunctions/is_similar.hpp>
#include <boost/gil/extension/toolbox/metafunctions/pixel_bit_size.hpp>
#include <boost/gil/extension/toolbox/metafunctions/shrink.hpp>

#endif
54 changes: 54 additions & 0 deletions include/boost/gil/extension/toolbox/metafunctions/shrink.hpp
Original file line number Diff line number Diff line change
@@ -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 <boost/gil.hpp>

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 <typename View>
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<double>(view.width());
double const view_height = static_cast<double>(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<std::ptrdiff_t>(left),
static_cast<std::ptrdiff_t>(top), static_cast<std::ptrdiff_t>(width),
static_cast<std::ptrdiff_t>(height));
}
}; // shrink
}} // namespace boost::gil

#endif
1 change: 1 addition & 0 deletions test/extension/toolbox/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
1 change: 1 addition & 0 deletions test/extension/toolbox/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test/extension/toolbox/shrink.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Copyright 2021 Prathamesh Tagore <prathameshtagore@gmail.com>
//
// 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 <boost/gil/extension/toolbox/metafunctions.hpp>
#include <boost/core/lightweight_test.hpp>

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<gil::rgb8_view_t> 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();
}