GDCM 2.2.0

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Directories
- Examples

SimpleScanner.cxx

    /*=========================================================================

      Program: GDCM (Grassroots DICOM). A DICOM library

      Copyright (c) 2006-2011 Mathieu Malaterre
      All rights reserved.
      See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

         This software is distributed WITHOUT ANY WARRANTY; without even
         the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
         PURPOSE.  See the above copyright notice for more information.

    =========================================================================*/
    /*
     * Simple example to show how to use Scanner API.
     * It exposes the three different cases:
     *  - DICOM Attribute is present and has a value
     *  - DICOM Attribute is present and has no value
     *  - DICOM Attribute is not present at all
     * It also shows the purpose of the function 'IsKey' to detect whether or
     * not the file has been read by the gdcm::Scanner. Technically most of the time
     * if a file is not a 'Key' this is because it is not a DICOM file. You need to use
     * gdcm::System::FileExists to decide whether or not the file actually exist on the disk.
     *
     * It was tested on this particular image:
     * ./SimpleScanner gdcmData/012345.002.050.dcm
     */

    #include "gdcmScanner.h"

    int main(int argc, char *argv[])
    {
      if( argc < 2 )
        {
        return 1;
        }
      const char *filename = argv[1];
      const char filename_invalid[] = "this is a file that may not exist on this disk.dcm";

      gdcm::Scanner s;

      const gdcm::Tag tag_array[] = {
        gdcm::Tag(0x8,0x50),
        gdcm::Tag(0x8,0x51),
        gdcm::Tag(0x8,0x60),
      };
      s.AddTag( tag_array[0] );
      s.AddTag( tag_array[1] );
      s.AddTag( tag_array[2] );

      gdcm::Directory::FilenamesType filenames;
      filenames.push_back( filename );
      filenames.push_back( filename_invalid );

      if( !s.Scan( filenames ) )
        {
        return 1;
        }

      //s.Print( std::cout );

      if( s.IsKey( filename ) )
        {
        std::cout << "INFO:" << filename << " is a proper Key for the Scanner (this is a DICOM file)" << std::endl;
        }

      if( !s.IsKey( filename_invalid ) )
        {
        std::cout << "INFO:" << filename_invalid << " is not a proper Key for the Scanner (this is either not a DICOM file or file does not exist)" << std::endl;
        }

      gdcm::Scanner::TagToValue const &ttv = s.GetMapping(filename);

      const gdcm::Tag *ptag = tag_array;
      for( ; ptag != tag_array + 3; ++ptag )
        {
        gdcm::Scanner::TagToValue::const_iterator it = ttv.find( *ptag );
        if( it != ttv.end() )
          {
          std::cout << *ptag << " was properly found in this file" << std::endl;
          // it contains a pair of value. the first one is the actual tag, so the following is always true:
          //  *ptag == it->first
          // The second part is the actual value (stored as RAW strings). You will have to reinterpret this string
          // if VR for *ptag is not VR::VRASCII !
          const char *value = it->second;
          if( *value )
            {
            std::cout << "  It has the value: " << value << std::endl;
            }
          else
            {
            std::cout << "  It has no value (empty)" << std::endl;
            }
          }
        else
          {
          std::cout << "Sorry " << *ptag << " could not be found in this file" << std::endl;
          }
        }

      return 0;
    }