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

Add image constructor from compatible view #520

Merged
merged 3 commits into from
Oct 11, 2020
Merged
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ doc/warnings.txt
/.vs
.vscode
*.code-workspace
out

# Clang/LLVM
/.clang-format
Expand Down
10 changes: 10 additions & 0 deletions include/boost/gil/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ class image
allocate_and_copy(img.dimensions(),img._view);
}

template <typename Loc,
typename std::enable_if<pixels_are_compatible<typename Loc::value_type, Pixel>::value, int>::type = 0>
image(const image_view<Loc>& view,
std::size_t alignment = 0,
const Alloc alloc_in = Alloc()) : _memory(nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
, _allocated_bytes( 0 )
{
allocate_and_copy(view.dimensions(),view);
}

// TODO Optimization: use noexcept (requires _view to be nothrow copy constructible)
image(image&& img) :
_view(img._view),
Expand Down
25 changes: 25 additions & 0 deletions test/core/image/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ struct test_constructor_from_other_image
}
};

struct test_constructor_from_view
{
template <typename Image>
void operator()(Image const&)
{
using image_t = Image;
gil::point_t const dimensions{ 256, 128 };
using pixel_t = typename image_t::view_t::value_type;
pixel_t const rnd_pixel = fixture::pixel_generator<pixel_t>::random();
{
//constructor interleaved from planar
gil::image<pixel_t, true> image1(dimensions, rnd_pixel);
auto v1 = gil::transposed_view(gil::const_view(image1));
image_t image2(gil::transposed_view(v1));
BOOST_TEST_EQ(image2.dimensions(), dimensions);
auto v2 = gil::const_view(image2);
BOOST_TEST_ALL_EQ(v1.begin(), v1.end(), v2.begin(), v2.end());
}
}
static void run()
{
boost::mp11::mp_for_each<fixture::rgb_interleaved_image_types>(test_constructor_from_view{});
}
};

struct test_move_constructor
{
template <typename Image>
Expand Down