+/**
+ * \ingroup gdcmDicomDir
+ * \brief create a gdcmDocument-like chained list from a root Directory
+ * @param path entry point of the tree-like structure
+ */
+void gdcmDicomDir::CreateDicomDirChainedList(std::string const & path)
+{
+ CallStartMethod();
+ gdcmDirList fileList(path,1); // gets recursively the file list
+ unsigned int count = 0;
+ VectDocument list;
+ gdcmHeader *header;
+
+ TagHT.clear();
+ patients.clear();
+
+ for( gdcmDirList::iterator it = fileList.begin();
+ it != fileList.end();
+ ++it )
+ {
+ progress = (float)(count+1)/(float)fileList.size();
+ CallProgressMethod();
+ if( abort )
+ {
+ break;
+ }
+
+ header = new gdcmHeader( it->c_str() );
+ if( !header )
+ {
+ dbg.Verbose( 1,
+ "gdcmDicomDir::CreateDicomDirChainedList: "
+ "failure in new Header ",
+ it->c_str() );
+ }
+
+ if( header->IsReadable() )
+ {
+ // Add the file header to the chained list:
+ list.push_back(header);
+ dbg.Verbose( 1,
+ "gdcmDicomDir::CreateDicomDirChainedList: readable ",
+ it->c_str() );
+
+ }
+ else
+ {
+ delete header;
+ }
+ count++;
+ }
+ // sorts Patient/Study/Serie/
+ std::sort(list.begin(), list.end(), gdcmDicomDir::HeaderLessThan );
+
+ std::string tmp = fileList.GetDirName();
+ //for each Header of the chained list, add/update the Patient/Study/Serie/Image info
+ SetElements(tmp, list);
+ CallEndMethod();
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief adds *the* Meta to a partially created DICOMDIR
+ */
+
+gdcmDicomDirMeta * gdcmDicomDir::NewMeta()
+{
+ gdcmDicomDirMeta *m = new gdcmDicomDirMeta( &TagHT );
+
+ if ( TagHT.begin() != TagHT.end() ) // after Document Parsing
+ {
+ TagDocEntryHT::iterator lastOneButSequence = TagHT.end();
+ lastOneButSequence --;
+ // ALL the 'out of Sequence' Tags belong to Meta Elems
+ // (we skip 0004|1220 [Directory record sequence] )
+ for ( TagDocEntryHT::iterator cc = TagHT.begin();
+ cc != lastOneButSequence;
+ ++cc)
+ {
+ m->AddDocEntry( cc->second );
+ }
+ }
+ else // after root directory parsing
+ {
+ std::list<gdcmElement> elemList;
+ elemList=gdcmGlobal::GetDicomDirElements()->GetDicomDirMetaElements();
+ m->FillObject(elemList);
+ }
+ m->SetSQItemNumber(0); // To avoid further missprinting
+ return m;
+}
+
+/**
+ * \brief adds a new Patient (with the basic elements) to a partially created DICOMDIR
+ */
+gdcmDicomDirPatient * gdcmDicomDir::NewPatient()
+{
+ std::list<gdcmElement>::iterator it;
+ uint16_t tmpGr,tmpEl;
+ gdcmDictEntry *dictEntry;
+ gdcmValEntry *entry;
+
+ std::list<gdcmElement> elemList;
+ elemList=gdcmGlobal::GetDicomDirElements()->GetDicomDirPatientElements();
+ gdcmSQItem *s = new gdcmSQItem(0);
+
+ // for all the DicomDirPatient Elements
+ for( it = elemList.begin(); it != elemList.end(); ++it )
+ {
+ tmpGr = it->group;
+ tmpEl = it->elem;
+ dictEntry = GetPubDict()->GetDictEntryByNumber(tmpGr, tmpEl);
+ entry = new gdcmValEntry( dictEntry );
+ entry->SetOffset(0); // just to avoid further missprinting
+ entry->SetValue( it->value );
+
+ // dealing with value length ...
+
+ if( dictEntry->GetGroup() == 0xfffe)
+ {
+ entry->SetLength(entry->GetValue().length());
+ }
+ else if( dictEntry->GetVR() == "UL" || dictEntry->GetVR() == "SL" )
+ {
+ entry->SetLength( 4 );
+ }
+ else if( dictEntry->GetVR() == "US" || dictEntry->GetVR() == "SS" )
+ {
+ entry->SetLength(2);
+ }
+ else if( dictEntry->GetVR() == "SQ" )
+ {
+ entry->SetLength( 0xffffffff );
+ }
+ else
+ {
+ entry->SetLength( entry->GetValue().length() );
+ }
+ s->AddDocEntry( entry );
+ }
+
+ gdcmDicomDirPatient *p = new gdcmDicomDirPatient(s, &TagHT);
+ patients.push_front( p );
+
+ return p;
+}
+
+/**
+ * \brief adds to the HTable
+ * the gdcmEntries (Dicom Elements) corresponding to the given type
+ * @param path full path file name (only used when type = GDCM_DICOMDIR_IMAGE
+ * @param type gdcmDicomDirObject type to create (GDCM_DICOMDIR_PATIENT,
+ * GDCM_DICOMDIR_STUDY, GDCM_DICOMDIR_SERIE ...)
+ * @param header gdcmHeader of the current file
+ */
+void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,
+ gdcmDocument *header)
+{
+ std::list<gdcmElement> elemList;
+ std::list<gdcmElement>::iterator it;
+ uint16_t tmpGr, tmpEl;
+ gdcmDictEntry *dictEntry;
+ gdcmValEntry *entry;
+ std::string val;
+ gdcmSQItem *si = new gdcmSQItem(0); // all the items will be at level 1
+ switch( type )
+ {
+ case GDCM_DICOMDIR_IMAGE:
+ elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirImageElements();
+ break;
+
+ case GDCM_DICOMDIR_SERIE:
+ elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirSerieElements();
+ break;
+
+ case GDCM_DICOMDIR_STUDY:
+ elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirStudyElements();
+ break;
+
+ case GDCM_DICOMDIR_PATIENT:
+ elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirPatientElements();
+ break;
+
+ case GDCM_DICOMDIR_META:
+ elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirMetaElements();
+ break;
+
+ default:
+ return;
+ }
+ // removed all the seems-to-be-useless stuff about Referenced Image Sequence
+ // to avoid further troubles
+ // imageElem 0008 1140 "" // Referenced Image Sequence
+ // imageElem fffe e000 "" // 'no length' item : length to be set to 0xffffffff later
+ // imageElem 0008 1150 "" // Referenced SOP Class UID : to be set/forged later
+ // imageElem 0008 1155 "" // Referenced SOP Instance UID : to be set/forged later
+ // imageElem fffe e00d "" // Item delimitation : length to be set to ZERO later
+ // for all the relevant elements found in their own spot of the DicomDir.dic
+ // FIXME : troubles found when it's a SeqEntry
+
+ for( it = elemList.begin(); it != elemList.end(); ++it)
+ {
+ tmpGr = it->group;
+ tmpEl = it->elem;
+ dictEntry = GetPubDict()->GetDictEntryByNumber(tmpGr, tmpEl);
+
+ entry = new gdcmValEntry( dictEntry ); // Be sure it's never a BinEntry !
+
+ entry->SetOffset(0); // just to avoid further missprinting
+ entry->SetLength(0); // just to avoid further missprinting
+
+ if( header ) // NULL when we Build Up (ex nihilo) a DICOMDIR
+ // or when we add the META elems
+ val = header->GetEntryByNumber(tmpGr, tmpEl);
+ else
+ val = GDCM_UNFOUND;
+
+ if( val == GDCM_UNFOUND)
+ {
+ if( tmpGr == 0x0004 && tmpEl == 0x1130 ) // File-set ID
+ {
+ // force to the *end* File Name
+ val = GetName( path );
+ }
+ else if( tmpGr == 0x0004 && tmpEl == 0x1500 ) // Only used for image
+ {
+ if( header->GetFileName().substr(0, path.length()) != path )
+ {
+ dbg.Verbose(0, "gdcmDicomDir::SetElement : the base path"
+ " of file name is incorrect");
+ val = header->GetFileName();
+ }
+ else
+ val = &(header->GetFileName().c_str()[path.length()]);
+ }
+ else
+ val = it->value;
+ }
+ else
+ {
+ if ( header->GetEntryLengthByNumber(tmpGr,tmpEl) == 0 )
+ val = it->value;
+ }
+
+ // GDCM_UNFOUND or not !
+
+ entry->SetValue( val ); // troubles expected when vr=SQ ...
+
+ if( dictEntry )
+ {
+ if( dictEntry->GetGroup() == 0xfffe )
+ {
+ entry->SetLength( entry->GetValue().length() ); // FIXME
+ }
+ else if( dictEntry->GetVR() == "UL" || dictEntry->GetVR() == "SL" )
+ {
+ entry->SetLength(4);
+ }
+ else if( dictEntry->GetVR() == "US" || dictEntry->GetVR() == "SS" )
+ {
+ entry->SetLength(2);
+ }
+ else if( dictEntry->GetVR() == "SQ" )
+ {
+ entry->SetLength( 0xffffffff );
+ }
+ else
+ {
+ entry->SetLength( entry->GetValue().length() );
+ }
+ }
+
+ if ( type == GDCM_DICOMDIR_META ) // fusible : should never print !
+ {
+ std::cout << "GDCM_DICOMDIR_META ?!? should never print that"
+ << std::endl;
+ }
+ si->AddEntry(entry);
+ }
+ switch( type )
+ {
+ case GDCM_DICOMDIR_IMAGE:
+ AddDicomDirImageToEnd(si);
+ break;
+
+ case GDCM_DICOMDIR_SERIE:
+ AddDicomDirSerieToEnd(si);
+ break;
+
+ case GDCM_DICOMDIR_STUDY:
+ AddDicomDirStudyToEnd(si);
+ break;
+
+ case GDCM_DICOMDIR_PATIENT:
+ AddDicomDirPatientToEnd(si);
+ break;
+
+ default:
+ return;
+ }
+ //int count=1; // find a trick to increment
+ //s->AddEntry(si, count); // Seg Faults
+
+}
+
+/**
+ * \brief CallStartMethod
+ */
+void gdcmDicomDir::CallStartMethod()
+{
+ progress = 0.0f;
+ abort = false;
+ if( startMethod )
+ {
+ startMethod( startArg );
+ }
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief CallProgressMethod
+ */
+void gdcmDicomDir::CallProgressMethod()
+{
+ if( progressMethod )
+ {
+ progressMethod( progressArg );
+ }
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief CallEndMethod
+ */
+void gdcmDicomDir::CallEndMethod()
+{
+ progress = 1.0f;
+ if( endMethod )
+ {
+ endMethod( endArg );
+ }
+}
+