]> Creatis software - gdcm.git/blobdiff - src/gdcmDicomDir.cxx
* Change the DocEntry inheritance to RefCounter
[gdcm.git] / src / gdcmDicomDir.cxx
index a67ed04bbf9762a84894203b46983a7423ba5cc2..f002e6f7ca0f174888cb41ba9c165f2ec073916d 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmDicomDir.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/06/24 10:55:58 $
-  Version:   $Revision: 1.142 $
+  Date:      $Date: 2005/10/24 16:00:47 $
+  Version:   $Revision: 1.165 $
   
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 //  PS 3.3-2003, pages 731-750
 //-----------------------------------------------------------------------------
 #include "gdcmDicomDir.h"
+#include "gdcmDicomDirObject.h"
 #include "gdcmDicomDirStudy.h"
 #include "gdcmDicomDirSerie.h"
+#include "gdcmDicomDirVisit.h"
 #include "gdcmDicomDirImage.h"
 #include "gdcmDicomDirPatient.h"
 #include "gdcmDicomDirMeta.h"
@@ -34,7 +36,7 @@
 #include "gdcmFile.h"
 #include "gdcmSeqEntry.h"
 #include "gdcmSQItem.h"
-#include "gdcmValEntry.h"
+#include "gdcmDataEntry.h"
 
 #include <fstream>
 #include <string>
 //
 // (Root directory Entity)     PATIENT
 // PATIENT                     STUDY
-// STUDY                       SERIES 
+// STUDY                       SERIES
+// STUDY                       VISIT 
 // SERIES                      IMAGE
 // IMAGE                       /
 //
@@ -117,20 +120,24 @@ DicomDir::DicomDir()
          :Document( )
 {
    Initialize();  // sets all private fields to NULL
+   ParseDir = false;
    NewMeta();
 }
 
-/**
- * \brief Constructor Parses recursively the directory and creates the DicomDir
+#ifndef GDCM_LEGACY_REMOVE
+/* *
+ * \ brief Constructor Parses recursively the directory and creates the DicomDir
  *        or uses an already built DICOMDIR, depending on 'parseDir' value.
- * @param fileName  name 
+ * @ param fileName  name 
  *                      - of the root directory (parseDir = true)
  *                      - of the DICOMDIR       (parseDir = false)
- * @param parseDir boolean
+ * @ param parseDir boolean
  *                      - true if user passed an entry point 
  *                        and wants to explore recursively the directories
  *                      - false if user passed an already built DICOMDIR file
  *                        and wants to use it 
+ * @ deprecated use : new DicomDir() + [ SetLoadMode(lm) + ] SetDirectoryName(name)
+ *              or : new DicomDir() + SetFileName(name)
  */
 DicomDir::DicomDir(std::string const &fileName, bool parseDir ):
    Document( )
@@ -141,18 +148,20 @@ DicomDir::DicomDir(std::string const &fileName, bool parseDir ):
    // (nothing is cheked in Document constructor, to avoid overhead)
 
    ParseDir = parseDir;
-   SetLoadMode (0x00000000); // concerns only dicom files
-   Load( fileName );
+   SetLoadMode (LD_ALL); // concerns only dicom files
+   SetFileName( fileName );
+   Load( );
 }
+#endif
 
 /**
  * \brief  Canonical destructor 
  */
 DicomDir::~DicomDir() 
 {
-   SetStartMethod(NULL);
-   SetProgressMethod(NULL);
-   SetEndMethod(NULL);
+   SetStartMethod(NULL,NULL,NULL);
+   SetProgressMethod(NULL,NULL,NULL);
+   SetEndMethod(NULL,NULL,NULL);
 
    ClearPatient();
    if ( MetaElems )
@@ -164,26 +173,71 @@ DicomDir::~DicomDir()
 //-----------------------------------------------------------------------------
 // Public
 
+/**
+ * \brief   Loader. use SetFileName(fn) 
+ *                  or SetLoadMode(lm) + SetDirectoryName(dn)  before !  
+ * @return false if file cannot be open or no swap info was found,
+ *         or no tag was found.
+ */
+bool DicomDir::Load( ) 
+{
+   if (!ParseDir)
+   {
+      if ( ! this->Document::Load( ) )
+         return false;
+   }
+   return DoTheLoadingJob( );   
+}
+
+#ifndef GDCM_LEGACY_REMOVE
+/* *
+ * \ brief   Loader. (DEPRECATED : kept not to break the API)
+ * @ param   fileName file to be open for parsing
+ * @ return false if file cannot be open or no swap info was found,
+ *         or no tag was found.
+ * @ deprecated use SetFileName(n) + Load() instead
+ */
 bool DicomDir::Load(std::string const &fileName ) 
 {
-   Filename = fileName;
-   // We should clean out anything that already exists.
+   SetFileName(fileName);
+   return Load();
+}
 
-   Initialize();  // sets all private fields to NULL
+/// DEPRECATED : use SetDirectoryName(dname) instead
+/* *
+ * \brief   Loader. (DEPRECATED : kept not to break the API)
+ * @param   paseDir Parse Dir
+ * @deprecated use SetDirectoryName(dname) instead
+ */
+void DicomDir::SetParseDir(bool parseDir)
+{
+   ParseDir = parseDir;
+}
+#endif
+
+/**
+ * \brief   Does the Loading Job (internal use only)
+ * @return false if file cannot be open or no swap info was found,
+ *         or no tag was found.
+ */
+bool DicomDir::DoTheLoadingJob( ) 
+{
+   Progress = 0.0f;
+   Abort = false;
 
    if (!ParseDir)
    {
    // Only if user passed a DICOMDIR
    // ------------------------------
       Fp = 0;
-      if ( !OpenFile() )
+      if (!Document::Load() )
       {
          return false;
       }
-      Document::Load(fileName);
+
       if ( GetFirstEntry() == 0 ) // when user passed a Directory to parse
       {
-         gdcmWarningMacro( "Entry HT empty for file: "<< fileName);
+         gdcmWarningMacro( "Entry HT empty for file: "<< GetFileName());
          return false;
       }
       // Directory record sequence
@@ -191,7 +245,7 @@ bool DicomDir::Load(std::string const &fileName )
       if ( !e )
       {
          gdcmWarningMacro( "NO 'Directory record sequence' (0x0004,0x1220)"
-                          << " in file " << fileName);
+                          << " in file " << GetFileName());
          return false;
       }
       else
@@ -201,7 +255,7 @@ bool DicomDir::Load(std::string const &fileName )
    {
    // Only if user passed a root directory
    // ------------------------------------
-      if ( fileName == "." )
+      if ( GetFileName() == "." )
       {
          // user passed '.' as Name
          // we get current directory name
@@ -210,8 +264,8 @@ bool DicomDir::Load(std::string const &fileName )
          SetFileName( dummy ); // will be converted into a string
       }
       NewMeta();
-      gdcmWarningMacro( "Parse directory and create the DicomDir : " << Filename );
-
+      gdcmWarningMacro( "Parse directory and create the DicomDir : " 
+                         << GetFileName() );
       ParseDirectory();
    }
    return true;
@@ -265,7 +319,7 @@ DicomDirMeta *DicomDir::NewMeta()
          if ( dynamic_cast<SeqEntry *>(entry) )
             break;
 
-         RemoveEntryNoDestroy(entry);
+         RemoveEntry(entry);
          MetaElems->AddEntry(entry);
 
          entry = GetFirstEntry();
@@ -340,6 +394,21 @@ void DicomDir::ParseDirectory()
    CreateDicomDir();
 }
 
+void DicomDir::SetStartMethod( DicomDir::Method *method, void *arg )
+{
+   SetStartMethod(method,arg,NULL);
+}
+
+void DicomDir::SetProgressMethod( DicomDir::Method *method, void *arg )
+{
+   SetProgressMethod(method,arg,NULL);
+}
+
+void DicomDir::SetEndMethod( DicomDir::Method *method, void *arg )
+{
+   SetEndMethod(method,arg,NULL);
+}
+
 /**
  * \brief   Set the start method to call when the parsing of the
  *          directory starts.
@@ -444,7 +513,7 @@ void DicomDir::SetEndMethodArgDelete( DicomDir::Method *method )
  * @return false only when fail to open
  */
  
-bool DicomDir::WriteDicomDir(std::string const &fileName) 
+bool DicomDir::Write(std::string const &fileName) 
 {  
    int i;
    uint16_t sq[4] = { 0x0004, 0x1220, 0xffff, 0xffff };
@@ -496,9 +565,9 @@ bool DicomDir::WriteDicomDir(std::string const &fileName)
  * @return true 
  */
  
-bool DicomDir::AnonymizeDicomDir() 
+bool DicomDir::Anonymize() 
 {
-   ValEntry *v;
+   DataEntry *v;
    // Something clever to be found to forge the Patient names
    std::ostringstream s;
    int i = 1;
@@ -507,22 +576,22 @@ bool DicomDir::AnonymizeDicomDir()
                                    ++cc)
    {
       s << i;
-      v = (*cc)->GetValEntry(0x0010, 0x0010) ; // Patient's Name
+      v = (*cc)->GetDataEntry(0x0010, 0x0010) ; // Patient's Name
       if (v)
       {
-         v->SetValue(s.str());
+         v->SetString(s.str());
       }
 
-      v = (*cc)->GetValEntry(0x0010, 0x0020) ; // Patient ID
+      v = (*cc)->GetDataEntry(0x0010, 0x0020) ; // Patient ID
       if (v)
       {
-         v->SetValue(" ");
+         v->SetString(" ");
       }
 
-      v = (*cc)->GetValEntry(0x0010, 0x0030) ; // Patient's BirthDate
+      v = (*cc)->GetDataEntry(0x0010, 0x0030) ; // Patient's BirthDate
       if (v)
       {
-         v->SetValue(" ");
+         v->SetString(" ");
       }
       s << "";
       i++;
@@ -557,9 +626,11 @@ void DicomDir::CreateDicomDirChainedList(std::string const &path)
          break;
       }
 
-   f = new File( );
-   f->SetLoadMode(LoadMode); // we allow user not to load Sequences...
-   f->Load( it->c_str() );
+      f = new File( );
+      f->SetLoadMode(LoadMode); // we allow user not to load Sequences, or Shadow
+                              //             groups, or ......
+      f->SetFileName( it->c_str() );
+   /*int res = */f->Load( );
 
 //     if ( !f )
 //     {
@@ -666,6 +737,7 @@ void DicomDir::CreateDicomDir()
    //  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);
@@ -692,64 +764,84 @@ void DicomDir::CreateDicomDir()
    while(tmpSI)
    {
       d = tmpSI->GetDocEntry(0x0004, 0x1430); // Directory Record Type
-      if ( ValEntry* valEntry = dynamic_cast<ValEntry *>(d) )
+      if ( DataEntry *dataEntry = dynamic_cast<DataEntry *>(d) )
       {
-         v = valEntry->GetValue();
+         v = dataEntry->GetString();
       }
       else
       {
-         gdcmWarningMacro( "(0004,1430) not a ValEntry ?!?");
+         gdcmWarningMacro( "(0004,1430) not a DataEntry ?!?");
          continue;
       }
 
-      if ( v == "PATIENT " )
+      // A decent DICOMDIR has much more images than series,
+      // more series than studies, and so on.
+      // This is the right order to preform the tests
+
+      if ( v == "IMAGE " ) 
       {
-         si = new DicomDirPatient(true);
-         if ( !AddPatientToEnd( static_cast<DicomDirPatient *>(si)) )
+         si = new DicomDirImage(true);
+         if ( !AddImageToEnd( static_cast<DicomDirImage *>(si)) )
          {
             delete si;
             si = NULL;
-            gdcmErrorMacro( "Add PatientToEnd failed");
+            gdcmErrorMacro( "Add AddImageToEnd failed");
          }
       }
-      else if ( v == "STUDY " )
+      else if ( v == "SERIES" )
       {
-         si = new DicomDirStudy(true);
-         if ( !AddStudyToEnd( static_cast<DicomDirStudy *>(si)) )
+         si = new DicomDirSerie(true);
+         if ( !AddSerieToEnd( static_cast<DicomDirSerie *>(si)) )
          {
             delete si;
             si = NULL;
-            gdcmErrorMacro( "Add AddStudyToEnd failed");
+            gdcmErrorMacro( "Add AddSerieToEnd failed");
          }
       }
-      else if ( v == "SERIES" )
+      else if ( v == "VISIT " )
       {
-         si = new DicomDirSerie(true);
-         if ( !AddSerieToEnd( static_cast<DicomDirSerie *>(si)) )
+         si = new DicomDirVisit(true);
+         if ( !AddVisitToEnd( static_cast<DicomDirVisit *>(si)) )
          {
             delete si;
             si = NULL;
-            gdcmErrorMacro( "Add AddSerieToEnd failed");
+            gdcmErrorMacro( "Add AddVisitToEnd failed");
          }
       }
-      else if ( v == "IMAGE " ) 
+      else if ( v == "STUDY " )
       {
-         si = new DicomDirImage(true);
-         if ( !AddImageToEnd( static_cast<DicomDirImage *>(si)) )
+         si = new DicomDirStudy(true);
+         if ( !AddStudyToEnd( static_cast<DicomDirStudy *>(si)) )
          {
             delete si;
             si = NULL;
-            gdcmErrorMacro( "Add AddImageToEnd failed");
+            gdcmErrorMacro( "Add AddStudyToEnd failed");
+         }
+      }
+      else if ( v == "PATIENT " )
+      {
+         si = new DicomDirPatient(true);
+         if ( !AddPatientToEnd( static_cast<DicomDirPatient *>(si)) )
+         {
+            delete si;
+            si = NULL;
+            gdcmErrorMacro( "Add PatientToEnd failed");
          }
       }
       else
       {
-         // It was not a 'PATIENT', nor a 'STUDY', nor a 'SERIE',
-         // neither an 'IMAGE' SQItem. Skip to next item.
-         continue;
+         // It was neither a 'PATIENT', nor a 'STUDY', nor a 'SERIE',
+         // nor an 'IMAGE' SQItem. Skip to next item.
+         gdcmWarningMacro( " -------------------------------------------"
+         << "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 )
-         MoveSQItem(si,tmpSI);
+         si->MoveObject(tmpSI);  // New code : Copies the List
 
       tmpSI=s->GetNextSQItem();
    }
@@ -803,6 +895,26 @@ bool DicomDir::AddSerieToEnd(DicomDirSerie *dd)
    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
@@ -853,12 +965,12 @@ void DicomDir::SetElements(std::string const &path, VectDocument const &list)
                                    ++it )
    {
       // get the current file characteristics
-      patCurName         = (*it)->GetEntryValue(0x0010,0x0010);
-      patCurID           = (*it)->GetEntryValue(0x0010,0x0011);
-      studCurInstanceUID = (*it)->GetEntryValue(0x0020,0x000d);
-      studCurID          = (*it)->GetEntryValue(0x0020,0x0010);
-      serCurInstanceUID  = (*it)->GetEntryValue(0x0020,0x000e);
-      serCurID           = (*it)->GetEntryValue(0x0020,0x0011);
+      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 )
       {
@@ -866,7 +978,7 @@ void DicomDir::SetElements(std::string const &path, VectDocument const &list)
          first = true;
       }
 
-      // if new Study Deal with 'STUDY' Elements   
+      // if new Study, deal with 'STUDY' Elements   
       if ( studCurInstanceUID != studPrevInstanceUID || studCurID != studPrevID 
          || first )
       {
@@ -874,7 +986,7 @@ void DicomDir::SetElements(std::string const &path, VectDocument const &list)
          first = true;
       }
 
-      // if new Serie Deal with 'SERIE' Elements   
+      // if new Serie, deal with 'SERIE' Elements   
       if ( serCurInstanceUID != serPrevInstanceUID || serCurID != serPrevID
          || first )
       {
@@ -909,7 +1021,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
    ListDicomDirElem::const_iterator it;
    uint16_t tmpGr, tmpEl;
    DictEntry *dictEntry;
-   ValEntry *entry;
+   DataEntry *entry;
    std::string val;
    SQItem *si;
 
@@ -952,14 +1064,14 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
          }
          break;
       case GDCM_DICOMDIR_META:
-         elemList = Global::GetDicomDirElements()->GetDicomDirMetaElements();
-         si = new DicomDirMeta(true);
          if ( MetaElems )
          {
             delete MetaElems;
             gdcmErrorMacro( "MetaElements already exist, they will be destroyed");
          }
-         MetaElems = static_cast<DicomDirMeta *>(si);
+         elemList = Global::GetDicomDirElements()->GetDicomDirMetaElements();
+         MetaElems = new DicomDirMeta(true);
+         si = MetaElems;
          break;
       default:
          return;
@@ -981,7 +1093,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
       tmpEl     = it->Elem;
       dictEntry = GetPubDict()->GetEntry(tmpGr, tmpEl);
 
-      entry     = new ValEntry( dictEntry ); // Be sure it's never a BinEntry !
+      entry     = DataEntry::New( dictEntry ); // Be sure it's never a DataEntry !
 
       entry->SetOffset(0); // just to avoid further missprinting
 
@@ -989,7 +1101,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
       {
          // NULL when we Build Up (ex nihilo) a DICOMDIR
          //   or when we add the META elems
-         val = header->GetEntryValue(tmpGr, tmpEl);
+         val = header->GetEntryString(tmpGr, tmpEl);
       }
       else
       {
@@ -1026,13 +1138,14 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
             val = it->Value;
       }
 
-      entry->SetValue( val ); // troubles expected when vr=SQ ...
+      entry->SetString( val ); // troubles expected when vr=SQ ...
 
       if ( type == GDCM_DICOMDIR_META ) // fusible : should never print !
       {
          gdcmWarningMacro("GDCM_DICOMDIR_META ?!? should never print that");
       }
       si->AddEntry(entry);
+      entry->Delete();
    }
 }
 
@@ -1043,13 +1156,13 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
  * @param src source SQItem
  */
 void DicomDir::MoveSQItem(DocEntrySet *dst,DocEntrySet *src)
-{
+{ 
    DocEntry *entry;
 
    entry = src->GetFirstEntry();
    while(entry)
    {
-      src->RemoveEntryNoDestroy(entry);
+      src->RemoveEntry(entry);
       dst->AddEntry(entry);
       // we destroyed -> the current iterator is not longer valid
       entry = src->GetFirstEntry();