cfitsio.cpp 3.8 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_TRUE(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
40
  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
41
42
43
44
45
46
  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);
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
60
  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
61
62
63
64
65
66
  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);
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
80
  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
81
82
83
84
85
86
  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);
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
100
  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
101
102
103
104
105
106
  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);
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
120
  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
121
122
123
124
125
126
  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);
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
140
  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
141
142
143
144
145
146
  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);
147
}