/*========================================================================= 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 "gdcmStrictScanner.h" #include "gdcmSimpleSubjectWatcher.h" #include "gdcmFileNameEvent.h" class MyFileWatcher : public gdcm::SimpleSubjectWatcher { public: MyFileWatcher(gdcm::Subject * s, const char *comment = ""): gdcm::SimpleSubjectWatcher(s,comment){} void ShowFileName(gdcm::Subject *, const gdcm::Event &evt) override { const gdcm::FileNameEvent &pe = dynamic_cast(evt); const char *fn = pe.GetFileName(); std::cout << "FileName: " << fn << " FileSize: " << gdcm::System::FileSize( fn ) << std::endl; } }; 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::SmartPointer sp = new gdcm::StrictScanner; gdcm::StrictScanner &s = *sp; //gdcm::SimpleSubjectWatcher w(&s, "TestFileName" ); MyFileWatcher w(&s, "TestFileName" ); const gdcm::Tag tag_array[] = { gdcm::Tag(0x8,0x50), gdcm::Tag(0x8,0x51), gdcm::Tag(0x8,0x60), gdcm::Tag(0x8,0x80), }; s.AddTag( tag_array[0] ); s.AddTag( tag_array[1] ); s.AddTag( tag_array[2] ); s.AddTag( tag_array[3] ); gdcm::Directory::FilenamesType filenames; filenames.push_back( filename ); filenames.push_back( filename_invalid ); if( !s.Scan( filenames ) ) { return 1; } //s.Print( std::cout ); for(gdcm::Directory::FilenamesType::const_iterator it = filenames.begin(); it != filenames.end(); ++it ) { if( s.IsKey( it->c_str() ) ) { std::cout << "INFO:" << it->c_str() << " is a proper Key for the Scanner (this is a DICOM file)" << std::endl; } else { std::cout << "INFO:" << it->c_str() << " is not a proper Key for the Scanner (this is either not a DICOM file or file does not exist)" << std::endl; } } gdcm::StrictScanner::TagToValue const &ttv = s.GetMapping(filename); const gdcm::Tag *ptag = tag_array; for( ; ptag != tag_array + 3; ++ptag ) { gdcm::StrictScanner::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; }