Skip to content

Commit 62952dd

Browse files
hjmjohnsonclaude
andcommitted
ENH: Use NumberToString for lossless float metadata in MetaImage IO
Non-template MetaData fields of type double, float, vector<double>, and Matrix<double> were serialized via std::ostringstream with default 6-digit precision, silently truncating values with more than 6 significant digits. Replace the lossy stream insertions with itk::ConvertNumberToString(), which wraps Google double-conversion::ToShortest() and produces the shortest decimal string that round-trips exactly through strtod/strtof. Three call sites are affected: - ExposeMetaData<double> / ExposeMetaData<float> in WriteImageInformation() - ExposeMetaData<std::vector<double>> (manual loop replacing joinElements) - WriteMatrixInMetaData<N>() template in the header See: #3249 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 61cc38d commit 62952dd

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

Modules/IO/Meta/include/itkMetaImageIO.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <fstream>
2424
#include "itkImageIOBase.h"
25+
#include "itkNumberToString.h"
2526
#include "itkSingletonMacro.h"
2627
#include "itkMetaDataObject.h"
2728
#include "metaObject.h"
@@ -220,7 +221,7 @@ MetaImageIO::WriteMatrixInMetaData(std::ostringstream & strs,
220221
{
221222
for (unsigned int j = 0; j < VNColumns; ++j)
222223
{
223-
strs << mval[i][j];
224+
strs << itk::ConvertNumberToString(mval[i][j]);
224225
if (i != VNRows - 1 || j != VNColumns - 1)
225226
{
226227
strs << ' ';

Modules/IO/Meta/src/itkMetaImageIO.cxx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "itkIOCommon.h"
2222
#include "itksys/SystemTools.hxx"
2323
#include "itkMath.h"
24+
#include "itkNumberToString.h"
2425
#include "itkSingleton.h"
2526
#include "itkMakeUniqueForOverwrite.h"
2627
#include "metaImageUtils.h"
@@ -458,11 +459,11 @@ MetaImageIO::WriteImageInformation()
458459
}
459460
else if (ExposeMetaData<double>(metaDict, key, dval))
460461
{
461-
strs << dval;
462+
strs << itk::ConvertNumberToString(dval);
462463
}
463464
else if (ExposeMetaData<float>(metaDict, key, fval))
464465
{
465-
strs << fval;
466+
strs << itk::ConvertNumberToString(fval);
466467
}
467468
else if (ExposeMetaData<long>(metaDict, key, lval))
468469
{
@@ -510,7 +511,14 @@ MetaImageIO::WriteImageInformation()
510511
}
511512
else if (ExposeMetaData<std::vector<double>>(metaDict, key, vval))
512513
{
513-
joinElements(vval, ' ', strs);
514+
for (size_t i = 0; i < vval.size(); ++i)
515+
{
516+
if (i > 0)
517+
{
518+
strs << ' ';
519+
}
520+
strs << itk::ConvertNumberToString(vval[i]);
521+
}
514522
}
515523
else if (WriteMatrixInMetaData<1>(strs, metaDict, key) || WriteMatrixInMetaData<2>(strs, metaDict, key) ||
516524
WriteMatrixInMetaData<3>(strs, metaDict, key) || WriteMatrixInMetaData<4>(strs, metaDict, key) ||

0 commit comments

Comments
 (0)