]> Creatis software - gdcm.git/blobdiff - src/gdcmFile.cxx
BUG: In c++ you are never garantee your var is initialized to 0
[gdcm.git] / src / gdcmFile.cxx
index d0d166b470598c9e839bf06f784d81f2837ea87e..94f5ca5b22b4895807937ffae513695655a7fc4c 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmFile.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/02/05 01:37:08 $
-  Version:   $Revision: 1.212 $
+  Date:      $Date: 2005/02/10 20:53:23 $
+  Version:   $Revision: 1.223 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -23,7 +23,7 @@
 // If not found (ACR_NEMA) we try Image Position       (0020,0030)
 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
 //                                   or Location       (0020,0050) 
-// as the Z coordinate, 
+//                                   as the Z coordinate, 
 // 0. for all the coordinates if nothing is found
 //
 // ---------------------------------------------------------------
@@ -35,6 +35,7 @@
 #include "gdcmTS.h"
 #include "gdcmValEntry.h"
 #include "gdcmBinEntry.h"
+#include "gdcmSeqEntry.h"
 #include "gdcmRLEFramesInfo.h"
 #include "gdcmJPEGFragmentsInfo.h"
 
@@ -366,10 +367,11 @@ int File::GetZSize()
   */
 float File::GetXSpacing()
 {
-   float xspacing, yspacing;
+   float xspacing = 1.0;
+   float yspacing = 1.0;
    const std::string &strSpacing = GetEntryValue(0x0028,0x0030);
 
-   if ( strSpacing == GDCM_UNFOUND )
+   if( strSpacing == GDCM_UNFOUND )
    {
       gdcmWarningMacro( "Unfound Pixel Spacing (0028,0030)" );
       return 1.;
@@ -379,11 +381,12 @@ float File::GetXSpacing()
    if( ( nbValues = sscanf( strSpacing.c_str(), 
          "%f\\%f", &yspacing, &xspacing)) != 2 )
    {
+      // if no values, xspacing is set to 1.0
+      if( nbValues == 0 )
+         xspacing = 1.0;
       // if single value is found, xspacing is defaulted to yspacing
-      if ( nbValues == 1 )
-      {
+      if( nbValues == 1 )
          xspacing = yspacing;
-      }
 
       if ( xspacing == 0.0 )
          xspacing = 1.0;
@@ -424,7 +427,11 @@ float File::GetYSpacing()
     }
 
    // if sscanf cannot read any float value, it won't affect yspacing
-   sscanf( strSpacing.c_str(), "%f", &yspacing);
+   int nbValues = sscanf( strSpacing.c_str(), "%f", &yspacing);
+
+   // if no values, xspacing is set to 1.0
+   if( nbValues == 0 )
+      yspacing = 1.0;
 
    if ( yspacing == 0.0 )
       yspacing = 1.0;
@@ -643,7 +650,7 @@ void File::GetImageOrientationPatient( float iop[6] )
 
 /**
  * \brief   Retrieve the number of Bits Stored (actually used)
- *          (as opposite to number of Bits Allocated)
+ *          (as opposed to number of Bits Allocated)
  * @return  The encountered number of Bits Stored, 0 by default.
  *          0 means the file is NOT USABLE. The caller has to check it !
  */
@@ -1095,7 +1102,7 @@ int File::GetNumberOfScalarComponentsRaw()
  */
 size_t File::GetPixelOffset()
 {
-   DocEntry* pxlElement = GetDocEntry(GrPixel,NumPixel);
+   DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
    if ( pxlElement )
    {
       return pxlElement->GetOffset();
@@ -1117,7 +1124,7 @@ size_t File::GetPixelOffset()
  */
 size_t File::GetPixelAreaLength()
 {
-   DocEntry* pxlElement = GetDocEntry(GrPixel,NumPixel);
+   DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
    if ( pxlElement )
    {
       return pxlElement->GetLength();
@@ -1131,33 +1138,118 @@ size_t File::GetPixelAreaLength()
 }
 
 /**
- * \brief anonymize a File (removes Patient's personal info)
- *        (read the code to see which ones ...)
+ * \brief Adds the characteristics of a new element we want to anonymize
+ *
  */
-bool File::AnonymizeFile()
+void File::AddAnonymizeElement (uint16_t group, uint16_t elem, 
+                                std::string const &value) 
+
+{ 
+   Element el;
+   el.Group = group;
+   el.Elem  = elem;
+   el.Value = value;
+   AnonymizeList.push_back(el); 
+}
+
+/**
+ * \brief Overwrites in the file the values of the DicomElements
+ *       held in the list 
+ */
+void File::AnonymizeNoLoad()
 {
-   // If exist, replace by spaces
-   SetValEntry ("  ",0x0010, 0x2154); // Telephone   
-   SetValEntry ("  ",0x0010, 0x1040); // Adress
-   SetValEntry ("  ",0x0010, 0x0020); // Patient ID
+   std::fstream *fp = new std::fstream(Filename.c_str(), 
+                              std::ios::in | std::ios::out | std::ios::binary);
+   // TODO : FIXME
+   // how to white out disk space if longer than 50 ?
+   
+   
+   gdcm::DocEntry *d;
+   uint32_t offset;
+   uint32_t lgth;
+   uint32_t valLgth = 0;
+   std::string *spaces;
+   for (ListElements::iterator it = AnonymizeList.begin();  
+                               it != AnonymizeList.end();
+                             ++it)
+   { 
+      d = GetDocEntry( (*it).Group, (*it).Elem);
 
-   DocEntry* patientNameHE = GetDocEntry (0x0010, 0x0010);
-  
-   if ( patientNameHE ) // we replace it by Study Instance UID (why not)
-   {
-      std::string studyInstanceUID =  GetEntryValue (0x0020, 0x000d);
-      if ( studyInstanceUID != GDCM_UNFOUND )
+      if ( d == NULL)
+         continue;
+
+      if ( dynamic_cast<BinEntry *>(d)
+        || dynamic_cast<SeqEntry *>(d) )
+         continue;
+
+      offset = d->GetOffset();
+      lgth =   d->GetLength();
+      if (valLgth < lgth)
       {
-         InsertValEntry(studyInstanceUID, 0x0010, 0x0010);
+         spaces = new std::string( lgth-valLgth, ' ');
+         (*it).Value = (*it).Value + *spaces;
+         delete spaces;
       }
-      else
+      fp->seekp( offset, std::ios::beg );
+      fp->write( (*it).Value.c_str(), lgth );
+     
+   }
+   fp->close();
+   delete fp;
+}
+
+/**
+ * \brief anonymize a File (removes Patient's personal info passed with
+ *        AddAnonymizeElement()
+ */
+bool File::AnonymizeFile()
+{
+   // If Anonymisation list is empty, let's perform some basic anonymization
+   if ( AnonymizeList.begin() == AnonymizeList.end() )
+   {
+      // If exist, replace by spaces
+      SetValEntry ("  ",0x0010, 0x2154); // Telephone   
+      SetValEntry ("  ",0x0010, 0x1040); // Adress
+      SetValEntry ("  ",0x0010, 0x0020); // Patient ID
+
+      DocEntry* patientNameHE = GetDocEntry (0x0010, 0x0010);
+  
+      if ( patientNameHE ) // we replace it by Study Instance UID (why not ?)
       {
-         InsertValEntry("anonymised", 0x0010, 0x0010);
+         std::string studyInstanceUID =  GetEntryValue (0x0020, 0x000d);
+         if ( studyInstanceUID != GDCM_UNFOUND )
+         {
+            SetValEntry(studyInstanceUID, 0x0010, 0x0010);
+         }
+         else
+         {
+            SetValEntry("anonymised", 0x0010, 0x0010);
+         }
       }
    }
+   else
+   {
+      gdcm::DocEntry *d;
+      for (ListElements::iterator it = AnonymizeList.begin();  
+                                  it != AnonymizeList.end();
+                                ++it)
+      {  
+         d = GetDocEntry( (*it).Group, (*it).Elem);
+
+         if ( d == NULL)
+            continue;
+
+         if ( dynamic_cast<BinEntry *>(d)
+           || dynamic_cast<SeqEntry *>(d) )
+            continue;
+
+         SetValEntry ((*it).Value, (*it).Group, (*it).Elem);
+      }
+}
 
-  // Just for fun :-(
-  // (if any) remove or replace all the stuff that contains a Date
+  // In order to make definitively impossible any further identification
+  // remove or replace all the stuff that contains a Date
 
 //0008 0012 DA ID Instance Creation Date
 //0008 0020 DA ID Study Date
@@ -1295,40 +1387,8 @@ bool File::Write(std::string fileName, FileType filetype)
       }
    }
 
-
-#ifdef GDCM_WORDS_BIGENDIAN
-   // Super Super hack that will make gdcm a BOMB ! but should
-   // Fix temporarily the dashboard
-   BinEntry *b = GetBinEntry(GrPixel,NumPixel);
-   if ( GetPixelSize() ==  16 )
-   {
-      uint16_t *im16 = (uint16_t *)b->GetBinArea();
-      int lgr = b->GetLength();
-      for( int i = 0; i < lgr / 2; i++ )
-      {
-         im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
-      }
-   }
-#endif //GDCM_WORDS_BIGENDIAN
-
-
    Document::WriteContent(fp, filetype);
 
-
-#ifdef GDCM_WORDS_BIGENDIAN
-   // Flip back the pixel ... I told you this is a hack
-   if ( GetPixelSize() ==  16 )
-   {
-      uint16_t *im16 = (uint16_t*)b->GetBinArea();
-      int lgr = b->GetLength();
-      for( int i = 0; i < lgr / 2; i++ )
-      {
-         im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
-      }
-   }
-#endif //GDCM_WORDS_BIGENDIAN
-
-
    fp->close();
    delete fp;
 
@@ -1359,7 +1419,7 @@ void File::InitializeDefaultFile()
    // Media Storage SOP Class UID (CT Image Storage)
    InsertValEntry("1.2.840.10008.5.1.4.1.1.2", 0x0002, 0x0002);
    // Media Storage SOP Instance UID
-   InsertValEntry(uidClass.c_str(),            0x0002, 0x0003);
+   InsertValEntry(uidMedia.c_str(),            0x0002, 0x0003);
    // Transfer Syntax UID (Explicit VR Little Endian)
    InsertValEntry("1.2.840.10008.1.2.1 ",      0x0002, 0x0010);
    // META Implementation Class UID
@@ -1578,7 +1638,7 @@ bool File::ReadTag(uint16_t testGroup, uint16_t testElement)
    long positionOnEntry = Fp->tellg();
    long currentPosition = Fp->tellg();          // On debugging purposes
 
-   //// Read the Item Tag group and element, and make
+   // Read the Item Tag group and element, and make
    // sure they are what we expected:
    uint16_t itemTagGroup;
    uint16_t itemTagElement;