cfitsio.cpp 4.14 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
static const auto filename = fixtures::ImagePath::concat_with_filename("test.fit");

TEST(IO, cfitsio_2D_uint8)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
15
16
17
18
19
20
21
22
  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}  //
  };

23
  auto img = mln::io::fits::imread(filename, 1);
Baptiste Esteban's avatar
Baptiste Esteban committed
24
25
26
27
  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
28
29
30

  auto* casted = img.cast_to<std::uint8_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
31
32
33
34
}

TEST(IO, cfitsio_2D_int16)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
35
36
37
38
39
40
41
42
  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}  //
  };

43
  auto img = mln::io::fits::imread(filename, 2);
Baptiste Esteban's avatar
Baptiste Esteban committed
44
45
46
47
  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
48
49
50

  auto* casted = img.cast_to<std::int16_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
51
52
53
54
}

TEST(IO, cfitsio_2D_int32)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
55
56
57
58
59
60
61
62
  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}  //
  };

63
  auto img = mln::io::fits::imread(filename, 3);
Baptiste Esteban's avatar
Baptiste Esteban committed
64
65
66
67
  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
68
69
70

  auto* casted = img.cast_to<std::int32_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
71
72
73
74
}

TEST(IO, cfitsio_2D_int64)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
75
76
77
78
79
80
81
82
  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}  //
  };

83
  auto img = mln::io::fits::imread(filename, 4);
Baptiste Esteban's avatar
Baptiste Esteban committed
84
85
86
87
  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
88
89
90

  auto* casted = img.cast_to<std::int64_t, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
91
92
93
94
}

TEST(IO, cfitsio_2D_float)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
95
96
97
98
99
100
101
102
  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}  //
  };

103
  auto img = mln::io::fits::imread(filename, 5);
Baptiste Esteban's avatar
Baptiste Esteban committed
104
105
106
107
  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
108
109
110

  auto* casted = img.cast_to<float, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
111
112
113
114
}

TEST(IO, cfitsio_2D_double)
{
Baptiste Esteban's avatar
Baptiste Esteban committed
115
116
117
118
119
120
121
122
  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}  //
  };

123
  auto img = mln::io::fits::imread(filename, 6);
Baptiste Esteban's avatar
Baptiste Esteban committed
124
125
126
127
  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
128
129
130

  auto* casted = img.cast_to<double, 2>();
  ASSERT_IMAGES_EQ_EXP(*casted, ref);
Baptiste Esteban's avatar
Baptiste Esteban committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
}

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);
164
}