cfitsio.cpp 3.67 KB
Newer Older
Baptiste Esteban's avatar
Baptiste Esteban committed
1
#include <mln/core/image/ndimage.hpp>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <mln/io/imread.hpp>

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

#include <gtest/gtest.h>

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;
  }
  catch (std::runtime_error&)
  {
    has_raised = true;
  }
  ASSERT_TRUE(has_raised);
}

TEST(IO, cfitsio_2D_uint8)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
28
29
30
31
32
33
34
35
  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}  //
  };

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

  auto* casted = img.cast_to<std::uint8_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
44
45
46
47
}

TEST(IO, cfitsio_2D_int16)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
48
49
50
51
52
53
54
55
  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}  //
  };

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

  auto* casted = img.cast_to<std::int16_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
64
65
66
67
}

TEST(IO, cfitsio_2D_int32)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
68
69
70
71
72
73
74
75
  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}  //
  };

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

  auto* casted = img.cast_to<std::int32_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
84
85
86
87
}

TEST(IO, cfitsio_2D_int64)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
88
89
90
91
92
93
94
95
  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}  //
  };

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

  auto* casted = img.cast_to<std::int64_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
104
105
106
107
}

TEST(IO, cfitsio_2D_float)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
108
109
110
111
112
113
114
115
  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}  //
  };

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

  auto* casted = img.cast_to<float, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
124
125
126
127
}

TEST(IO, cfitsio_2D_double)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
128
129
130
131
132
133
134
135
  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}  //
  };

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

  auto* casted = img.cast_to<double, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
144
}