Determining if a file is a Dicom file

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

Determining if a file is a Dicom file

#1 Post by martinrame »

Hi, I need to read a directory and determine if a file is Dicom, what is the fastest way to do this.

Currently I'm using DcmFileFormat to open each file, but this is too slow in some cases.

Thanks in advance.

omarelgazzar
Posts: 101
Joined: Wed, 2009-07-08, 16:06
Location: Oldenburg, Germany

#2 Post by omarelgazzar »

In the dcmtk tool dcmftest, you can find a code in the main function that checks whether a DICOM file uses DICOM part 10. It checks the meta header of the file for the File Preamble and for the DICOM prefix ("DICM") so you don't need to load the file using DcmFileFormat.

martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

#3 Post by martinrame »

Thanks omarelgazzar, here's the function with a slight modification:

Code: Select all

  bool fileIsDicom(char * fname)
  {
    bool ok = false;
    FILE* f = NULL;

    f = fopen(fname, "rb");
    if (f == 0) {
        ok = false;
    } else {
        char signature[4];
        if ((fseek(f, DCM_PreambleLen, SEEK_SET) < 0) ||
            (fread(signature, 1, DCM_MagicLen, f) != DCM_MagicLen)) {
            ok = false;
        } else if (strncmp(signature, DCM_Magic, DCM_MagicLen) != 0) {
            ok = false;
        } else {
            /* looks ok */
            ok = true; 
        }
        fclose(f);
    }
    return ok;
  }

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot] and 1 guest