5
votes

I have to pick all valid DICOM Files from folder. I can recursively pick all the files from the folder which have *.DCM extension. But any file with *.DCM also picked up and such file is not valid DICOM File.

What is best way.

I thought of reading few byte of the file and validating.

Or

Any Other Method or any other EXEs we have which validates.

Thank you, Harsha

Edit: Solution for the problem: I finally used the dcmftest.exe for verification. Hope I am on right track. -Harsha

6

6 Answers

4
votes

Validating a DICOM file is not as easy task considering the different mandatory and optional tags present in different IODs. I think it is better to use an existing solution to do this. You can take a look at DCMCHECK from DCMTK to do this.

11
votes

You want to recognize DICOM files, not to validate. There is big difference. Validation means (at least!) that all the tags required for its SOP class are present.

Recognition is easy, as the DICOM file has to contain text DICM at the offset 0x80, so that tags start at the offset 0x84 of file.

Note that sometimes only the serialized dataset is stored (starting with tag group 8 at file offset 0), and these are more difficult to recognize, but are not standard.

EDIT: As an example, consider a RAR archive. It's easy to recognize, because it starts with Rar!. However, to be sure that it's a valid RAR archive, you have to decompress all the files and check their CRCs, and this is something that could be done only by RAR itself (and it's slow).

6
votes

I know this has been answered already but I had a similar requirement, so I whipped up some extension methods to do exactly that. Works on Files, FileStreams, MemoryStreams and generic Streams. Only reads the specific 4 bytes needed to validate the filetype. Extremely efficient, I was able to run through thousands of files within seconds.

C#

public static class Dicom
{
    public static bool IsDicomFile(this Stream s)
    {
        //Create an empty 4 byte array
        byte[] dba = new byte[4];

        //Seek to 0x80
        s.Seek(128, SeekOrigin.Begin);

        //Read the following 4 dba
        s.Read(dba, 0, 4);

        //Compare to 'DICM'
        return dba.SequenceEqual(new byte[4] {68, 73, 67, 77});
    }

    public static bool IsDicomFile(this MemoryStream ms)
    {
        return ((Stream)ms).IsDicomFile();
    }

    public static bool IsDicomFile(this FileStream fs)
    {
        return ((Stream)fs).IsDicomFile();
    }

    public static bool IsDicomFile(this FileInfo fi)
    {
        return fi.OpenRead().IsDicomFile();
    }
}

VB.NET

<Extension()> _
Public Function IsDicomFile(ByVal s As Stream) As Boolean
    'Create an empty 4 byte array
    Dim dba() As Byte = New Byte(3) {}

    'Seek to 0x80
    s.Seek(128, SeekOrigin.Begin)

    'Read the subsequent 4 bytes
    s.Read(dba, 0, 4)

    'Compare to 'DICM'
    Return dba.SequenceEqual(New Byte(3) {68, 73, 67, 77})
End Function

<Extension()> _
Public Function IsDicomFile(ByVal ms As MemoryStream) As Boolean
    Return DirectCast(ms, Stream).IsDicomFile
End Function

<Extension()> _
Public Function IsDicomFile(ByVal fs As FileStream) As Boolean
    Return DirectCast(fs, Stream).IsDicomFile
End Function

<Extension()> _
Public Function IsDicomFile(ByVal fi As FileInfo) As Boolean
    Return fi.OpenRead().IsDicomFile
End Function
5
votes

FYI, files with a .dcm extension are not really legitimate DICOM, although for legacy reasons it's a good idea to write your programs to accept them anyway (but you shouldn't put 3 character filename extensions on DICOM files exported by your application). According to the part of the DICOM standard regarding media exchange, "The ISO 9660 File Name Extension shall not be used." Furthermore, no semantics should be inferred from filenames or directory structure except for the special DICOMDIR file described in parts 10 and 12 of the standard.

ruslik's answer gives you the correct way to recognize DICOM files. If it has DICM in the designated location in the file preamble, then it's a DICOM file. Otherwise, it's not.

2
votes
For Java User
dcm4che-tool-dcmvalidate
usage: dcmvalidate --iod  [..][..]
Utility to validate DICOM objects according a specified Information Object
Definition.
-
Options:
 -h,--help             display this help and exit
    --iod    path to xml file with Information Object Definition
 -V,--version          output version information and exit
Example:
$ dcmvalidate --iod etc/dcmvalidate/dicomdir-iod.xml DICOMDIR

Validate DICOMDIR against IOD specified in etc/dcmvalidate/dicomdir.xml

Click Here for more Reference
2
votes

Be aware : checking the preamble for "DICM" will only check to see if the file is a DICOM v3 file.

Previous versions of DICOM did not have the preamble. 100% valid DICOM files that can be viewed, with all the requisite DICOM tags etc, and are importable into a DICOM node, don't have the preamble.

I'm checking with OFFIS to see if the licensed version of DCMCHECK also has this constraint or not, but I've not heard back from them yet.