cfitsio.cpp 4.48 KB
Newer Older
Baptiste Esteban's avatar
Baptiste Esteban committed
1
#include <mln/core/image/ndimage.hpp>
2
3
4
5
6
7
8
#include <mln/io/imread.hpp>

#include <fixtures/ImageCompare/image_compare.hpp>
#include <fixtures/ImagePath/image_path.hpp>

#include <gtest/gtest.h>

Baptiste Esteban's avatar
Baptiste Esteban committed
9
10
#include <cstring>

11
12
13
14
15
16
17
18
19
20
static const auto filename = fixtures::ImagePath::concat_with_filename("test.fit");

TEST(IO, cfitsio_not_an_image)
{
  bool has_raised = false;
  try
  {
    auto img = mln::io::fits::imread(filename, 0);
    (void)img;
  }
Baptiste Esteban's avatar
Baptiste Esteban committed
21
  catch (std::runtime_error& e)
22
23
  {
    has_raised = true;
Baptiste Esteban's avatar
Baptiste Esteban committed
24
    ASSERT_EQ(std::strcmp("Unhandled image number of dimension (Got 0, expected in [1 - 4])", e.what()), 0);
25
26
27
28
29
30
  }
  ASSERT_TRUE(has_raised);
}

TEST(IO, cfitsio_2D_uint8)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
31
32
33
34
35
36
37
38
  mln::image2d<std::uint8_t> ref = {
      {0, 1, 2, 3, 4},      //
      {5, 6, 7, 8, 9},      //
      {10, 11, 12, 13, 14}, //
      {15, 16, 17, 18, 19}, //
      {20, 21, 22, 23, 24}  //
  };

39
  auto img = mln::io::fits::imread(filename, 1);
Baptiste Esteban's avatar
Baptiste Esteban committed
40
41
42
43
  ASSERT_EQ(img.sample_type(), mln::sample_type_id::UINT8);
  ASSERT_EQ(img.pdim(), 2);
  ASSERT_EQ(img.width(), 5);
  ASSERT_EQ(img.height(), 5);
Baptiste Esteban's avatar
Baptiste Esteban committed
44
45
46

  auto* casted = img.cast_to<std::uint8_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
47
48
49
50
}

TEST(IO, cfitsio_2D_int16)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
51
52
53
54
55
56
57
58
  mln::image2d<std::int16_t> ref = {
      {0, 1, 2, 3, 4},      //
      {5, 6, 7, 8, 9},      //
      {10, 11, 12, 13, 14}, //
      {15, 16, 17, 18, 19}, //
      {20, 21, 22, 23, 24}  //
  };

59
  auto img = mln::io::fits::imread(filename, 2);
Baptiste Esteban's avatar
Baptiste Esteban committed
60
61
62
63
  ASSERT_EQ(img.sample_type(), mln::sample_type_id::INT16);
  ASSERT_EQ(img.pdim(), 2);
  ASSERT_EQ(img.width(), 5);
  ASSERT_EQ(img.height(), 5);
Baptiste Esteban's avatar
Baptiste Esteban committed
64
65
66

  auto* casted = img.cast_to<std::int16_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
67
68
69
70
}

TEST(IO, cfitsio_2D_int32)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
71
72
73
74
75
76
77
78
  mln::image2d<std::int32_t> ref = {
      {0, 1, 2, 3, 4},      //
      {5, 6, 7, 8, 9},      //
      {10, 11, 12, 13, 14}, //
      {15, 16, 17, 18, 19}, //
      {20, 21, 22, 23, 24}  //
  };

79
  auto img = mln::io::fits::imread(filename, 3);
Baptiste Esteban's avatar
Baptiste Esteban committed
80
81
82
83
  ASSERT_EQ(img.sample_type(), mln::sample_type_id::INT32);
  ASSERT_EQ(img.pdim(), 2);
  ASSERT_EQ(img.width(), 5);
  ASSERT_EQ(img.height(), 5);
Baptiste Esteban's avatar
Baptiste Esteban committed
84
85
86

  auto* casted = img.cast_to<std::int32_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
87
88
89
90
}

TEST(IO, cfitsio_2D_int64)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
91
92
93
94
95
96
97
98
  mln::image2d<std::int64_t> ref = {
      {0, 1, 2, 3, 4},      //
      {5, 6, 7, 8, 9},      //
      {10, 11, 12, 13, 14}, //
      {15, 16, 17, 18, 19}, //
      {20, 21, 22, 23, 24}  //
  };

99
  auto img = mln::io::fits::imread(filename, 4);
Baptiste Esteban's avatar
Baptiste Esteban committed
100
101
102
103
  ASSERT_EQ(img.sample_type(), mln::sample_type_id::INT64);
  ASSERT_EQ(img.pdim(), 2);
  ASSERT_EQ(img.width(), 5);
  ASSERT_EQ(img.height(), 5);
Baptiste Esteban's avatar
Baptiste Esteban committed
104
105
106

  auto* casted = img.cast_to<std::int64_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
107
108
109
110
}

TEST(IO, cfitsio_2D_float)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
111
112
113
114
115
116
117
118
  mln::image2d<float> ref = {
      {0.f, 1.f, 2.f, 3.f, 4.f},      //
      {5.f, 6.f, 7.f, 8.f, 9.f},      //
      {10.f, 11.f, 12.f, 13.f, 14.f}, //
      {15.f, 16.f, 17.f, 18.f, 19.f}, //
      {20.f, 21.f, 22.f, 23.f, 24.f}  //
  };

119
  auto img = mln::io::fits::imread(filename, 5);
Baptiste Esteban's avatar
Baptiste Esteban committed
120
121
122
123
  ASSERT_EQ(img.sample_type(), mln::sample_type_id::FLOAT);
  ASSERT_EQ(img.pdim(), 2);
  ASSERT_EQ(img.width(), 5);
  ASSERT_EQ(img.height(), 5);
Baptiste Esteban's avatar
Baptiste Esteban committed
124
125
126

  auto* casted = img.cast_to<float, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
127
128
129
130
}

TEST(IO, cfitsio_2D_double)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
131
132
133
134
135
136
137
138
  mln::image2d<double> ref = {
      {0.0, 1.0, 2.0, 3.0, 4.0},      //
      {5.0, 6.0, 7.0, 8.0, 9.0},      //
      {10.0, 11.0, 12.0, 13.0, 14.0}, //
      {15.0, 16.0, 17.0, 18.0, 19.0}, //
      {20.0, 21.0, 22.0, 23.0, 24.0}  //
  };

139
  auto img = mln::io::fits::imread(filename, 6);
Baptiste Esteban's avatar
Baptiste Esteban committed
140
141
142
143
  ASSERT_EQ(img.sample_type(), mln::sample_type_id::DOUBLE);
  ASSERT_EQ(img.pdim(), 2);
  ASSERT_EQ(img.width(), 5);
  ASSERT_EQ(img.height(), 5);
Baptiste Esteban's avatar
Baptiste Esteban committed
144
145
146

  auto* casted = img.cast_to<double, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
Baptiste Esteban's avatar
Baptiste Esteban committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
}

TEST(IO, cfitsio_3D)
{
  auto img = mln::io::fits::imread(filename, 7);
  ASSERT_EQ(img.sample_type(), mln::sample_type_id::UINT8);
  ASSERT_EQ(img.pdim(), 3);
  ASSERT_EQ(img.width(), 5);
  ASSERT_EQ(img.height(), 4);

  auto* casted = img.cast_to<std::uint8_t, 3>();
  int   cur    = 0;
  mln_foreach (auto p, casted->domain())
    ASSERT_EQ(casted->operator()(p), cur++);
}

TEST(IO, cfitsio_read_to_argument)
{
  mln::image2d<std::uint8_t> ref = {
      {0, 1, 2, 3, 4},      //
      {5, 6, 7, 8, 9},      //
      {10, 11, 12, 13, 14}, //
      {15, 16, 17, 18, 19}, //
      {20, 21, 22, 23, 24}  //
  };

  mln::image2d<std::uint8_t> img;
  mln::io::fits::imread(filename, img, 1);

  ASSERT_EQ(img.width(), 5);
  ASSERT_EQ(img.height(), 5);

  ASSERT_IMAGES_EQ_EXP(img, ref);
180
}