Skip to content

Commit

Permalink
Add image constructor from compatible view (#520)
Browse files Browse the repository at this point in the history
* Use pixels_are_compatible instead of std::is_convertible
  • Loading branch information
sdebionne authored Oct 11, 2020
1 parent f7e83e0 commit f374a67
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
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

0 comments on commit f374a67

Please sign in to comment.