From 8fc158e27a933706dc168d717a420d52814dfda2 Mon Sep 17 00:00:00 2001 From: okaerin <43344129+okaerin@users.noreply.github.com> Date: Fri, 12 Feb 2021 11:40:36 +0100 Subject: [PATCH 1/2] BUG: Fix number encoding to use C locale The floating point number encoding defaults to the C locale on the standard C++ std IO. The sscanf uses the OS dependent LC_NUMERIC value. This fixes that behavior --- Modules/IO/GDCM/src/itkGDCMImageIO.cxx | 4 ++++ Modules/IO/Stimulate/src/itkStimulateImageIO.cxx | 8 ++++++++ Modules/IO/VTK/src/itkVTKImageIO.cxx | 3 +++ 3 files changed, 15 insertions(+) diff --git a/Modules/IO/GDCM/src/itkGDCMImageIO.cxx b/Modules/IO/GDCM/src/itkGDCMImageIO.cxx index 0ea22e8583c..2042ffd0247 100644 --- a/Modules/IO/GDCM/src/itkGDCMImageIO.cxx +++ b/Modules/IO/GDCM/src/itkGDCMImageIO.cxx @@ -1022,7 +1022,9 @@ GDCMImageIO::Write(const void * buffer) if (hasIPP) { double origin3D[3]; + std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale sscanf(tempString.c_str(), "%lf\\%lf\\%lf", &(origin3D[0]), &(origin3D[1]), &(origin3D[2])); + std::locale::global(currentLocale);//reset locale image.SetOrigin(0, origin3D[0]); image.SetOrigin(1, origin3D[1]); image.SetOrigin(2, origin3D[2]); @@ -1053,6 +1055,7 @@ GDCMImageIO::Write(const void * buffer) if (hasIOP) { double directions[6]; + std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale sscanf(tempString.c_str(), "%lf\\%lf\\%lf\\%lf\\%lf\\%lf", &(directions[0]), @@ -1061,6 +1064,7 @@ GDCMImageIO::Write(const void * buffer) &(directions[3]), &(directions[4]), &(directions[5])); + std::locale::global(currentLocale);//reset locale image.SetDirectionCosines(0, directions[0]); image.SetDirectionCosines(1, directions[1]); image.SetDirectionCosines(2, directions[2]); diff --git a/Modules/IO/Stimulate/src/itkStimulateImageIO.cxx b/Modules/IO/Stimulate/src/itkStimulateImageIO.cxx index 0e6da22f9b7..366913ad214 100644 --- a/Modules/IO/Stimulate/src/itkStimulateImageIO.cxx +++ b/Modules/IO/Stimulate/src/itkStimulateImageIO.cxx @@ -256,7 +256,9 @@ StimulateImageIO::InternalReadImageInformation(std::ifstream & file) // the origin is not specified, but the fov is, then the image is assumed // to be centered: + std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale sscanf(line, "%*s %f %f %f %f", origin, origin + 1, origin + 2, origin + 3); + std::locale::global(currentLocale);//reset locale for (unsigned int i = 0; i < m_NumberOfDimensions; i++) { m_Origin[i] = origin[i]; @@ -277,7 +279,9 @@ StimulateImageIO::InternalReadImageInformation(std::ifstream & file) // not // specified it is calculated according to: fov = interval * dim + std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale sscanf(line, "%*s %f %f %f %f", fov, fov + 1, fov + 2, fov + 3); + std::locale::global(currentLocale);//reset locale fov_specified = true; } else if (text.find("interval") < text.length()) @@ -288,7 +292,9 @@ StimulateImageIO::InternalReadImageInformation(std::ifstream & file) // one value for each dimension. If the interval is not specified it is // calculated according to: interval = fov / dim + std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale sscanf(line, "%*s %f %f %f %f", spacing, spacing + 1, spacing + 2, spacing + 3); + std::locale::global(currentLocale);//reset locale for (unsigned int i = 0; i < m_NumberOfDimensions; i++) { m_Spacing[i] = spacing[i]; @@ -337,7 +343,9 @@ StimulateImageIO::InternalReadImageInformation(std::ifstream & file) // the // low_value and high_value. + std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale sscanf(line, "%*s %f %f", range, range + 1); + std::locale::global(currentLocale);//reset locale m_DisplayRange[0] = range[0]; m_DisplayRange[1] = range[1]; } diff --git a/Modules/IO/VTK/src/itkVTKImageIO.cxx b/Modules/IO/VTK/src/itkVTKImageIO.cxx index ee66ead9d96..45ff499e957 100644 --- a/Modules/IO/VTK/src/itkVTKImageIO.cxx +++ b/Modules/IO/VTK/src/itkVTKImageIO.cxx @@ -237,7 +237,9 @@ VTKImageIO::InternalReadImageInformation(std::ifstream & file) if (text.find("spacing") < text.length() || text.find("aspect_ratio") < text.length()) { double spacing[3]; + std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale sscanf(text.c_str(), "%*s %lf %lf %lf", spacing, spacing + 1, spacing + 2); + std::locale::global(currentLocale);//reset for (unsigned int i = 0; i < m_NumberOfDimensions; i++) { this->SetSpacing(i, spacing[i]); @@ -247,6 +249,7 @@ VTKImageIO::InternalReadImageInformation(std::ifstream & file) else if (text.find("origin") < text.length()) { double origin[3]; + std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale sscanf(text.c_str(), "%*s %lf %lf %lf", origin, origin + 1, origin + 2); for (unsigned int i = 0; i < m_NumberOfDimensions; i++) { From b2d8ba5bf731c6d54a05aa5f1ec0a10506067292 Mon Sep 17 00:00:00 2001 From: okaerin <43344129+okaerin@users.noreply.github.com> Date: Fri, 12 Feb 2021 21:45:19 +0100 Subject: [PATCH 2/2] BUG: fix missing reset fixed a missing locale reset. fixed the mentioned styling issues. --- Modules/IO/GDCM/src/itkGDCMImageIO.cxx | 12 ++++++---- .../IO/Stimulate/src/itkStimulateImageIO.cxx | 24 ++++++++++++------- Modules/IO/VTK/src/itkVTKImageIO.cxx | 11 ++++++--- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/Modules/IO/GDCM/src/itkGDCMImageIO.cxx b/Modules/IO/GDCM/src/itkGDCMImageIO.cxx index 2042ffd0247..344121f6d52 100644 --- a/Modules/IO/GDCM/src/itkGDCMImageIO.cxx +++ b/Modules/IO/GDCM/src/itkGDCMImageIO.cxx @@ -1022,9 +1022,11 @@ GDCMImageIO::Write(const void * buffer) if (hasIPP) { double origin3D[3]; - std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale + // save and reset old locale + std::locale currentLocale = std::locale::global(std::locale::classic()); sscanf(tempString.c_str(), "%lf\\%lf\\%lf", &(origin3D[0]), &(origin3D[1]), &(origin3D[2])); - std::locale::global(currentLocale);//reset locale + // reset locale + std::locale::global(currentLocale); image.SetOrigin(0, origin3D[0]); image.SetOrigin(1, origin3D[1]); image.SetOrigin(2, origin3D[2]); @@ -1055,7 +1057,8 @@ GDCMImageIO::Write(const void * buffer) if (hasIOP) { double directions[6]; - std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale + // save and reset old locale + std::locale currentLocale = std::locale::global(std::locale::classic()); sscanf(tempString.c_str(), "%lf\\%lf\\%lf\\%lf\\%lf\\%lf", &(directions[0]), @@ -1064,7 +1067,8 @@ GDCMImageIO::Write(const void * buffer) &(directions[3]), &(directions[4]), &(directions[5])); - std::locale::global(currentLocale);//reset locale + // reset locale + std::locale::global(currentLocale); image.SetDirectionCosines(0, directions[0]); image.SetDirectionCosines(1, directions[1]); image.SetDirectionCosines(2, directions[2]); diff --git a/Modules/IO/Stimulate/src/itkStimulateImageIO.cxx b/Modules/IO/Stimulate/src/itkStimulateImageIO.cxx index 366913ad214..332a293930c 100644 --- a/Modules/IO/Stimulate/src/itkStimulateImageIO.cxx +++ b/Modules/IO/Stimulate/src/itkStimulateImageIO.cxx @@ -256,9 +256,11 @@ StimulateImageIO::InternalReadImageInformation(std::ifstream & file) // the origin is not specified, but the fov is, then the image is assumed // to be centered: - std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale + // save and reset old locale + std::locale currentLocale = std::locale::global(std::locale::classic()); sscanf(line, "%*s %f %f %f %f", origin, origin + 1, origin + 2, origin + 3); - std::locale::global(currentLocale);//reset locale + // reset locale + std::locale::global(currentLocale); for (unsigned int i = 0; i < m_NumberOfDimensions; i++) { m_Origin[i] = origin[i]; @@ -279,9 +281,11 @@ StimulateImageIO::InternalReadImageInformation(std::ifstream & file) // not // specified it is calculated according to: fov = interval * dim - std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale + // save and reset old locale + std::locale currentLocale = std::locale::global(std::locale::classic()); sscanf(line, "%*s %f %f %f %f", fov, fov + 1, fov + 2, fov + 3); - std::locale::global(currentLocale);//reset locale + // reset locale + std::locale::global(currentLocale); fov_specified = true; } else if (text.find("interval") < text.length()) @@ -292,9 +296,11 @@ StimulateImageIO::InternalReadImageInformation(std::ifstream & file) // one value for each dimension. If the interval is not specified it is // calculated according to: interval = fov / dim - std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale + // save and reset old locale + std::locale currentLocale = std::locale::global(std::locale::classic()); sscanf(line, "%*s %f %f %f %f", spacing, spacing + 1, spacing + 2, spacing + 3); - std::locale::global(currentLocale);//reset locale + // reset locale + std::locale::global(currentLocale); for (unsigned int i = 0; i < m_NumberOfDimensions; i++) { m_Spacing[i] = spacing[i]; @@ -343,9 +349,11 @@ StimulateImageIO::InternalReadImageInformation(std::ifstream & file) // the // low_value and high_value. - std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale + // save and reset old locale + std::locale currentLocale = std::locale::global(std::locale::classic()); sscanf(line, "%*s %f %f", range, range + 1); - std::locale::global(currentLocale);//reset locale + // reset locale + std::locale::global(currentLocale); m_DisplayRange[0] = range[0]; m_DisplayRange[1] = range[1]; } diff --git a/Modules/IO/VTK/src/itkVTKImageIO.cxx b/Modules/IO/VTK/src/itkVTKImageIO.cxx index 45ff499e957..15c1753e2e4 100644 --- a/Modules/IO/VTK/src/itkVTKImageIO.cxx +++ b/Modules/IO/VTK/src/itkVTKImageIO.cxx @@ -237,9 +237,11 @@ VTKImageIO::InternalReadImageInformation(std::ifstream & file) if (text.find("spacing") < text.length() || text.find("aspect_ratio") < text.length()) { double spacing[3]; - std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale + // save and reset old locale + std::locale currentLocale = std::locale::global(std::locale::classic()); sscanf(text.c_str(), "%*s %lf %lf %lf", spacing, spacing + 1, spacing + 2); - std::locale::global(currentLocale);//reset + // reset locale + std::locale::global(currentLocale); for (unsigned int i = 0; i < m_NumberOfDimensions; i++) { this->SetSpacing(i, spacing[i]); @@ -249,8 +251,11 @@ VTKImageIO::InternalReadImageInformation(std::ifstream & file) else if (text.find("origin") < text.length()) { double origin[3]; - std::locale currentLocale = std::locale::global(std::locale::classic());//save and reset old locale + // save and reset old locale + std::locale currentLocale = std::locale::global(std::locale::classic()); sscanf(text.c_str(), "%*s %lf %lf %lf", origin, origin + 1, origin + 2); + // reset locale + std::locale::global(currentLocale); for (unsigned int i = 0; i < m_NumberOfDimensions; i++) { this->SetOrigin(i, origin[i]);