filter.cpp 2.39 KB
Newer Older
Edwin Carlinet's avatar
Edwin Carlinet committed
1
#include <mln/core/image/image2d.hpp>
Michaël Roynard's avatar
Michaël Roynard committed
2
3
#include <mln/core/image/private/image_operators.hpp>
#include <mln/core/image/view/filter.hpp>
Edwin Carlinet's avatar
Edwin Carlinet committed
4
5

#include <mln/core/algorithm/fill.hpp>
Michaël Roynard's avatar
Michaël Roynard committed
6
#include <mln/core/algorithm/iota.hpp>
Edwin Carlinet's avatar
Edwin Carlinet committed
7
8

#include <mln/core/rangev3/foreach.hpp>
Michaël Roynard's avatar
Michaël Roynard committed
9
#include <mln/core/rangev3/view/zip.hpp>
Edwin Carlinet's avatar
Edwin Carlinet committed
10
11
12

#include <helpers.hpp>

Michaël Roynard's avatar
Michaël Roynard committed
13
14
#include <gtest/gtest.h>

Edwin Carlinet's avatar
Edwin Carlinet committed
15
16
17
18

TEST(View, filter_readonly)
{
  using namespace mln;
Michaël Roynard's avatar
Michaël Roynard committed
19
  using namespace mln::experimental::ops;
Edwin Carlinet's avatar
Edwin Carlinet committed
20
21
22
23
24
25
26

  box2d        dom{{-1, -2}, {3, 3}};
  image2d<int> ima(dom);

  iota(ima, 0);
  auto x = view::filter(ima, [](int v) { return v > 10; });

Michaël Roynard's avatar
Michaël Roynard committed
27
  ASSERT_TRUE(mln::experimental::all(x > 10));
Edwin Carlinet's avatar
Edwin Carlinet committed
28
29
30

  mln_foreach_new (auto&& pix, ima.new_pixels())
  {
Michaël Roynard's avatar
Michaël Roynard committed
31
32
33
34
35
36
    (void)pix;
    // FIXME:
    // if (pix.val() > 10)
    //  ASSERT_EQ(pix.val(), x(pix.point()));
    // else
    //  ASSERT_EQ(pix.val(), x.at(pix.point()));
Edwin Carlinet's avatar
Edwin Carlinet committed
37
38
39
40
41
42
43
44
45
46
  }

  mln_foreach_new (auto&& pix, x.new_pixels())
    ASSERT_EQ(pix.val(), ima(pix.point()));
}


TEST(View, filter_writable)
{
  using namespace mln;
Michaël Roynard's avatar
Michaël Roynard committed
47
  using namespace mln::experimental::ops;
Edwin Carlinet's avatar
Edwin Carlinet committed
48
49
50
51
52
53
54
55
56

  box2d        dom{{-1, -2}, {3, 3}};
  image2d<int> ima(dom);


  iota(ima, 0);
  auto x = view::filter(ima, [](int v) { return v > 10; });

  fill(x, 10);
Michaël Roynard's avatar
Michaël Roynard committed
57
  ASSERT_TRUE(mln::experimental::all(ima <= 10));
Edwin Carlinet's avatar
Edwin Carlinet committed
58
59
}

Michaël Roynard's avatar
Michaël Roynard committed
60
/*
Edwin Carlinet's avatar
Edwin Carlinet committed
61
62
63
TEST(View, filter_twice)
{
  using namespace mln;
Michaël Roynard's avatar
Michaël Roynard committed
64
  using namespace mln::experimental::ops;
Edwin Carlinet's avatar
Edwin Carlinet committed
65
66
67
68
69
70
71
72

  box2d        dom{{-1, -2}, {3, 3}};
  image2d<int> ima(dom);

  iota(ima, 0);
  auto x = view::filter(ima, [](int v) { return v > 10; });
  auto u = view::filter(x, [](int v) { return v < 15; });

Michaël Roynard's avatar
Michaël Roynard committed
73
  ASSERT_TRUE(mln::experimental::all(land(u > 10, u < 15)));
Edwin Carlinet's avatar
Edwin Carlinet committed
74

Michaël Roynard's avatar
Michaël Roynard committed
75
  mln_foreach_new (auto&& pix, ima.new_pixels())
Edwin Carlinet's avatar
Edwin Carlinet committed
76
  {
Michaël Roynard's avatar
Michaël Roynard committed
77
78
79
80
81
82
    (void)pix;
    // FIXME:
    // if (pix.val() > 10 and pix.val() < 15)
    //   ASSERT_EQ(pix.val(), u(pix.point()));
    // else
    //   ASSERT_EQ(pix.val(), u.at(pix.point()));
Edwin Carlinet's avatar
Edwin Carlinet committed
83
84
85
    // ASSERT_TRUE(!u.domain().has(pix.point()));
  }

Michaël Roynard's avatar
Michaël Roynard committed
86

Edwin Carlinet's avatar
Edwin Carlinet committed
87
88
89
90
91
92
93
94
95
96
  {
    mln_foreach_new (auto&& pix, u.new_pixels())
      ASSERT_EQ(pix.val(), ima(pix.point()));
  }

  image2d<int> before = clone(ima);
  fill(u, 1);
  {
    mln_foreach_new ((auto [old_v, new_v]), mln::ranges::view::zip(before.new_values(), u.new_values()))
    {
Michaël Roynard's avatar
Michaël Roynard committed
97
98
99
100
101
102
103
      (void)old_v;
      (void)new_v;
      // FIXME:
      // if (old_v > 10 && old_v < 15)
      //   ASSERT_EQ(1, new_v);
      // else
      //   ASSERT_EQ(old_v, new_v);
Edwin Carlinet's avatar
Edwin Carlinet committed
104
105
106
107
108
109
    }
  }

  mln_foreach_new (auto&& px, u.new_pixels())
    ASSERT_EQ(px.val(), ima(px.point()));
}
Michaël Roynard's avatar
Michaël Roynard committed
110
*/