00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date$ 00006 Version: $Revision: 1.12 $ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 /* 00019 This test is meant to reproduce the following error: 00020 00021 - The machine or current user has a German locale. 00022 - This esp. means that stream IO expects the decimal separator as a comma: "," 00023 - DICOM files use a point "." as the decimal separator to be locale independent 00024 - The parser used by MITK (ITK's GDCM) seems to use the current locale instead of the "C" or "POSIX" locale 00025 - This leads to spacings (and probably other numbers) being trimmed/rounded, 00026 e.g. the correct spacing of 0.314 is read as 1.0 etc. 00027 00028 MITK shold work around this behavior. This test is meant to verify any workaround 00029 and will be activated once such a workaround is available. 00030 00031 */ 00032 00033 #include "mitkDataNodeFactory.h" 00034 #include "mitkStandardFileLocations.h" 00035 00036 #include "mitkTestingMacros.h" 00037 00038 #include <list> 00039 #include <locale> 00040 #include <locale.h> 00041 00042 bool mitkDICOMLocaleTestChangeLocale(const std::string& locale) 00043 { 00044 try 00045 { 00046 MITK_TEST_OUTPUT(<< " ** Changing locale from " << setlocale(LC_ALL, NULL) << " to '" << locale << "'"); 00047 setlocale(LC_ALL, locale.c_str()); 00048 std::locale l( locale.c_str() ); 00049 std::cin.imbue(l); 00050 return true; 00051 } 00052 catch(...) 00053 { 00054 MITK_TEST_OUTPUT(<< "Could not activate locale " << locale); 00055 return false; 00056 } 00057 00058 } 00059 00060 void mitkDICOMLocaleTestWithReferenceImage() 00061 { 00062 mitk::StandardFileLocations::Pointer locator = mitk::StandardFileLocations::GetInstance(); 00063 MITK_TEST_CONDITION_REQUIRED(locator.IsNotNull(),"Instantiating StandardFileLocations"); 00064 std::string filename = locator->FindFile("spacing-ok.dcm", "Core/Code/Testing/Data/"); 00065 00066 mitk::Image::Pointer image; 00067 mitk::DataNodeFactory::Pointer factory = mitk::DataNodeFactory::New(); 00068 factory->SetFileName( filename ); 00069 factory->Update(); 00070 MITK_TEST_CONDITION_REQUIRED(factory->GetNumberOfOutputs() > 0, "file loaded"); 00071 00072 mitk::DataNode::Pointer node = factory->GetOutput( 0 ); 00073 image = dynamic_cast<mitk::Image*>(node->GetData()); 00074 if(image.IsNull()) 00075 { 00076 MITK_TEST_FAILED_MSG(<< "File "<< filename << " is not an image - test will not be applied." ); 00077 00078 return; 00079 } 00080 00081 MITK_TEST_OUTPUT(<< "File "<< filename << " could be loaded." ); 00082 00083 MITK_TEST_OUTPUT(<< "MITK image reports pixel spacing of " << image->GetGeometry()->GetSpacing()[0] << " " << image->GetGeometry()->GetSpacing()[1] ); 00084 MITK_TEST_CONDITION_REQUIRED(image->GetGeometry()->GetSpacing()[0] - 0.3141592 < 0.0000001, "correct x spacing"); 00085 MITK_TEST_CONDITION_REQUIRED(image->GetGeometry()->GetSpacing()[1] - 0.3141592 < 0.0000001, "correct y spacing"); 00086 } 00087 00088 int mitkDICOMLocaleTest(int /*argc*/, char* /*argv*/ []) 00089 { 00090 MITK_TEST_BEGIN("DICOMLocaleTest"); 00091 00092 // load a reference DICOM file with the "C" locale being set 00093 mitkDICOMLocaleTestChangeLocale("C"); 00094 mitkDICOMLocaleTestWithReferenceImage(); 00095 // load a reference DICOM file with German locales being set 00096 typedef std::list<std::string> StringList; 00097 StringList alllocales; 00098 alllocales.push_back("de_DE"); 00099 alllocales.push_back("de_DE.utf8"); 00100 alllocales.push_back("de_DE.UTF8"); 00101 alllocales.push_back("de_DE@euro"); 00102 alllocales.push_back("German_Germany"); 00103 00104 // QuickFix for MAC OS X 00105 // See for more the Bug #3894 comments 00106 #if defined (__APPLE__) || defined(MACOSX) 00107 alllocales.push_back("C"); 00108 #endif 00109 00110 unsigned int numberOfTestedGermanLocales(0); 00111 00112 for (StringList::iterator iter = alllocales.begin(); 00113 iter != alllocales.end(); 00114 ++iter) 00115 { 00116 if ( mitkDICOMLocaleTestChangeLocale(*iter) ) 00117 { 00118 ++numberOfTestedGermanLocales; 00119 mitkDICOMLocaleTestWithReferenceImage(); 00120 } 00121 } 00122 00123 MITK_TEST_CONDITION_REQUIRED( numberOfTestedGermanLocales > 0, "Verify that at least one German locale has been tested."); 00124 00125 MITK_TEST_END(); 00126 } 00127