~ubuntu-branches/debian/sid/simpleitk/sid

« back to all changes in this revision

Viewing changes to Testing/Unit/IOTests.cxx

  • Committer: Package Import Robot
  • Author(s): Ghislain Antony Vaillant
  • Date: 2017-11-02 08:49:18 UTC
  • Revision ID: package-import@ubuntu.com-20171102084918-7hs09ih668xq87ej
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=========================================================================
 
2
*
 
3
*  Copyright Insight Software Consortium
 
4
*
 
5
*  Licensed under the Apache License, Version 2.0 (the "License");
 
6
*  you may not use this file except in compliance with the License.
 
7
*  You may obtain a copy of the License at
 
8
*
 
9
*         http://www.apache.org/licenses/LICENSE-2.0.txt
 
10
*
 
11
*  Unless required by applicable law or agreed to in writing, software
 
12
*  distributed under the License is distributed on an "AS IS" BASIS,
 
13
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
*  See the License for the specific language governing permissions and
 
15
*  limitations under the License.
 
16
*
 
17
*=========================================================================*/
 
18
#include <SimpleITKTestHarness.h>
 
19
#include <sitkImageFileReader.h>
 
20
#include <sitkImageSeriesReader.h>
 
21
#include <sitkImageFileWriter.h>
 
22
#include <sitkHashImageFilter.h>
 
23
 
 
24
TEST(IO,ImageFileReader) {
 
25
  itk::simple::HashImageFilter hasher;
 
26
  itk::simple::ImageFileReader reader;
 
27
 
 
28
  typedef std::map<std::string,std::string> MapType;
 
29
  MapType mapping;
 
30
 
 
31
  // Configure the mapping between filename and MD5 hash
 
32
  mapping["Input/RA-Short.nrrd"]            = "a963bd6a755b853103a2d195e01a50d3";
 
33
  mapping["Input/RA-Float.nrrd"]            = "3ccccde44efaa3d688a86e94335c1f16";
 
34
  mapping["Input/RA-Slice-Short.nrrd"]      = "22cdc0af7d51934a744b9c4fd4748cd1";
 
35
  mapping["Input/RA-Slice-Float.nrrd"]      = "999078d36a4491d691cc93d8c3ed29fc";
 
36
  mapping["Input/Ramp-Zero-One-Float.nrrd"] = "f47efe1a6f48cb5cfe1aa23232f1168b";
 
37
  mapping["Input/Ramp-One-Zero-Float.nrrd"] = "0b18fac85955571d069b3666ff987119";
 
38
  mapping["Input/Ramp-Up-Short.nrrd"]       = "86f2ea8a68b3069f33f2271829a30aa2";
 
39
  mapping["Input/Ramp-Down-Short.nrrd"]     = "71f5c852a6251069fa1d60f796463343";
 
40
  mapping["Input/STAPLE1.png"]              = "095f00a68a84df4396914fa758f34dcc";
 
41
  mapping["Input/STAPLE2.png"]              = "301858f5eee39b092d28d9837f008fb8";
 
42
 
 
43
 
 
44
  // Loop over the map, load each file, and compare the hash value
 
45
  for ( MapType::iterator it = mapping.begin(); it != mapping.end(); ++it ) {
 
46
    reader.SetFileName ( dataFinder.GetFile ( it->first ) );
 
47
    EXPECT_EQ ( reader.GetFileName(), dataFinder.GetFile ( it->first ) );
 
48
    itk::simple::Image image = reader.Execute();
 
49
    ASSERT_TRUE ( image.GetITKBase() != NULL );
 
50
    hasher.SetHashFunction ( itk::simple::HashImageFilter::MD5 );
 
51
    EXPECT_EQ ( it->second, hasher.Execute ( image ) ) << " reading " << it->first;
 
52
    // Try the functional interface
 
53
    EXPECT_EQ ( it->second, hasher.Execute ( itk::simple::ReadImage ( dataFinder.GetFile ( it->first ) ) ) ) << "Functional interface";
 
54
  }
 
55
 
 
56
}
 
57
 
 
58
TEST(IO,ReadWrite) {
 
59
  itk::simple::HashImageFilter hasher;
 
60
  itk::simple::ImageFileReader reader;
 
61
  itk::simple::ImageFileWriter writer;
 
62
 
 
63
  // From the command line utility
 
64
  std::string md5 = "a963bd6a755b853103a2d195e01a50d3";
 
65
  std::string sha1 = "126ea8c3ef5573ca1e4e0deece920c2c4a4f38b5";
 
66
 
 
67
 
 
68
  itk::simple::Image image = reader.SetFileName ( dataFinder.GetFile ( "Input/RA-Short.nrrd" ) ).Execute();
 
69
  ASSERT_TRUE ( image.GetITKBase() != NULL );
 
70
  hasher.SetHashFunction ( itk::simple::HashImageFilter::MD5 );
 
71
  EXPECT_EQ ( md5, hasher.Execute ( image ) );
 
72
  hasher.SetHashFunction ( itk::simple::HashImageFilter::SHA1 );
 
73
  EXPECT_EQ ( sha1, hasher.Execute ( image ) );
 
74
 
 
75
  // Write it out
 
76
  std::string filename = dataFinder.GetOutputFile ( "IO.ReadWrite.nrrd" );
 
77
  writer.SetFileName ( filename ).Execute ( image );
 
78
  ASSERT_TRUE ( dataFinder.FileExists ( filename ) );
 
79
 
 
80
  image = reader.SetFileName ( filename ).Execute();
 
81
  ASSERT_TRUE ( image.GetITKBase() != NULL );
 
82
 
 
83
  // Make sure we wrote and read the file correctly
 
84
  hasher.SetHashFunction ( itk::simple::HashImageFilter::MD5 );
 
85
  EXPECT_EQ ( md5, hasher.Execute ( image ) );
 
86
  hasher.SetHashFunction ( itk::simple::HashImageFilter::SHA1 );
 
87
  EXPECT_EQ ( sha1, hasher.Execute ( image ) );
 
88
 
 
89
  // Again, with the functional interface
 
90
  filename = dataFinder.GetOutputFile ( "IO.ReadWrite-Functional.nrrd" );
 
91
  itk::simple::WriteImage ( image, filename );
 
92
  ASSERT_TRUE ( dataFinder.FileExists ( filename ) );
 
93
 
 
94
  image = reader.SetFileName ( filename ).Execute();
 
95
  ASSERT_TRUE ( image.GetITKBase() != NULL );
 
96
 
 
97
  // Make sure we wrote and read the file correctly
 
98
  hasher.SetHashFunction ( itk::simple::HashImageFilter::MD5 );
 
99
  EXPECT_EQ ( md5, hasher.Execute ( image ) );
 
100
  hasher.SetHashFunction ( itk::simple::HashImageFilter::SHA1 );
 
101
  EXPECT_EQ ( sha1, hasher.Execute ( image ) );
 
102
 
 
103
}
 
104
 
 
105
 
 
106
TEST(IO,2DFormats) {
 
107
  itk::simple::HashImageFilter hasher;
 
108
  itk::simple::ImageFileReader reader;
 
109
 
 
110
  itk::simple::Image image = reader.SetFileName ( dataFinder.GetFile ( "Input/RA-Slice-Short.png" ) ).Execute();
 
111
  ASSERT_TRUE ( image.GetITKBase() != NULL );
 
112
  hasher.SetHashFunction ( itk::simple::HashImageFilter::SHA1 );
 
113
  EXPECT_EQ ( "bf0f7bae60b0322222e224941c31f37a981901aa", hasher.Execute ( image ) );
 
114
  ASSERT_EQ ( 2u, image.GetDimension() );
 
115
  EXPECT_EQ ( 64u, image.GetWidth() );
 
116
  EXPECT_EQ ( 64u, image.GetHeight() );
 
117
  EXPECT_EQ ( 0u, image.GetDepth() );
 
118
 
 
119
}
 
120
 
 
121
namespace sitk = itk::simple;
 
122
 
 
123
TEST(IO, SeriesReader) {
 
124
 
 
125
  std::vector< std::string > fileNames;
 
126
  fileNames.push_back( dataFinder.GetFile ( "Input/BlackDots.png" ) );
 
127
  fileNames.push_back( dataFinder.GetFile ( "Input/BlackDots.png" ) );
 
128
  fileNames.push_back( dataFinder.GetFile ( "Input/BlackDots.png" ) );
 
129
 
 
130
  sitk::ImageSeriesReader reader;
 
131
  sitk::Image image = reader.SetFileNames ( fileNames ).Execute();
 
132
  EXPECT_EQ ( "b13c0a17109e3a5058e8f225c9ef2dbcf79ac240", sitk::Hash( image ) );
 
133
  EXPECT_EQ ( 3u, image.GetDimension() );
 
134
  EXPECT_EQ ( 256u, image.GetWidth() );
 
135
  EXPECT_EQ ( 256u, image.GetHeight() );
 
136
  EXPECT_EQ ( 3u, image.GetDepth() );
 
137
 
 
138
  fileNames.push_back( dataFinder.GetFile ( "Input/WhiteDots.png" ));
 
139
  image = sitk::ReadImage( fileNames );
 
140
  EXPECT_EQ ( "62fff5903956f108fbafd506e31c1e733e527820", sitk::Hash( image ) );
 
141
  EXPECT_EQ ( 4u, image.GetDepth() );
 
142
 
 
143
  fileNames.resize(0);
 
144
  fileNames.push_back( dataFinder.GetFile ( "Input/VM1111Shrink-RGB.png" ) );
 
145
  fileNames.push_back( dataFinder.GetFile ( "Input/VM1111Shrink-RGB.png" ) );
 
146
  fileNames.push_back( dataFinder.GetFile ( "Input/VM1111Shrink-RGB.png" ) );
 
147
  reader.SetFileNames ( fileNames );
 
148
  image = reader.Execute();
 
149
  EXPECT_EQ ( 3u, image.GetDepth() );
 
150
  EXPECT_EQ ( "bb42b8d3991132b4860adbc4b3f6c38313f52b4c", sitk::Hash( image ) );
 
151
 
 
152
}
 
153
 
 
154
TEST(IO,WriteOptions) {
 
155
 
 
156
  sitk::ImageFileWriter writer;
 
157
 
 
158
  EXPECT_EQ( false, writer.GetUseCompression() );
 
159
 
 
160
  writer.SetUseCompression( false );
 
161
  EXPECT_EQ( false, writer.GetUseCompression() );
 
162
 
 
163
  writer.UseCompressionOn();
 
164
  EXPECT_EQ( true, writer.GetUseCompression() );
 
165
 
 
166
  writer.UseCompressionOff();
 
167
  EXPECT_EQ( false, writer.GetUseCompression() );
 
168
 
 
169
  sitk::Image image = sitk::ReadImage( dataFinder.GetFile ( "Input/BlackDots.png" ) );
 
170
  EXPECT_EQ ( "0188164c9932359b3f33f176d0d73661c4dc04a8", sitk::Hash( image ) );
 
171
 
 
172
  writer.Execute( image, dataFinder.GetOutputFile( "with_compression.nrrd" ), true );
 
173
  EXPECT_EQ( true, writer.GetUseCompression() );
 
174
 
 
175
  writer.Execute( image, dataFinder.GetOutputFile( "without_compression.nrrd" ), false );
 
176
  EXPECT_EQ( false, writer.GetUseCompression() );
 
177
 
 
178
}
 
179
 
 
180
TEST(IO,Write) {
 
181
 
 
182
  sitk::Image image = sitk::ReadImage( dataFinder.GetFile ( "Input/BlackDots.png" ) );
 
183
  EXPECT_EQ ( "0188164c9932359b3f33f176d0d73661c4dc04a8", sitk::Hash( image ) );
 
184
 
 
185
  ASSERT_THROW(sitk::WriteImage( image, dataFinder.GetOutputFile ( "this.isafilenamewithnoimageio" ) ),  std::exception ) << "Checking for assert on bad output image name.";
 
186
}