Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkDICOMLocaleTest.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 /*
14  This test is meant to reproduce the following error:
15 
16  - The machine or current user has a German locale.
17  - This esp. means that stream IO expects the decimal separator as a comma: ","
18  - DICOM files use a point "." as the decimal separator to be locale independent
19  - The parser used by MITK (ITK's GDCM) seems to use the current locale instead of the "C" or "POSIX" locale
20  - This leads to spacings (and probably other numbers) being trimmed/rounded,
21  e.g. the correct spacing of 0.314 is read as 1.0 etc.
22 
23 */
24 
26 #include "mitkTestDICOMLoading.h"
27 
28 #include "mitkTestingMacros.h"
29 
30 #include <list>
31 #include <locale>
32 #include <locale.h>
33 
34 bool mitkDICOMLocaleTestChangeLocale(const std::string& locale)
35 {
36  try
37  {
38  MITK_TEST_OUTPUT(<< " ** Changing locale from " << setlocale(LC_ALL, nullptr) << " to '" << locale << "'");
39  setlocale(LC_ALL, locale.c_str());
40  std::locale l( locale.c_str() );
41  std::cin.imbue(l);
42  return true;
43  }
44  catch(...)
45  {
46  MITK_TEST_OUTPUT(<< "Could not activate locale " << locale);
47  return false;
48  }
49 
50 }
51 
52 void mitkDICOMLocaleTestWithReferenceImage(std::string filename)
53 {
55 
56  mitk::TestDICOMLoading::ImageList images = loader.LoadFiles({ filename });
57 
58  MITK_TEST_CONDITION_REQUIRED(images.size() > 0, "file " << filename << " loaded");
59 
60  mitk::DataNode::Pointer node = factory->GetOutput( 0 );
61  image = dynamic_cast<mitk::Image*>(node->GetData());
62 
64  if(images.empty())
65  {
66  MITK_TEST_FAILED_MSG(<< "File "<< filename << " is not an image - test will not be applied." );
67 
68  return;
69  }
70  else
71  {
72  image = images[0];
73  }
74 
75  // note importance of minor differences in spacings:
76  // DICOM has order y-spacing, x-spacing, while in MITK we assume x-spacing, y-spacing (both meant for 0 and 1 index in array)
77  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(image->GetGeometry()->GetSpacing()[0], 0.3141592), "correct x spacing? found "
78  << image->GetGeometry()->GetSpacing()[0]);
79  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(image->GetGeometry()->GetSpacing()[1], 0.3411592), "correct y spacing? found "
80  << image->GetGeometry()->GetSpacing()[1]);
81 }
82 
83 int mitkDICOMLocaleTest(int argc, char* argv[])
84 {
85  MITK_TEST_BEGIN("DICOMLocaleTest");
86 
87  MITK_TEST_CONDITION_REQUIRED(argc >= 2, "File to load has been specified on commandline");
88 
90 
91  std::string filename = argv[1];
92 
93  // load a reference DICOM file with the "C" locale being set
96  // load a reference DICOM file with German locales being set
97  typedef std::list<std::string> StringList;
98  StringList alllocales;
99  alllocales.push_back("de_DE");
100  alllocales.push_back("de_DE.utf8");
101  alllocales.push_back("de_DE.UTF8");
102  alllocales.push_back("de_DE@euro");
103  alllocales.push_back("German_Germany");
104 
105  // supressing this test to be run on MacOS X
106  // See bug #3894
107 #if defined (__APPLE__) || defined(MACOSX)
108  alllocales.push_back("C");
109 #endif
110 
111  unsigned int numberOfTestedGermanLocales(0);
112 
113  for (StringList::iterator iter = alllocales.begin();
114  iter != alllocales.end();
115  ++iter)
116  {
117  if ( mitkDICOMLocaleTestChangeLocale(*iter) )
118  {
119  ++numberOfTestedGermanLocales;
121  }
122  }
123 
124  if(numberOfTestedGermanLocales == 0)
125  {
126  MITK_TEST_OUTPUT(<< "Warning: No German locale was found on the system.");
127  }
128  //MITK_TEST_CONDITION_REQUIRED( numberOfTestedGermanLocales > 0, "Verify that at least one German locale has been tested.");
129 
130  MITK_TEST_END();
131 }
132 
ImageVectorType images
bool mitkDICOMLocaleTestChangeLocale(const std::string &locale)
static std::string GetConfigurationString()
Provide combination of preprocessor defines that was active during compilation.
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
#define MITK_TEST_OUTPUT(x)
Output some text.
std::list< Image::Pointer > ImageList
void mitkDICOMLocaleTestWithReferenceImage(std::string filename)
#define MITK_TEST_FAILED_MSG(MSG)
Fail and finish test with message MSG.
Image class for storing images.
Definition: mitkImage.h:72
mitk::Image::Pointer image
std::vector< std::string > StringList
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
int mitkDICOMLocaleTest(int argc, char *argv[])
and MITK_TEST_END()
ImageList LoadFiles(const StringList &files)