Search code examples
c#dicomgdcm

GDCM library cannot read dicom file in C#


I tried to read a DICOM file using GDCM library using this code :

gdcm.ImageReader imagereader = new gdcm.ImageReader();
imagereader.SetFileName(@"E:\sample_success.dcm");
if (!imagereader.Read()) throw new Exception("Cannot read dicom file!");

For "sample_success.dcm" file, I can read the file just fine (sample_success.png). But using "sample_failed.dcm" file, GDCM throws exception because it couldn't read it. I tried to open the file using other DICOM viewer such as Radiant and it worked. Is there something wrong with my GDCM build? Why it cannot read it?

I use GDCM 2.6.5. Please find both samples here.


Solution

  • You're file contains garbage (bunch of binary 0) after offset 0x1480aa (somewhere in the Pixel Data attribute). What did you expect from a toolkit if not report properly an error ?

    By design GDCM will still load whatever it can until the error. So if you remove your new Exception in your code, you can decide (for example) to pass the imagereader.GetFile() to a gdcm::Writer and rewrite the file as clean DICOM.

    As a side note I do not have access to Radiant software but I find it very odd that it does not indicate an error in that case.

    I've checked with DCMTK and dicom3tools they all report a parsing issue.

    Using the gdcm command line tool you can almost rewrite the file clean using:

    $ gdcmconv -I sample_failed.dcm sample_failed_correct.dcm
    

    Because your input dataset is invalid, GDCM (falsely) believe to see an attribute, you can remove it using:

    $ gdcmanon --dumb --remove 0,0 sample_failed_correct.dcm sample_failed_correct_clean.dcm
    

    And then:

    $ gdcminfo sample_failed_correct.dcm 
    MediaStorage is 1.2.840.10008.5.1.4.1.1.3.1 [Ultrasound Multi-frame Image Storage]
    TransferSyntax is 1.2.840.10008.1.2.4.50 [JPEG Baseline (Process 1): Default Transfer Syntax for Lossy JPEG 8 Bit Image Compression]
    NumberOfDimensions: 3
    Dimensions: (800,600,21)
    SamplesPerPixel    :3
    BitsAllocated      :8
    BitsStored         :8
    HighBit            :7
    PixelRepresentation:0
    ScalarType found   :UINT8
    PhotometricInterpretation: YBR_FULL_422
    PlanarConfiguration: 0
    TransferSyntax: 1.2.840.10008.1.2.4.50
    Origin: (0,0,0)
    Spacing: (0.0106324,0.0106324,1)
    DirectionCosines: (1,0,0,0,1,0)
    Rescale Intercept/Slope: (0,1)
    Orientation Label: AXIAL
    

    Which is valid with the number of Fragments in the Pixel Data:

    $ gdcmdump sample_failed_correct.dcm | grep Item | grep "ff.d8" | wc
         21     126    2856