+
+/**
+ * \brief Copies all the attributes from an other DocEntrySet
+ * @param set entry to copy from
+ * @remarks The contained DocEntries are not copied, only referenced
+ */
+void DicomDir::Copy(DocEntrySet *set)
+{
+ // Remove all previous childs
+ ClearPatient();
+
+ Document::Copy(set);
+
+ DicomDir *dd = dynamic_cast<DicomDir *>(set);
+ if( dd )
+ {
+ if(MetaElems)
+ MetaElems->Unregister();
+ MetaElems = dd->MetaElems;
+ if(MetaElems)
+ MetaElems->Register();
+
+ Patients = dd->Patients;
+ for(ItPatient = Patients.begin();ItPatient != Patients.end();++ItPatient)
+ (*ItPatient)->Register();
+ }
+}
+
+//-----------------------------------------------------------------------------
+// Protected
+/**
+ * \brief create a Document-like chained list from a root Directory
+ * @param path entry point of the tree-like structure
+ */
+void DicomDir::CreateDicomDirChainedList(std::string const &path)
+{
+ CallStartMethod();
+ DirList dirList(path,1); // gets recursively the file list
+ unsigned int count = 0;
+ VectDocument list;
+ File *f;
+
+ DirListType fileList = dirList.GetFilenames();
+ unsigned int nbFile = fileList.size();
+ for( DirListType::iterator it = fileList.begin();
+ it != fileList.end();
+ ++it )
+ {
+ Progress = (float)(count+1)/(float)nbFile;
+ CallProgressMethod();
+ if ( Abort )
+ {
+ break;
+ }
+
+ f = File::New( );
+ f->SetLoadMode(LoadMode); // we allow user not to load Sequences,
+ // or Shadow groups, or ......
+ f->SetFileName( it->c_str() );
+ f->Load( );
+
+ if ( f->IsReadable() )
+ {
+ // Add the file to the chained list:
+ list.push_back(f);
+ gdcmDebugMacro( "Readable " << it->c_str() );
+ }
+ else
+ {
+ f->Delete();
+ }
+ count++;
+ }
+ // sorts Patient/Study/Serie/
+ std::sort(list.begin(), list.end(), DicomDir::HeaderLessThan );
+
+ std::string tmp = dirList.GetDirName();
+ //for each File of the chained list, add/update the Patient/Study/Serie/Image info
+ SetElements(tmp, list);
+ CallEndMethod();
+
+ for(VectDocument::iterator itDoc=list.begin();
+ itDoc!=list.end();
+ ++itDoc)
+ {
+ dynamic_cast<File *>(*itDoc)->Delete();
+ }
+}
+
+
+//-----------------------------------------------------------------------------
+// Private
+/**
+ * \brief Sets all fields to NULL
+ */
+void DicomDir::Initialize()
+{
+ Progress = 0.0;
+ Abort = false;
+
+ MetaElems = NULL;
+}
+
+/**
+ * \brief create a 'gdcm::DicomDir' from a DICOMDIR Header
+ */
+void DicomDir::CreateDicomDir()
+{
+ // The SeqEntries of "Directory Record Sequence" are parsed.
+ // When a DicomDir tag ("PATIENT", "STUDY", "SERIE", "IMAGE") is found :
+ // 1 - we save the beginning iterator
+ // 2 - we continue to parse
+ // 3 - we find an other tag
+ // + we create the object for the precedent tag
+ // + loop to 1 -
+ gdcmDebugMacro("Create DicomDir");
+
+ // Directory record sequence
+ DocEntry *e = GetDocEntry(0x0004, 0x1220);
+ if ( !e )
+ {
+ gdcmWarningMacro( "No Directory Record Sequence (0004,1220) found");
+ return;
+ }
+
+ SeqEntry *s = dynamic_cast<SeqEntry *>(e);
+ if ( !s )
+ {
+ gdcmWarningMacro( "Element (0004,1220) is not a Sequence ?!?");
+ return;
+ }
+
+ NewMeta();
+
+ DocEntry *d;
+ std::string v;
+ SQItem *si;
+
+ SQItem *tmpSI=s->GetFirstSQItem();
+ while(tmpSI)
+ {
+ d = tmpSI->GetDocEntry(0x0004, 0x1430); // Directory Record Type
+ if ( DataEntry *dataEntry = dynamic_cast<DataEntry *>(d) )
+ {
+ v = dataEntry->GetString();
+ }
+ else
+ {
+ gdcmWarningMacro( "(0004,1430) not a DataEntry ?!?");
+ continue;
+ }
+
+ // A decent DICOMDIR has much more images than series,
+ // more series than studies, and so on.
+ // This is the right order to perform the tests
+
+ if ( v == "IMAGE " )
+ {
+ si = DicomDirImage::New(true);
+ if ( !AddImageToEnd( static_cast<DicomDirImage *>(si)) )
+ {
+ si->Delete();
+ si = NULL;
+ gdcmErrorMacro( "Add AddImageToEnd failed");
+ }
+ }
+ else if ( v == "SERIES" )
+ {
+ si = DicomDirSerie::New(true);
+ if ( !AddSerieToEnd( static_cast<DicomDirSerie *>(si)) )
+ {
+ si->Delete();
+ si = NULL;
+ gdcmErrorMacro( "Add AddSerieToEnd failed");
+ }
+ }
+ else if ( v == "VISIT " )
+ {
+ si = DicomDirVisit::New(true);
+ if ( !AddVisitToEnd( static_cast<DicomDirVisit *>(si)) )
+ {
+ si->Delete();
+ si = NULL;
+ gdcmErrorMacro( "Add AddVisitToEnd failed");
+ }
+ }
+ else if ( v == "STUDY " )
+ {
+ si = DicomDirStudy::New(true);
+ if ( !AddStudyToEnd( static_cast<DicomDirStudy *>(si)) )
+ {
+ si->Delete();
+ si = NULL;
+ gdcmErrorMacro( "Add AddStudyToEnd failed");
+ }
+ }
+ else if ( v == "PATIENT " )
+ {
+ si = DicomDirPatient::New(true);
+ if ( !AddPatientToEnd( static_cast<DicomDirPatient *>(si)) )
+ {
+ si->Delete();
+ si = NULL;
+ gdcmErrorMacro( "Add PatientToEnd failed");
+ }
+ }
+ else
+ {
+ // It was neither a 'PATIENT', nor a 'STUDY', nor a 'SERIE',
+ // nor an 'IMAGE' SQItem. Skip to next item.
+ gdcmDebugMacro( " -------------------------------------------"
+ << "a non PATIENT/STUDY/SERIE/IMAGE SQItem was found : "
+ << v);
+
+ // FIXME : deal with other item types !
+ tmpSI=s->GetNextSQItem(); // To avoid infinite loop
+ continue;
+ }
+ if ( si )
+ si->Copy(tmpSI);
+
+ tmpSI=s->GetNextSQItem();
+ }
+ ClearEntry();
+}
+
+/**
+ * \brief AddPatientToEnd
+ * @param dd SQ Item to enqueue to the DicomPatient chained List
+ */
+bool DicomDir::AddPatientToEnd(DicomDirPatient *dd)
+{
+ Patients.push_back(dd);
+ return true;
+}
+
+/**
+ * \brief AddStudyToEnd
+ * @param dd SQ Item to enqueue to the DicomDirStudy chained List
+ */
+bool DicomDir::AddStudyToEnd(DicomDirStudy *dd)
+{
+ if ( Patients.size() > 0 )
+ {
+ ListDicomDirPatient::iterator itp = Patients.end();
+ itp--;
+ (*itp)->AddStudy(dd);
+ return true;
+ }
+ return false;
+}
+
+/**
+ * \brief AddSerieToEnd
+ * @param dd SQ Item to enqueue to the DicomDirSerie chained List
+ */
+bool DicomDir::AddSerieToEnd(DicomDirSerie *dd)
+{
+ if ( Patients.size() > 0 )
+ {
+ ListDicomDirPatient::iterator itp = Patients.end();
+ itp--;
+
+ DicomDirStudy *study = (*itp)->GetLastStudy();
+ if ( study )
+ {
+ study->AddSerie(dd);
+ return true;
+ }
+ }
+ return false;
+}
+
+/**
+ * \brief AddVisitToEnd
+ * @param dd SQ Item to enqueue to the DicomDirVisit chained List
+ */
+bool DicomDir::AddVisitToEnd(DicomDirVisit *dd)
+{
+ if ( Patients.size() > 0 )
+ {
+ ListDicomDirPatient::iterator itp = Patients.end();
+ itp--;
+
+ DicomDirStudy *study = (*itp)->GetLastStudy();
+ if ( study )
+ {
+ study->AddVisit(dd);
+ return true;
+ }
+ }
+ return false;
+}
+/**
+ * \brief AddImageToEnd
+ * @param dd SQ Item to enqueue to the DicomDirImage chained List
+ */
+bool DicomDir::AddImageToEnd(DicomDirImage *dd)
+{
+ if ( Patients.size() > 0 )
+ {
+ ListDicomDirPatient::iterator itp = Patients.end();
+ itp--;
+
+ DicomDirStudy *study = (*itp)->GetLastStudy();
+ if ( study )
+ {
+ DicomDirSerie *serie = study->GetLastSerie();
+ if ( serie )
+ {
+ serie->AddImage(dd);
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+/**
+ * \brief for each Header of the chained list,
+ * add/update the Patient/Study/Serie/Image info
+ * @param path path of the root directory
+ * @param list chained list of Headers
+ */
+void DicomDir::SetElements(std::string const &path, VectDocument const &list)
+{
+ ClearEntry();
+ ClearPatient();
+
+ std::string patPrevName = "", patPrevID = "";
+ std::string studPrevInstanceUID = "", studPrevID = "";
+ std::string serPrevInstanceUID = "", serPrevID = "";
+
+ std::string patCurName, patCurID;
+ std::string studCurInstanceUID, studCurID;
+ std::string serCurInstanceUID, serCurID;
+
+ bool first = true;
+ for( VectDocument::const_iterator it = list.begin();
+ it != list.end();
+ ++it )
+ {
+ // get the current file characteristics
+ patCurName = (*it)->GetEntryString(0x0010,0x0010);
+ patCurID = (*it)->GetEntryString(0x0010,0x0011);
+ studCurInstanceUID = (*it)->GetEntryString(0x0020,0x000d);
+ studCurID = (*it)->GetEntryString(0x0020,0x0010);
+ serCurInstanceUID = (*it)->GetEntryString(0x0020,0x000e);
+ serCurID = (*it)->GetEntryString(0x0020,0x0011);
+
+ if ( patCurName != patPrevName || patCurID != patPrevID || first )
+ {
+ SetElement(path, GDCM_DICOMDIR_PATIENT, *it);
+ first = true;
+ }
+
+ // if new Study, deal with 'STUDY' Elements
+ if ( studCurInstanceUID != studPrevInstanceUID || studCurID != studPrevID
+ || first )
+ {
+ SetElement(path, GDCM_DICOMDIR_STUDY, *it);
+ first = true;
+ }
+
+ // if new Serie, deal with 'SERIE' Elements
+ if ( serCurInstanceUID != serPrevInstanceUID || serCurID != serPrevID
+ || first )
+ {
+ SetElement(path, GDCM_DICOMDIR_SERIE, *it);
+ }
+
+ // Always Deal with 'IMAGE' Elements
+ SetElement(path, GDCM_DICOMDIR_IMAGE, *it);
+
+ patPrevName = patCurName;
+ patPrevID = patCurID;
+ studPrevInstanceUID = studCurInstanceUID;
+ studPrevID = studCurID;
+ serPrevInstanceUID = serCurInstanceUID;
+ serPrevID = serCurID;
+ first = false;
+ }
+}
+
+/**
+ * \brief adds to the HTable
+ * the Entries (Dicom Elements) corresponding to the given type
+ * @param path full path file name (only used when type = GDCM_DICOMDIR_IMAGE
+ * @param type DicomDirObject type to create (GDCM_DICOMDIR_PATIENT,
+ * GDCM_DICOMDIR_STUDY, GDCM_DICOMDIR_SERIE ...)
+ * @param header Header of the current file
+ */
+void DicomDir::SetElement(std::string const &path, DicomDirType type,
+ Document *header)
+{
+ ListDicomDirElem elemList;
+ ListDicomDirElem::const_iterator it;
+ uint16_t tmpGr, tmpEl;
+ //DictEntry *dictEntry;
+ DataEntry *entry;
+ std::string val;
+ SQItem *si;
+
+ switch( type )
+ {
+ case GDCM_DICOMDIR_IMAGE:
+ elemList = Global::GetDicomDirElements()->GetDicomDirImageElements();
+ si = DicomDirImage::New(true);
+ if ( !AddImageToEnd(static_cast<DicomDirImage *>(si)) )
+ {
+ si->Delete();
+ gdcmErrorMacro( "Add ImageToEnd failed");
+ }
+ break;
+ case GDCM_DICOMDIR_SERIE:
+ elemList = Global::GetDicomDirElements()->GetDicomDirSerieElements();
+ si = DicomDirSerie::New(true);
+ if ( !AddSerieToEnd(static_cast<DicomDirSerie *>(si)) )
+ {
+ si->Delete();
+ gdcmErrorMacro( "Add SerieToEnd failed");
+ }
+ break;
+ case GDCM_DICOMDIR_STUDY:
+ elemList = Global::GetDicomDirElements()->GetDicomDirStudyElements();
+ si = DicomDirStudy::New(true);
+ if ( !AddStudyToEnd(static_cast<DicomDirStudy *>(si)) )
+ {
+ si->Delete();
+ gdcmErrorMacro( "Add StudyToEnd failed");
+ }
+ break;
+ case GDCM_DICOMDIR_PATIENT:
+ elemList = Global::GetDicomDirElements()->GetDicomDirPatientElements();
+ si = DicomDirPatient::New(true);
+ if ( !AddPatientToEnd(static_cast<DicomDirPatient *>(si)) )
+ {
+ si->Delete();
+ gdcmErrorMacro( "Add PatientToEnd failed");
+ }
+ break;
+ case GDCM_DICOMDIR_META:
+ if ( MetaElems )
+ {
+ MetaElems->Delete();
+ gdcmErrorMacro( "MetaElements already exist, they will be destroyed");
+ }
+ elemList = Global::GetDicomDirElements()->GetDicomDirMetaElements();
+ MetaElems = DicomDirMeta::New(true);
+ si = MetaElems;
+ break;
+ default:
+ return;
+ }
+
+ // FIXME : troubles found when it's a SeqEntry
+
+ // 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
+
+ std::string referencedVal;
+ // for all the relevant elements found in their own spot of the DicomDir.dic
+ for( it = elemList.begin(); it != elemList.end(); ++it)
+ {
+ tmpGr = it->Group;
+ tmpEl = it->Elem;
+ //dictEntry = GetPubDict()->GetEntry(tmpGr, tmpEl);
+ //entry = DataEntry::New( dictEntry );
+
+ entry = DataEntry::New(tmpGr, tmpEl, GDCM_VRUNKNOWN); /// \todo : modify dicomelements file, to store VR
+ entry->SetOffset(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->GetEntryString(tmpGr, tmpEl);
+ }
+ else
+ {
+ val = GDCM_UNFOUND;
+ }
+
+ if ( val == GDCM_UNFOUND)
+ {
+ if ( tmpGr == 0x0004 ) // never present in File !
+ {
+ switch (tmpEl)
+ {
+ case 0x1130: // File-set ID
+ // force to the *end* File Name
+ val = Util::GetName( path );
+ break;
+
+ case 0x1500: // Only used for image
+ if ( header->GetFileName().substr(0, path.length()) != path )
+ {
+ gdcmWarningMacro( "The base path of file name is incorrect");
+ val = header->GetFileName();
+ }
+ else
+ {
+ // avoid the first '/' in File name !
+ if ( header->GetFileName().c_str()[path.length()]
+ == GDCM_FILESEPARATOR )
+ val = &(header->GetFileName().c_str()[path.length()+1]);
+ else
+ val = &(header->GetFileName().c_str()[path.length()]);
+ }
+ break;
+
+ case 0x1510: // Referenced SOP Class UID in File
+ referencedVal = header->GetEntryString(0x0008, 0x0016);
+ // FIXME : probabely something to check
+ val = referencedVal;
+ break;
+
+ case 0x1511: // Referenced SOP Instance UID in File
+ referencedVal = header->GetEntryString(0x0008, 0x0018);
+ // FIXME : probabely something to check
+ val = referencedVal;
+ break;
+
+ case 0x1512: // Referenced Transfer Syntax UID in File
+ referencedVal = header->GetEntryString(0x0002, 0x0010);
+ // FIXME : probabely something to check
+ val = referencedVal;
+ break;
+
+ default :
+ val = it->Value;
+ }
+ }
+ else
+ {
+ // If the entry is not found in the Header, don't write its 'value' in the DICOMDIR !
+ entry->Delete();
+ continue;
+ }
+ }
+ else
+ {
+ if ( header->GetEntryLength(tmpGr,tmpEl) == 0 )
+ {
+ val = it->Value;
+ // Don't polute the DICOMDIR with empty fields
+ if (val == "")
+ {
+ entry->Delete();
+ continue;
+ }
+ }
+ }
+
+/* FIX later the pb of creating the 'Implementation Version Name'!
+
+ if (val == GDCM_UNFOUND)
+ val = "";
+
+ if ( tmpGr == 0x0002 && tmpEl == 0x0013)
+ {
+ // 'Implementation Version Name'
+ std::string val = "GDCM ";
+ val += Util::GetVersion();
+ }
+*/
+
+ entry->SetString( val ); // troubles expected when vr=SQ ...
+
+ if ( type == GDCM_DICOMDIR_META ) // fusible : should never print !
+ {
+ gdcmDebugMacro("GDCM_DICOMDIR_META ?!? should never print that");
+ }
+
+ si->AddEntry(entry);
+ entry->Delete();
+ }
+}
+
+/**
+ * \brief Move the content of the source SQItem to the destination SQItem
+ * Only DocEntry's are moved
+ * @param dst destination SQItem
+ * @param src source SQItem
+ */
+void DicomDir::MoveSQItem(DocEntrySet *dst, DocEntrySet *src)
+{
+ DocEntry *entry;
+// todo : rewrite the whole stuff, without using RemoveEntry an AddEntry,
+// to save time
+ entry = src->GetFirstEntry();
+ while(entry)
+ {
+ dst->AddEntry(entry); // use it, *before* removing it!
+ src->RemoveEntry(entry);
+ // we destroyed -> the current iterator is not longer valid
+ entry = src->GetFirstEntry();
+ }
+}
+
+/**
+ * \brief compares two files
+ */
+bool DicomDir::HeaderLessThan(Document *header1, Document *header2)
+{
+ return *header1 < *header2;
+}
+
+//-----------------------------------------------------------------------------
+// Print
+/**
+ * \brief Canonical Printer
+ * @param os ostream we want to print in
+ * @param indent Indentation string to be prepended during printing
+ */
+void DicomDir::Print(std::ostream &os, std::string const & )
+{
+ if ( MetaElems )
+ {
+ MetaElems->SetPrintLevel(PrintLevel);
+ MetaElems->Print(os);
+ }
+ for(ListDicomDirPatient::iterator cc = Patients.begin();
+ cc != Patients.end();
+ ++cc)
+ {
+ (*cc)->SetPrintLevel(PrintLevel);
+ (*cc)->Print(os);
+ }
+}
+
+//-----------------------------------------------------------------------------
+} // end namespace gdcm