Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port TuttleOFX extension : aligned #583

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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/aligned.hpp>

#endif
70 changes: 70 additions & 0 deletions include/boost/gil/extension/toolbox/metafunctions/aligned.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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 _aligned_hpp_
#define _aligned_hpp_

#include <boost/gil.hpp>

namespace boost { namespace gil {

/// \brief Aligns the image view passed through struct constructor in a specific direction
/// inside the image view passed through the overloaded '()' operator. The direction of
/// alignment is specified by constructor parameters.
template <typename view_t>
struct aligned
{
// Following enum variables will be used for specifying alignment of view passed through
// constructor with respect to the view passed through overloaded '()' operator.
enum
{
left = (0x1 << 0),
center = (0x1 << 1),
right = (0x1 << 2),
top = (0x1 << 3),
middle = (0x1 << 5),
bottom = (0x1 << 7),
};

view_t& v2;
int align;
aligned(view_t v2, int align = center | middle)
: v2(v2)
, align(align)
{
}

void operator()(view_t& view)
{
using namespace boost::gil;

std::size_t w = v2.width(), h = v2.height();

// For ensuring that view passed through '()' operator has dimensions greater than or
// equal to the dimensions of view passed through constructor.
if (h > view.height() || w > view.width())
{
throw std::length_error("Image view passed through overloaded '()' operator must have"
" dimensions greater than or equal to the dimensions of image view passed through"
" struct constructor");
}

std::ptrdiff_t x = 0;
if (align & center)
x = (view.width() - w) / 2;
else if (align & right)
x = view.width() - w;

std::ptrdiff_t y = 0;
if (align & middle)
y = (view.height() - h) / 2;
else if (align & bottom)
y = view.height() - h;

view_t v3 = subimage_view(view, x, y, w, h);
copy_pixels(v2, v3);
}
}; // shrink
}} // namespace boost::gil
#endif
3 changes: 2 additions & 1 deletion test/extension/toolbox/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ foreach(_name
is_bit_aligned
is_homogeneous
pixel_bit_size
subchroma_image)
subchroma_image
aligned)
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 aligned.cpp ;

# TODO: Add subchroma_image.cpp after fixing run-time failure,
# for details see https://github.com/boostorg/gil/pull/164
Expand Down
Loading