Skip to content

Commit

Permalink
Add any_image_view::size() (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebionne authored Mar 20, 2020
1 parent 5ced6e5 commit c036059
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/design/dynamic_image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ GIL ``any_image_view`` and ``any_image`` are subclasses of ``variant``:
typedef std::ptrdiff_t x_coord_t;
typedef std::ptrdiff_t y_coord_t;
typedef point<std::ptrdiff_t> point_t;
using size_type = std::size_t;
any_image_view();
template <typename T> explicit any_image_view(const T& obj);
Expand All @@ -131,6 +132,7 @@ GIL ``any_image_view`` and ``any_image`` are subclasses of ``variant``:
// parameters of the currently instantiated view
std::size_t num_channels() const;
point_t dimensions() const;
size_type size() const;
x_coord_t width() const;
y_coord_t height() const;
};
Expand Down
10 changes: 10 additions & 0 deletions include/boost/gil/extension/dynamic_image/any_image_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ struct any_type_get_dimensions
result_type operator()(const T& v) const { return v.dimensions(); }
};

// works for image_view
struct any_type_get_size
{
using result_type = std::size_t;
template <typename T>
result_type operator()(const T& v) const { return v.size(); }
};

} // namespace detail

////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -71,6 +79,7 @@ class any_image_view : public make_variant_over<Views>::type
using x_coord_t = std::ptrdiff_t;
using y_coord_t = std::ptrdiff_t;
using point_t = point<std::ptrdiff_t>;
using size_type = std::size_t;

any_image_view() = default;
any_image_view(any_image_view const& view) : parent_t((parent_t const&)view) {}
Expand Down Expand Up @@ -105,6 +114,7 @@ class any_image_view : public make_variant_over<Views>::type

std::size_t num_channels() const { return apply_operation(*this, detail::any_type_get_num_channels()); }
point_t dimensions() const { return apply_operation(*this, detail::any_type_get_dimensions()); }
size_type size() const { return apply_operation(*this, detail::any_type_get_size()); }
x_coord_t width() const { return dimensions().x; }
y_coord_t height() const { return dimensions().y; }
};
Expand Down
3 changes: 3 additions & 0 deletions test/extension/dynamic_image/subimage_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(subimage_equals_image, Image, fixture::image_types
auto const v0 = gil::const_view(i0);
BOOST_TEST(v0.dimensions().x == 4);
BOOST_TEST(v0.dimensions().y == 4);
BOOST_TEST(v0.size() == 4 * 4);

// request with 2 x point_t values
{
auto v1 = gil::subimage_view(gil::view(i0), {0, 0}, i0.dimensions());
BOOST_TEST(v0.dimensions() == v1.dimensions());
BOOST_TEST(v0.size() == v1.size());
BOOST_TEST(gil::equal_pixels(v0, v1));
}
// request with 4 x dimension values
{
auto v1 = gil::subimage_view(gil::view(i0), 0, 0, i0.dimensions().x, i0.dimensions().y);
BOOST_TEST(v0.dimensions() == v1.dimensions());
BOOST_TEST(v0.size() == v1.size());
BOOST_TEST(gil::equal_pixels(v0, v1));
}
}
Expand Down

0 comments on commit c036059

Please sign in to comment.