File: test_niftiimage.cc

package info (click to toggle)
mia 2.4.7-16
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,332 kB
  • sloc: cpp: 162,690; python: 1,317; sh: 321; xml: 127; makefile: 41; csh: 24; ansic: 9
file content (156 lines) | stat: -rw-r--r-- 5,350 bytes parent folder | download | duplicates (3)
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
/* -*- mia-c++  -*-
 *
 * This file is part of MIA - a toolbox for medical image analysis
 * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
 *
 * MIA is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MIA; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#define VSTREAM_DOMAIN "NiftiImageIOtest"

#include <mia/internal/autotest.hh>

#include <boost/mpl/vector.hpp>

#include <nifti/niftiimage.hh>
#include <unistd.h>

using namespace mia;
using namespace std;
using namespace niftiimage;
namespace bmpl = boost::mpl;


typedef bmpl::vector<int8_t,
        uint8_t,
        int16_t,
        uint16_t,
        int32_t,
        uint32_t,
        int64_t,
        uint64_t,
        float,
        double
        > test_types;


BOOST_AUTO_TEST_CASE_TEMPLATE( test_simple_write_read_with_qform, T, test_types )
{
       C3DBounds size(2, 3, 4);
       T3DImage<T> *image = new T3DImage<T>(size);
       P3DImage pimage(image);
       C3DFVector voxel(2.0, 3.0, 4.0);
       C3DFVector origin(10, 20, 30);
       Quaternion rot(0.5, 0.1, 0.5, 0.7);
       auto iv = image->begin();
       auto ev = image->end();
       int i = 0;

       while (iv != ev)
              *iv++ = i++;

       pimage->set_voxel_size(voxel);
       pimage->set_origin(origin);
       pimage->set_rotation(rot);
       pimage->set_attribute("nifti-qform-code", 1);
       CNifti3DImageIOPlugin io;
       CNifti3DImageIOPlugin::Data images;
       images.push_back(pimage);
       stringstream filename;
       filename << "testimage-sform-" << __type_descr<T>::value << ".nii";
       cvdebug() << "test with " << filename.str() << "\n";
       BOOST_REQUIRE(io.save(filename.str(), images));
       auto loaded = io.load(filename.str());
       BOOST_REQUIRE(loaded);
       BOOST_REQUIRE(loaded->size() == 1u);
       const auto& ploaded = dynamic_cast<const T3DImage<T>&>(*(*loaded)[0]);
       iv = image->begin();
       auto il = ploaded.begin();

       while (iv != ev) {
              BOOST_CHECK_EQUAL(*il, *iv);
              ++iv;
              ++il;
       }

       BOOST_CHECK_EQUAL(ploaded.get_voxel_size(), voxel);
       BOOST_CHECK_CLOSE(ploaded.get_origin().x, origin.x, 0.001);
       BOOST_CHECK_CLOSE(ploaded.get_origin().y, origin.y, 0.001);
       BOOST_CHECK_CLOSE(ploaded.get_origin().z, origin.z, 0.001);
       auto qloaded = ploaded.get_rotation().as_quaternion();
       BOOST_CHECK_CLOSE(qloaded.x(), rot.x(), 0.001);
       BOOST_CHECK_CLOSE(qloaded.y(), rot.y(), 0.001);
       BOOST_CHECK_CLOSE(qloaded.z(), rot.z(), 0.001);
       BOOST_CHECK_CLOSE(qloaded.w(), rot.w(), 0.001);
       unlink(filename.str().c_str());
}

BOOST_AUTO_TEST_CASE_TEMPLATE( test_simple_write_read_write_with_sform, T, test_types )
{
       C3DBounds size(2, 3, 4);
       T3DImage<T> *image = new T3DImage<T>(size);
       P3DImage pimage(image);
       C3DFVector voxel(2.0, 3.0, 4.0);
       C3DFVector origin(10, 20, 30);
       Quaternion rot(0.5, 0.1, 0.5, 0.7);
       auto iv = image->begin();
       auto ev = image->end();
       int i = 0;

       while (iv != ev)
              *iv++ = i++;

       pimage->set_voxel_size(voxel);
       pimage->set_origin(origin);
       pimage->set_rotation(rot);
       pimage->set_attribute("nifti-sform-code", 1);
       vector<float> am = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
       pimage->set_attribute("nifti-sform", am);
       CNifti3DImageIOPlugin io;
       CNifti3DImageIOPlugin::Data images;
       images.push_back(pimage);
       stringstream filename;
       filename << "testimage-" << __type_descr<T>::value << ".nii";
       cvdebug() << "test with " << filename.str() << "\n";
       BOOST_REQUIRE(io.save(filename.str(), images));
       auto loaded = io.load(filename.str());
       BOOST_REQUIRE(loaded);
       BOOST_REQUIRE(loaded->size() == 1u);
       const auto& ploaded = dynamic_cast<const T3DImage<T>&>(*(*loaded)[0]);
       iv = image->begin();
       auto il = ploaded.begin();

       while (iv != ev) {
              BOOST_CHECK_EQUAL(*il, *iv);
              ++iv;
              ++il;
       }

       BOOST_CHECK_EQUAL(ploaded.get_voxel_size(), voxel);
       BOOST_CHECK_CLOSE(ploaded.get_origin().x, origin.x, 0.001);
       BOOST_CHECK_CLOSE(ploaded.get_origin().y, origin.y, 0.001);
       BOOST_CHECK_CLOSE(ploaded.get_origin().z, origin.z, 0.001);
       auto qloaded = ploaded.get_rotation().as_quaternion();
       BOOST_CHECK_CLOSE(qloaded.x(), rot.x(), 0.001);
       BOOST_CHECK_CLOSE(qloaded.y(), rot.y(), 0.001);
       BOOST_CHECK_CLOSE(qloaded.z(), rot.z(), 0.001);
       BOOST_CHECK_CLOSE(qloaded.w(), rot.w(), 0.001);
       unlink(filename.str().c_str());
       // try saving again to check attribute translation
       BOOST_REQUIRE(io.save(filename.str(), *loaded));
       unlink(filename.str().c_str());
}