rexcardan / Evil-DICOM

A C# DICOM Library
171 stars 98 forks source link

Non-conformant Decimal Strings #50

Closed zaxasheats closed 7 years ago

zaxasheats commented 7 years ago

I think I've got another one for you.

Some of the data I must output has to be rescaled using RescaleSlope and RescaleIntercept. I do some double precision math to determine those values and assign them to the DICOMObject tags.

It appears when the write happens, the maximum length and formatting of the exponent is not enforced. In Evil DICOM produced file for slope I have a value of "3.05171124637127E-05". DICOM only allows 16 bytes there so DVTk is rejecting it for being too long.

Also it claims "E-05" is an invalid exponent. From what I can determine it doesn't like leading zeroes in the exponent.

I dropped the leading zero from the exponent and removed 3 more characters from the end of the decimal string and that made DVTk happy. The result was 3.05171124637E-5

So I think a bit more formatting needs to happen when you write a DS VR.

Thanks, Matt

BTW, your CodeString fix was perfect. Thanks for that!

rexcardan commented 7 years ago

Ok. I had some time to fix this. This is my new DS formatter:

        public static string ComposeDecimalString(double[] data)
        {
            if (data != null)
                return string.Join("\\", data.Select(d =>
                {
                    //Max character is 16. Start with 12 decimal and work our way down
                    var limit = 12;
                    string format = string.Empty;
                    while ((format = d.ToString($"G{limit}", CultureInfo.InvariantCulture)).Length > 16)
                    {
                        limit--;
                    };
                    return format;
                }).ToArray());

            return string.Empty;
        }

Its in the new source, and NuGet v2.0.1.5