Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
DICOMTesting/test/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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 /*
18  This test is meant to reproduce the following error:
19 
20  - The machine or current user has a German locale.
21  - This esp. means that stream IO expects the decimal separator as a comma: ","
22  - DICOM files use a point "." as the decimal separator to be locale independent
23  - The parser used by MITK (ITK's GDCM) seems to use the current locale instead of the "C" or "POSIX" locale
24  - This leads to spacings (and probably other numbers) being trimmed/rounded,
25  e.g. the correct spacing of 0.314 is read as 1.0 etc.
26 
27 */
28 
29 #include "mitkDataNodeFactory.h"
31 #include "mitkDicomSeriesReader.h"
32 
33 #include "mitkTestingMacros.h"
34 
35 #include <list>
36 #include <locale>
37 #include <locale.h>
38 
39 bool mitkDICOMLocaleTestChangeLocale(const std::string& locale)
40 {
41  try
42  {
43  MITK_TEST_OUTPUT(<< " ** Changing locale from " << setlocale(LC_ALL, NULL) << " to '" << locale << "'");
44  setlocale(LC_ALL, locale.c_str());
45  std::locale l( locale.c_str() );
46  std::cin.imbue(l);
47  return true;
48  }
49  catch(...)
50  {
51  MITK_TEST_OUTPUT(<< "Could not activate locale " << locale);
52  return false;
53  }
54 
55 }
56 
58 {
61  factory->SetFileName( filename );
62  factory->Update();
63  MITK_TEST_CONDITION_REQUIRED(factory->GetNumberOfOutputs() > 0, "file " << filename << " loaded");
64 
65  mitk::DataNode::Pointer node = factory->GetOutput( 0 );
66  image = dynamic_cast<mitk::Image*>(node->GetData());
67  if(image.IsNull())
68  {
69  MITK_TEST_FAILED_MSG(<< "File "<< filename << " is not an image - test will not be applied." );
70 
71  return;
72  }
73 
74  // note importance of minor differences in spacings:
75  // 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)
76  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(image->GetGeometry()->GetSpacing()[0], 0.3141592), "correct x spacing? found "
77  << image->GetGeometry()->GetSpacing()[0]);
78  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(image->GetGeometry()->GetSpacing()[1], 0.3411592), "correct y spacing? found "
79  << image->GetGeometry()->GetSpacing()[1]);
80 }
81 
82 int mitkDICOMLocaleTest(int argc, char* argv[])
83 {
84  MITK_TEST_BEGIN("DICOMLocaleTest");
85 
86  MITK_TEST_CONDITION_REQUIRED(argc >= 2, "File to load has been specified on commandline");
87 
89 
90  std::string filename = argv[1];
91 
92  // load a reference DICOM file with the "C" locale being set
95  // load a reference DICOM file with German locales being set
96  typedef std::list<std::string> StringList;
97  StringList alllocales;
98  alllocales.push_back("de_DE");
99  alllocales.push_back("de_DE.utf8");
100  alllocales.push_back("de_DE.UTF8");
101  alllocales.push_back("de_DE@euro");
102  alllocales.push_back("German_Germany");
103 
104  // supressing this test to be run on MacOS X
105  // See bug #3894
106 #if defined (__APPLE__) || defined(MACOSX)
107  alllocales.push_back("C");
108 #endif
109 
110  unsigned int numberOfTestedGermanLocales(0);
111 
112  for (StringList::iterator iter = alllocales.begin();
113  iter != alllocales.end();
114  ++iter)
115  {
116  if ( mitkDICOMLocaleTestChangeLocale(*iter) )
117  {
118  ++numberOfTestedGermanLocales;
120  }
121  }
122 
123  if(numberOfTestedGermanLocales == 0)
124  {
125  MITK_TEST_OUTPUT(<< "Warning: No German locale was found on the system.");
126  }
127  //MITK_TEST_CONDITION_REQUIRED( numberOfTestedGermanLocales > 0, "Verify that at least one German locale has been tested.");
128 
129  MITK_TEST_END();
130 }
131 
static std::string GetConfigurationString()
Provide combination of preprocessor defines that was active during compilation.
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
bool mitkDICOMLocaleTestChangeLocale(const std::string &locale)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
int mitkDICOMLocaleTest(int argc, char *argv[])
#define MITK_TEST_OUTPUT(x)
Output some text.
static const std::string filename
#define MITK_TEST_FAILED_MSG(MSG)
Fail and finish test with message MSG.
Image class for storing images.
Definition: mitkImage.h:76
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.
static Pointer New()
and MITK_TEST_END()
void mitkDICOMLocaleTestWithReferenceImage(std::string filename)