]> Creatis software - gdcm.git/blobdiff - src/gdcmDicomDir.cxx
Load returns now a bool (allow user not to call IsReadable() )
[gdcm.git] / src / gdcmDicomDir.cxx
index fd83573bd5178f14855e3f401b1dc2ea93177a3b..d8e60efe966f7bc517b955ae8f8da74b01ab6d17 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmDicomDir.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/02/05 01:37:08 $
-  Version:   $Revision: 1.128 $
+  Date:      $Date: 2005/05/03 09:43:04 $
+  Version:   $Revision: 1.138 $
   
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 #else
 #   include <unistd.h>
 #endif
-
+// ----------------------------------------------------------------------------
+//         Note for future developpers
+// ----------------------------------------------------------------------------
+//
+//  Dicom PS 3.3 describes the relationship between Directory Records, as follow
+//    (see also PS 4.3, 2004, page 50 for Entity-Relationship model)
+//
+//  Directory Record Type      Directory Record Types which may be included
+//                                in the next lower-level directory Entity
+//
+// (Root directory Entity)     PATIENT
+//
+// PATIENT                     STUDY
+//
+// STUDY                       SERIES, VISIT, RESULTS, STUDY COMPONENT
+//
+// SERIES                      IMAGE, OVERLAYS, MODALITY LUT, VOI LUT,
+//                             CURVE, STORED PRINT, RT DOSE, RT STRUCTURE SET
+//                             RT PLAN, RT TREAT RECORD, PRESENTATION, WAVEFORM,
+//                             SR DOCUMENT, KEY OBJECT DOC, SPECTROSCOPY,
+//                             RAW DATA, REGISTRATION, FIDUCIAL
+// IMAGE
+// OVERLAY
+// MODALITY LUT
+// VOI LUT
+// CURVE
+// STORED PRINT
+// RT DOSE
+// RT STRUCTURE SET
+// RT PLAN
+// RT TREAT RECORD
+// PRESENTATION
+// WAVEFORM
+// SR DOCUMENT
+// KEY OBJECT DOC
+// SPECTROSCOPY
+// RAW DATA
+// REGISTRATION
+// FIDUCIAL
+// 
+// ----------------------
+// The current gdcm version only deals with :
+//
+// (Root directory Entity)     PATIENT
+// PATIENT                     STUDY
+// STUDY                       SERIES 
+// SERIES                      IMAGE
+// IMAGE                       /
+//
+// DicomDir::CreateDicomDir will have to be completed
+// Treelike structure management will have to be upgraded
+// ----------------------------------------------------------------------------
+    
 namespace gdcm 
 {
 //-----------------------------------------------------------------------------
@@ -78,82 +130,89 @@ DicomDir::DicomDir()
  *                        and wants to use it 
  */
 DicomDir::DicomDir(std::string const &fileName, bool parseDir ):
-   Document( fileName )
+   Document( )
 {
    // At this step, Document constructor is already executed,
    // whatever user passed (a root directory or a DICOMDIR)
    // and whatever the value of parseDir was.
    // (nothing is cheked in Document constructor, to avoid overhead)
 
-   Initialize();  // sets all private fields to NULL
+   ParseDir = parseDir;
+   SetLoadMode (0x00000000); // concerns only dicom files
+   Load( fileName );
+}
 
-   // if user passed a root directory, sure we didn't get anything
+/**
+ * \brief  Canonical destructor 
+ */
+DicomDir::~DicomDir() 
+{
+   SetStartMethod(NULL);
+   SetProgressMethod(NULL);
+   SetEndMethod(NULL);
 
-   if ( GetFirstEntry() == 0 ) // when user passed a Directory to parse
+   ClearPatient();
+   if ( MetaElems )
    {
-      if (!parseDir)
-         gdcmWarningMacro( "Entry HT empty for file: "<<fileName);
+      delete MetaElems;
+   }
+}
 
-   // Only if user passed a root directory
-   // ------------------------------------
-      if ( fileName == "." )
-      {
-         // user passed '.' as Name
-         // we get current directory name
-         char dummy[1000];
-         getcwd(dummy, (size_t)1000);
-         SetFileName( dummy ); // will be converted into a string
-      }
+//-----------------------------------------------------------------------------
+// Public
 
-      if ( parseDir ) // user asked for a recursive parsing of a root directory
-      {
-         NewMeta();
+bool DicomDir::Load(std::string const &fileName ) 
+{
+   Filename = fileName;
+   // We should clean out anything that already exists.
 
-         gdcmWarningMacro( "Parse directory and create the DicomDir");
-         ParseDirectory();
+   Initialize();  // sets all private fields to NULL
+
+   if (!ParseDir)
+   {
+   // Only if user passed a DICOMDIR
+   // ------------------------------
+      Fp = 0;
+      if ( !OpenFile() )
+      {
+         return false;
       }
-      else
+      Document::Load(fileName);
+      if ( GetFirstEntry() == 0 ) // when user passed a Directory to parse
       {
-         /// \todo if parseDir == false, it should be tagged as an error
-         // NO ! user may just call ParseDirectory() *after* constructor
+         gdcmWarningMacro( "Entry HT empty for file: "<< fileName);
+         return false;
       }
-   }
-   // Only if user passed a DICOMDIR
-   // ------------------------------
-   else 
-   {
       // Directory record sequence
       DocEntry *e = GetDocEntry(0x0004, 0x1220);
       if ( !e )
       {
          gdcmWarningMacro( "NO 'Directory record sequence' (0x0004,0x1220)"
                           << " in file " << fileName);
-         /// \todo FIXME : what do we do when the parsed file IS NOT a
-         ///       DICOMDIR file ?         
+         return false;
       }
       else
          CreateDicomDir();
    }
-}
-
-/**
- * \brief  Canonical destructor 
- */
-DicomDir::~DicomDir() 
-{
-   SetStartMethod(NULL);
-   SetProgressMethod(NULL);
-   SetEndMethod(NULL);
-
-   ClearPatient();
-   if ( MetaElems )
+   else
    {
-      delete MetaElems;
+   // Only if user passed a root directory
+   // ------------------------------------
+      if ( fileName == "." )
+      {
+         // user passed '.' as Name
+         // we get current directory name
+         char dummy[1000];
+         getcwd(dummy, (size_t)1000);
+         SetFileName( dummy ); // will be converted into a string
+      }
+      NewMeta();
+      gdcmWarningMacro( "Parse directory and create the DicomDir : " << Filename );
+      ParseDirectory();
    }
+   return true;
 }
 
-//-----------------------------------------------------------------------------
-// Public
 /**
  * \brief  This predicate, based on hopefully reasonable heuristics,
  *         decides whether or not the current document was properly parsed
@@ -298,6 +357,7 @@ void DicomDir::SetStartMethod( DicomDir::Method *method, void *arg,
    StartMethodArgDelete = argDelete;
 }
 
+
 /**
  * \brief   Set the progress method to call when the parsing of the
  *          directory progress
@@ -376,9 +436,6 @@ void DicomDir::SetEndMethodArgDelete( DicomDir::Method *method )
  * \brief    writes on disc a DICOMDIR
  * \ warning does NOT add the missing elements in the header :
  *           it's up to the user doing it !
- * \todo : to be re-written using the DICOMDIR tree-like structure
- *         *not* the chained list
- *         (does NOT exist if the DICOMDIR is user-forged !)
  * @param  fileName file to be written to 
  * @return false only when fail to open
  */
@@ -399,7 +456,7 @@ bool DicomDir::WriteDicomDir(std::string const &fileName)
 
    char filePreamble[128];
    memset(filePreamble, 0, 128);
-   fp->write(filePreamble, 128); //FIXME
+   fp->write(filePreamble, 128);
    binary_write( *fp, "DICM");
  
    DicomDirMeta *ptrMeta = GetMeta();
@@ -430,6 +487,53 @@ bool DicomDir::WriteDicomDir(std::string const &fileName)
    return true;
 }
 
+/**
+ * \brief    Anonymize a DICOMDIR
+ * @return true 
+ */
+bool DicomDir::AnonymizeDicomDir() 
+{
+   ValEntry *v;
+   // Something clever to be found to forge the Patient names
+   std::ostringstream s;
+   int i = 1;
+   for(ListDicomDirPatient::iterator cc = Patients.begin();
+                                     cc!= Patients.end();
+                                   ++cc)
+   {
+      s << i;
+      v = (*cc)->GetValEntry(0x0010, 0x0010) ; // Patient's Name
+      if (v)
+      {
+         v->SetValue(s.str());
+      }
+
+      v = (*cc)->GetValEntry(0x0010, 0x0020) ; // Patient ID
+      if (v)
+      {
+         v->SetValue(" ");
+      }
+
+      v = (*cc)->GetValEntry(0x0010, 0x0030) ; // Patient's BirthDate
+      if (v)
+      {
+         v->SetValue(" ");
+      }
+      s << "";
+      i++;
+   }
+   return true;
+}
+
+
+
+
+
+
+
+
+
 //-----------------------------------------------------------------------------
 // Protected
 /**
@@ -442,7 +546,7 @@ void DicomDir::CreateDicomDirChainedList(std::string const &path)
    DirList dirList(path,1); // gets recursively the file list
    unsigned int count = 0;
    VectDocument list;
-   File *header;
+   File *f;
 
    DirListType fileList = dirList.GetFilenames();
 
@@ -457,22 +561,25 @@ void DicomDir::CreateDicomDirChainedList(std::string const &path)
          break;
       }
 
-      header = new File( it->c_str() );
-      if( !header )
-      {
-         gdcmWarningMacro( "Failure in new gdcm::File " << it->c_str() );
-         continue;
-      }
+   f = new File( );
+   f->SetLoadMode(LoadMode); // we allow user not to load Sequences...
+   f->Load( it->c_str() );
+
+//     if( !f )
+//     {
+//         gdcmWarningMacro( "Failure in new gdcm::File " << it->c_str() );
+//         continue;
+//      }
       
-      if( header->IsReadable() )
+      if( f->IsReadable() )
       {
          // Add the file to the chained list:
-         list.push_back(header);
+         list.push_back(f);
          gdcmWarningMacro( "Readable " << it->c_str() );
        }
        else
        {
-          delete header;
+          delete f;
        }
        count++;
    }
@@ -552,7 +659,7 @@ void DicomDir::Initialize()
 }
 
 /**
- * \brief create a 'DicomDir' from a DICOMDIR Header 
+ * \brief create a 'gdcm::DicomDir' from a DICOMDIR Header 
  */
 void DicomDir::CreateDicomDir()
 {
@@ -569,7 +676,6 @@ void DicomDir::CreateDicomDir()
    if ( !e )
    {
       gdcmWarningMacro( "No Directory Record Sequence (0004,1220) found");
-      /// \todo FIXME: what to do when the parsed file IS NOT a DICOMDIR file ? 
       return;         
    }
    
@@ -596,7 +702,7 @@ void DicomDir::CreateDicomDir()
       }
       else
       {
-         gdcmWarningMacro( "Not a ValEntry.");
+         gdcmWarningMacro( "(0004,1430) not a ValEntry ?!?");
          continue;
       }
 
@@ -732,7 +838,7 @@ bool DicomDir::AddImageToEnd(DicomDirImage *dd)
  * @param   path path of the root directory
  * @param   list chained list of Headers
  */
-void DicomDir::SetElements(std::string const & path, VectDocument const &list)
+void DicomDir::SetElements(std::string const &path, VectDocument const &list)
 {
    ClearEntry();
    ClearPatient();
@@ -747,7 +853,8 @@ void DicomDir::SetElements(std::string const & path, VectDocument const &list)
 
    bool first = true;
    for( VectDocument::const_iterator it = list.begin();
-                                     it != list.end(); ++it )
+                                     it != list.end(); 
+                                   ++it )
    {
       // get the current file characteristics
       patCurName         = (*it)->GetEntryValue(0x0010,0x0010);
@@ -776,7 +883,6 @@ void DicomDir::SetElements(std::string const & path, VectDocument const &list)
          || first )
       {
          SetElement(path, GDCM_DICOMDIR_SERIE, *it);
-         first = true;
       }
       
       // Always Deal with 'IMAGE' Elements  
@@ -803,7 +909,7 @@ void DicomDir::SetElements(std::string const & path, VectDocument const &list)
 void DicomDir::SetElement(std::string const &path, DicomDirType type,
                           Document *header)
 {
-   ListDicomDirElem elemList; //FIXME this is going to be a by copy operation
+   ListDicomDirElem elemList;
    ListDicomDirElem::const_iterator it;
    uint16_t tmpGr, tmpEl;
    DictEntry *dictEntry;
@@ -869,10 +975,10 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
    // 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 all the relevant elements found in their own spot of the DicomDir.dic
    for( it = elemList.begin(); it != elemList.end(); ++it)
    {
       tmpGr     = it->Group;