Commit 0f0dfbdc authored by Michaël Roynard's avatar Michaël Roynard
Browse files

Migrate all/any

parent af1b877f
#pragma once
#include <mln/core/algorithm/all_of.hpp>
#include <mln/core/grays.hpp>
#include <mln/core/image/image2d.hpp>
#include <mln/core/image/private/image_operators.hpp>
#include <mln/core/trace.hpp>
......@@ -88,7 +90,10 @@ namespace mln
}
}
mln_postcondition(all(curv >= 0));
using namespace mln::experimental::ops;
mln_postcondition(all_of(curv >= 0));
return curv;
}
} // namespace mln
......@@ -12,30 +12,6 @@
namespace mln
{
/// \defgroup ima_ops Image operators
/// \{
/// \brief Check if all elements of image eval to true.
///
/// \param ima The image to check.
/// \return True iif all elements eval to True.
///
/// \tparam I The type of image. It must be a model of IterableImage.
/// \pre Image's value type must be convertible to bool.
template <typename I>
bool all(const Image<I>& ima);
/// \brief Check if any element of image eval to true
///
/// \param ima The image to check.
/// \return True if at least one element evals to True.
///
/// \tparam I The type of image. It must be a model of IterableImage.
/// \pre Image's value type must be convertible to bool.
template <typename I>
bool any(const Image<I>& ima);
/// \}
/******************************************/
/**** Implementation ****/
......@@ -149,36 +125,6 @@ namespace mln
MLN_GENERATE_CONST_BINARY_EXPR(land, logical_and);
MLN_GENERATE_CONST_BINARY_EXPR(lor, logical_or);
template <typename I>
inline bool all(const Image<I>& ima)
{
BOOST_CONCEPT_ASSERT((IterableImage<I>));
static_assert(std::is_convertible<typename I::value_type, bool>::value,
"Image value type must be convertible to bool");
mln_viter(v, exact(ima));
mln_forall (v)
if (!*v)
return false;
return true;
}
template <typename I>
inline bool any(const Image<I>& ima)
{
BOOST_CONCEPT_ASSERT((IterableImage<I>));
static_assert(std::is_convertible<typename I::value_type, bool>::value,
"Image value type must be convertible to bool");
mln_viter(v, exact(ima));
mln_forall (v)
if (*v)
return true;
return false;
}
template <class I, class Image1, class Image2>
typename boost::lazy_enable_if_c<
is_a<Image1, Image>::value and is_a<Image2, Image>::value,
......
#include <mln/core/grays.hpp>
#include <mln/core/image/image2d.hpp>
#include <mln/core/algorithm/all_of.hpp>
#include <mln/core/algorithm/clone.hpp>
#include <mln/core/algorithm/iota.hpp>
#include <mln/core/extension/fill.hpp>
#include <mln/core/grays.hpp>
#include <mln/core/image/image2d.hpp>
#include <mln/core/image/private/image_operators.hpp>
#include <mln/io/imprint.hpp>
#include <gtest/gtest.h>
......@@ -12,6 +12,7 @@
TEST(Core, Extension_Fill)
{
using namespace mln;
using namespace mln::experimental::ops;
image2d<uint8> ima(10, 10);
iota(ima, 0);
......@@ -20,5 +21,5 @@ TEST(Core, Extension_Fill)
extension::fill(ima, 42);
io::imprint_with_border(ima);
ASSERT_TRUE(all(ima == out));
ASSERT_TRUE(all_of(ima == out));
}
......@@ -55,6 +55,7 @@ TEST(Core, Image2d_LValueOperator)
TEST(Core, Image2d_Operators)
{
using namespace mln;
using namespace mln::experimental::ops;
image2d<int> ima(5, 5);
image2d<int> ref(5, 5);
......@@ -66,12 +67,13 @@ TEST(Core, Image2d_Operators)
mln_forall (v)
*v = i--;
ASSERT_TRUE(mln::all(-ima == ref));
ASSERT_TRUE(mln::all_of(-ima == ref));
}
TEST(Core, Image2d_MixedOperator)
{
using namespace mln;
using namespace mln::experimental::ops;
image2d<char> x(5, 5);
image2d<short> y(5, 5);
......@@ -81,26 +83,29 @@ TEST(Core, Image2d_MixedOperator)
static_assert(std::is_same_v<typename decltype(x + x)::value_type, char>);
ASSERT_TRUE(mln::all((x + y) == (2 * y)));
ASSERT_TRUE(mln::all_of((x + y) == (2 * y)));
}
TEST(Core, Image2d_WhereOperator)
{
using namespace mln;
using namespace mln::experimental::ops;
image2d<uint8_t> x(5, 5);
image2d<uint8_t> y(5, 5);
mln::iota(x, 0);
mln::iota(y, 0);
auto f1 = where(x > 12, x, uint8_t(12)); // RValue image + LValue image + scalar
auto f2 = where(x > 12, x, y); // RValue image + LValue image + LValue image
auto f3 = where(x > 12, uint8_t(12), x); // RValue image + Scalar + LValue image
auto f4 = where(x > 12, uint8_t(0), uint8_t(1)); // RValue image + Scalar + Scalar
auto f1 = experimental::where(x > 12, x, uint8_t(12)); // RValue image + LValue image + scalar
auto f2 = experimental::where(x > 12, x, y); // RValue image + LValue image + LValue image
auto f3 = experimental::where(x > 12, uint8_t(12), x); // RValue image + Scalar + LValue image
auto f4 = experimental::where(x > 12, uint8_t(0), uint8_t(1)); // RValue image + Scalar + Scalar
ASSERT_TRUE(mln::all(f1 >= 12));
static_assert(std::is_same_v<mln_reference(decltype(f1)), const uint8_t&>);
static_assert(std::is_same_v<mln_reference(decltype(f2)), uint8_t&>);
// FIXME: issue https://github.com/ericniebler/range-v3/issues/996 with gcc8.2
// FIXME: migrate rangev3 @HEAD
// ASSERT_TRUE(mln::all_of(f1 >= 12));
static_assert(std::is_same_v<image_reference_t<decltype(f1)>, uint8_t>);
static_assert(std::is_same_v<image_reference_t<decltype(f2)>, uint8_t&>);
}
......@@ -193,6 +198,7 @@ TEST(Core, IfElse)
static_assert(std::is_same_v<image_reference_t<decltype(f4)>, uint8_t>);
// FIXME: issue https://github.com/ericniebler/range-v3/issues/996 with gcc8.2
// FIXME: migrate rangev3 @HEAD
// ASSERT_TRUE(mln::all_of(f1 == ref_f1));
// ASSERT_TRUE(mln::all_of(f2 == ref_f2));
// ASSERT_TRUE(mln::all_of(f3 == ref_f3));
......@@ -205,6 +211,7 @@ TEST(Core, IfElse)
{1, 2, 3}};
// FIXME: issue https://github.com/ericniebler/range-v3/issues/996 with gcc8.2
// FIXME: migrate rangev3 @HEAD
// mln::fill(f2, 42);
// ASSERT_TRUE(mln::all_of(x == ref_x));
......
......@@ -13,6 +13,24 @@
#include <gtest/gtest.h>
// FIXME:
namespace to_migrate
{
template <typename I>
inline bool __all(const mln::Image<I>& ima)
{
static_assert(std::is_convertible<typename I::value_type, bool>::value,
"Image value type must be convertible to bool");
mln_viter(v, exact(ima));
mln_forall (v)
if (!*v)
return false;
return true;
}
} // namespace to_migrate
TEST(Morpho, component_tree_io)
{
......@@ -47,5 +65,6 @@ TEST(Morpho, component_tree_io)
})));
ASSERT_TRUE(data1->m_S == data2->m_S);
ASSERT_TRUE(all(data1->m_pmap == data2->m_pmap));
// FIXME:
ASSERT_TRUE(::to_migrate::__all(data1->m_pmap == data2->m_pmap));
}
#include <mln/core/algorithm/all_of.hpp>
#include <mln/core/algorithm/fill.hpp>
#include <mln/core/algorithm/iota.hpp>
#include <mln/core/colors.hpp>
#include <mln/core/grays.hpp>
#include <mln/core/image/image2d.hpp>
#include <mln/core/image/private/image_operators.hpp>
#include <mln/core/win2d.hpp>
#include <mln/io/imread.hpp>
#include <mln/morpho/canvas/private/dilation_by_periodic_line.hpp>
......@@ -16,6 +18,23 @@
#include <gtest/gtest.h>
// FIXME:
namespace to_migrate
{
template <typename I>
inline bool __all(const mln::Image<I>& ima)
{
static_assert(std::is_convertible<typename I::value_type, bool>::value,
"Image value type must be convertible to bool");
mln_viter(v, exact(ima));
mln_forall (v)
if (!*v)
return false;
return true;
}
} // namespace to_migrate
using namespace mln;
......@@ -171,25 +190,29 @@ TEST(Dilation, Rectangle2d)
TEST(Dilation, Generic_with_wide_enough_extension)
{
using namespace mln::experimental::ops;
image2d<uint8> ima;
io::imread(fixtures::ImagePath::concat_with_filename("small.pgm"), ima);
{ // Fast: border wide enough
mln::se::disc se(3, 0);
auto out = morpho::structural::dilate(ima, se);
ASSERT_TRUE(all(out >= ima)); // extensive
ASSERT_TRUE(all_of(out >= ima)); // extensive
}
}
// Border is not wide enough => use morpher for bound checking
TEST(Dilation, Generic_with_too_small_extension)
{
using namespace mln::experimental::ops;
image2d<uint8> ima;
io::imread(fixtures::ImagePath::concat_with_filename("small.pgm"), ima);
mln::se::disc se(4, 0);
auto out = morpho::structural::dilate(ima, se);
ASSERT_TRUE(all(out >= ima)); // extensive
ASSERT_TRUE(all_of(out >= ima)); // extensive
}
// Dilation on a with a vmorph / binary case
......@@ -200,7 +223,8 @@ TEST(Dilation, Square_on_a_vmorph)
mln::se::rect2d win(3, 3);
auto out = morpho::structural::dilate(ima > 128, win);
ASSERT_TRUE(all(out >= (ima > 128))); // extensive
// FIXME:
ASSERT_TRUE(::to_migrate::__all(out >= (ima > 128))); // extensive
}
TEST(Dilation, Unregular_domain)
......@@ -212,19 +236,21 @@ TEST(Dilation, Unregular_domain)
auto out = clone(ima);
auto tmp = out | where(ima > 128);
morpho::structural::dilate(ima | where(ima > 128), win, tmp, std::less<uint8>());
ASSERT_TRUE(all((out | (ima <= 128)) == (ima | (ima <= 128))));
ASSERT_TRUE(all(out >= ima)); // extensive
ASSERT_TRUE(::to_migrate::__all((out | (ima <= 128)) == (ima | (ima <= 128))));
ASSERT_TRUE(::to_migrate::__all(out >= ima)); // extensive
}
// Custom comparison function, erosion
TEST(Dilation, Custom_cmp_function)
{
using namespace mln::experimental::ops;
image2d<uint8> ima;
io::imread(fixtures::ImagePath::concat_with_filename("small.pgm"), ima);
mln::se::rect2d win(5, 5);
auto out = morpho::structural::dilate(ima, win, std::greater<uint8>());
ASSERT_TRUE(all(out <= ima)); // anti-extensive
ASSERT_TRUE(all_of(out <= ima)); // anti-extensive
}
// Dilation of a binary image
......@@ -258,12 +284,14 @@ TEST(Dilation, Binary)
// Dilation of a bianry image
TEST(Dilation, Binary_2)
{
using namespace mln::experimental::ops;
image2d<bool> ima;
io::imread(fixtures::ImagePath::concat_with_filename("tiny.pbm"), ima);
mln::se::rect2d win(3, 3);
auto out = morpho::structural::dilate(ima, win);
ASSERT_TRUE(all(ima <= out)); // anti-extensive
ASSERT_TRUE(all_of(ima <= out)); // anti-extensive
}
// Dilation of a rgb image
......@@ -274,7 +302,8 @@ TEST(Dilation, RGB)
mln::se::rect2d win(5, 5);
auto out = morpho::structural::dilate(ima, win);
ASSERT_TRUE(all(red(ima) <= red(out))); // anti-extensive
ASSERT_TRUE(all(green(ima) <= green(out))); // anti-extensive
ASSERT_TRUE(all(blue(ima) <= blue(out))); // anti-extensive
// FIXME:
ASSERT_TRUE(::to_migrate::__all(red(ima) <= red(out))); // anti-extensive
ASSERT_TRUE(::to_migrate::__all(green(ima) <= green(out))); // anti-extensive
ASSERT_TRUE(::to_migrate::__all(blue(ima) <= blue(out))); // anti-extensive
}
#include <mln/core/algorithm/all_of.hpp>
#include <mln/core/algorithm/iota.hpp>
#include <mln/core/grays.hpp>
#include <mln/core/image/image2d.hpp>
#include <mln/core/image/private/image_operators.hpp>
#include <mln/core/neighb2d.hpp>
#include <mln/core/se/rect2d.hpp>
#include <mln/io/imread.hpp>
......@@ -16,6 +18,8 @@ using namespace mln;
TEST(Morpho, gradient_gradient_0)
{
using namespace mln::experimental::ops;
image2d<uint8> ima(10, 10);
iota(ima, 10);
......@@ -24,7 +28,8 @@ TEST(Morpho, gradient_gradient_0)
auto out = morpho::structural::gradient(ima, win);
static_assert(std::is_same<decltype(out)::value_type, int>::value, "Error integral promotion should give int.");
ASSERT_TRUE(all(lor(out == 1, out == 2)));
ASSERT_TRUE(all_of(out == 1 || out == 2));
}
}
......@@ -42,6 +47,8 @@ TEST(Morpho, gradient_gradient_1)
// Border is not wide enough => call dilate + erode
TEST(Morpho, gradient_gradient_2)
{
using namespace mln::experimental::ops;
image2d<uint8> ima(0);
image2d<uint8> ima2;
io::imread(fixtures::ImagePath::concat_with_filename("small.pgm"), ima);
......@@ -50,7 +57,7 @@ TEST(Morpho, gradient_gradient_2)
mln::se::rect2d win(3, 3);
auto out1 = morpho::structural::gradient(ima, win);
auto out2 = morpho::structural::gradient(ima2, win);
ASSERT_TRUE(all(out1 == out2));
ASSERT_TRUE(all_of(out1 == out2));
}
// Dilation on a with a vmorph / binary case
......@@ -80,6 +87,8 @@ TEST(Morpho, gradient_gradient_4)
// On colors
TEST(Morpho, gradient_gradient_5)
{
using namespace mln::experimental::ops;
image2d<rgb8> ima;
image2d<rgb8> ima2(0);
io::imread(fixtures::ImagePath::concat_with_filename("small.ppm"), ima);
......@@ -88,5 +97,5 @@ TEST(Morpho, gradient_gradient_5)
mln::se::rect2d win(3, 3);
auto out1 = morpho::structural::gradient(ima, win);
auto out2 = morpho::structural::gradient(ima2, win);
ASSERT_TRUE(all(out1 == out2));
ASSERT_TRUE(all_of(out1 == out2));
}
......@@ -8,6 +8,25 @@
#include <gtest/gtest.h>
// FIXME:
namespace to_migrate
{
template <typename I>
inline bool __all(const mln::Image<I>& ima)
{
static_assert(std::is_convertible<typename I::value_type, bool>::value,
"Image value type must be convertible to bool");
mln_viter(v, exact(ima));
mln_forall (v)
if (!*v)
return false;
return true;
}
} // namespace to_migrate
TEST(Morpho, hit_or_miss)
{
using namespace mln;
......@@ -28,8 +47,10 @@ TEST(Morpho, hit_or_miss)
auto out = morpho::hit_or_miss(ima, win1, win2);
auto out2 = morpho::hit_or_miss(lnot(ima), win2, win1);
ASSERT_TRUE(all(out == out2));
// FIXME:
ASSERT_TRUE(::to_migrate::__all(out == out2));
auto out3 = morpho::hit_or_miss(mln::imcast<uint8>(ima), win1, win2);
ASSERT_TRUE(all(mln::imcast<uint8>(out) == out3));
// FIXME:
ASSERT_TRUE(::to_migrate::__all(mln::imcast<uint8>(out) == out3));
}
#include <mln/core/algorithm/all_of.hpp>
#include <mln/core/algorithm/iota.hpp>
#include <mln/core/grays.hpp>
#include <mln/core/image/image2d.hpp>
#include <mln/core/image/private/image_operators.hpp>
#include <mln/core/neighb2d.hpp>
#include <mln/core/se/rect2d.hpp>
#include <mln/io/imread.hpp>
......@@ -16,6 +18,8 @@ using namespace mln;
TEST(Morpho, opening_closing_opening_0)
{
using namespace mln::experimental::ops;
image2d<uint8> ima;
io::imread(fixtures::ImagePath::concat_with_filename("small.pgm"), ima);
......@@ -24,8 +28,8 @@ TEST(Morpho, opening_closing_opening_0)
auto out1 = morpho::structural::opening(ima, win);
auto out2 = morpho::structural::closing(ima, win);
ASSERT_TRUE(all(out1 <= ima)); // anti-extensive
ASSERT_TRUE(all(out2 >= ima)); // extensive
ASSERT_TRUE(all_of(out1 <= ima)); // anti-extensive
ASSERT_TRUE(all_of(out2 >= ima)); // extensive
}
}
......
......@@ -10,6 +10,24 @@
#include <gtest/gtest.h>
// FIXME:
namespace to_migrate
{
template <typename I>
inline bool __all(const mln::Image<I>& ima)
{
static_assert(std::is_convertible<typename I::value_type, bool>::value,
"Image value type must be convertible to bool");
mln_viter(v, exact(ima));
mln_forall (v)
if (!*v)
return false;
return true;
}
} // namespace to_migrate
TEST(Morpho, saturate)
{
......@@ -19,14 +37,14 @@ TEST(Morpho, saturate)
io::imread(fixtures::ImagePath::concat_with_filename("squares.pgm"), ima);
image2d<bool> out = morpho::saturate(ima == 154, c4, point2d{0, 0});
ASSERT_TRUE(all(out == (lor(lor(ima == 154, ima == 251), ima == 12))));
ASSERT_TRUE(::to_migrate::__all(out == lor(lor(ima == 154, ima == 251), ima == 12)));
morpho::saturate(ima == 130, c4, point2d{0, 0}, out);
ASSERT_TRUE(all(out == (ima >= 12)));
ASSERT_TRUE(::to_migrate::__all(out == (ima >= 12)));
// pinf \in A => sat(A) == domain(f)
morpho::saturate(ima == 195, c4, point2d{69, 45}, out);
ASSERT_TRUE(all(out == true));
ASSERT_TRUE(::to_migrate::__all(out == true));
// io::imprint(out);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment