]> Creatis software - gdcm.git/blobdiff - Example/ReWrite.cxx
Fix troubles in AnonymizeMultiPatient.
[gdcm.git] / Example / ReWrite.cxx
index 37d15073272714258874b4ce6f42d2fe27111ebc..dab56fb6931623ff89aaae412e768c9e38f35ee0 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: ReWrite.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/07/21 04:55:50 $
-  Version:   $Revision: 1.9 $
+  Date:      $Date: 2006/04/19 10:23:56 $
+  Version:   $Revision: 1.20 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -30,8 +30,9 @@ int main(int argc, char *argv[])
    " Re write a full gdcm-readable Dicom image                              ",
    "     (usefull when the file header is not very straight).               ",
    "                                                                        ",
-   " usage: ReWrite filein=inputFileName fileout=anonymizedFileName         ", 
-   "       [mode=write mode] [noshadow] [noseq][debug]                      ", 
+   " usage: ReWrite filein=inputFileName fileout=outputFileName             ", 
+   "       [mode=write mode] [noshadow] [noseq][debug]                      ",
+   "       [rubout=xBegin,xEnd,yBegin,yEnd [ruboutvalue=n (<255)] ]         ",
    "                                                                        ",
    "        mode = a (ACR), x (Explicit VR Dicom), r (RAW : only pixels)    ",
    "        noshadowseq: user doesn't want to load Private Sequences        ",
@@ -66,23 +67,44 @@ int main(int argc, char *argv[])
       return 0;
    }
 
-   char *mode = am->ArgMgrGetString("mode",(char *)"X");
+   const char *mode = am->ArgMgrGetString("mode","X");
 
-   int loadMode = 0x00000000;
+   int loadMode = gdcm::LD_ALL;
    if ( am->ArgMgrDefined("noshadowseq") )
-      loadMode |= NO_SHADOWSEQ;
+      loadMode |= gdcm::LD_NOSHADOWSEQ;
    else 
    {
    if ( am->ArgMgrDefined("noshadow") )
-         loadMode |= NO_SHADOW;
+         loadMode |= gdcm::LD_NOSHADOW;
       if ( am->ArgMgrDefined("noseq") )
-         loadMode |= NO_SEQ;
+         loadMode |= gdcm::LD_NOSEQ;
    }
 
    bool rgb = ( 0 != am->ArgMgrDefined("RGB") );
 
    if (am->ArgMgrDefined("debug"))
       gdcm::Debug::DebugOn();
+      
+   bool fail = false;
+   int *boundVal;
+   int ruboutVal;
+   bool rubout = false; 
+   if (am->ArgMgrDefined("rubout"))
+   {
+      int nbBound;
+      boundVal = am->ArgMgrGetListOfInt("rubout", &nbBound);
+
+      if (nbBound !=4)
+      {
+         std::cout << "Illegal number of 'rubout' boundary values (expected : 4, found:" 
+                   << nbBound << "); 'rubout' ignored" << std::endl;
+         fail = true;
+      }
+            
+      ruboutVal = am->ArgMgrGetInt("ruboutvalue", 0);
+      rubout = true;   
+   }
+
  
    // if unused Params we give up
    if ( am->ArgMgrPrintUnusedLabels() )
@@ -96,24 +118,24 @@ int main(int argc, char *argv[])
 
    // ----------- End Arguments Manager ---------
 
-   gdcm::File *f = new gdcm::File();
+   gdcm::File *f = gdcm::File::New();
    f->SetLoadMode( loadMode );
    f->SetFileName( fileName );
    bool res = f->Load();  
    if ( !res )
    {
-      delete f;
+      f->Delete();
       return 0;
    }
   
    if (!f->IsReadable())
    {
        std::cerr << "Sorry, not a Readable DICOM / ACR File"  <<std::endl;
-       delete f;
+       f->Delete();
        return 0;
    }
    
-   gdcm::FileHelper *fh = new gdcm::FileHelper(f);
+   gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
    void *imageData; 
    int dataSize;
   
@@ -150,7 +172,7 @@ int main(int argc, char *argv[])
              << " SampleserPixel="      << sPP
              << " PlanarConfiguration=" << planarConfig 
              << " PhotometricInterpretation=" 
-                                << f->GetEntryValue(0x0028,0x0004) 
+             << f->GetEntryString(0x0028,0x0004) 
              << std::endl;
 
    int numberOfScalarComponents=f->GetNumberOfScalarComponents();
@@ -160,46 +182,123 @@ int main(int argc, char *argv[])
    std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]" 
              << std::endl;
 
-   switch (mode[0])
-   {
-   case 'A' :
-   case 'a' :
-            // Writting an ACR file
-            // from a full gdcm readable File
-
-      std::cout << "WriteACR" << std::endl;
-      fh->WriteAcr(outputFileName);
-      break;
-
-   case 'D' : // Not documented in the 'usage', because the method 
-   case 'd' : //                             is known to be bugged. 
-           // Writting a DICOM Implicit VR file
-           // from a full gdcm readable File
-
-      std::cout << "WriteDCM Implicit VR" << std::endl;
-      fh->WriteDcmImplVR(outputFileName);
-      break;
+   // Since we just ReWrite the image, we know no modification 
+   // was performed on the pixels.
+   // We don't want this image appears as a 'Secondary Captured image'
+   fh->SetContentType(gdcm::UNMODIFIED_PIXELS_IMAGE);
+   
 
-   case 'X' :
-   case 'x' :
-              // writting a DICOM Explicit VR 
-              // from a full gdcm readable File
+   if (rubout)
+   {     
+      if (boundVal[0]<0 || boundVal[0]>nX)
+      { 
+         std::cout << "xBegin out of bounds; 'rubout' ignored" << std::endl;
+         fail = true;      
+      }
+      if (boundVal[1]<0 || boundVal[1]>nX)
+      { 
+         std::cout << "xEnd out of bounds; 'rubout' ignored" << std::endl;
+         fail = true;      
+      }
+      if (boundVal[0] > boundVal[1])
+      { 
+         std::cout << "xBegin greater than xEnd; 'rubout' ignored" << std::endl;
+         fail = true;      
+      }       
+      if (boundVal[2]<0 || boundVal[2]>nY)
+      { 
+         std::cout << "yBegin out of bounds; 'rubout' ignored" << std::endl;
+         fail = true;      
+      }
+      if (boundVal[3]<0 || boundVal[3]>nY)
+      { 
+         std::cout << "yEnd out of bounds; 'rubout' ignored" << std::endl;
+         fail = true;      
+      }
+      if (boundVal[2] > boundVal[3])
+      { 
+         std::cout << "yBegin greater than yEnd; 'rubout' ignored" << std::endl;
+         fail = true;      
+      }  
+      if (!fail)
+      {
+         int pixelLength = f->GetBitsAllocated()/8;
+         int lineLength = nX * sPP * pixelLength;
+         size_t lengthToRubout = (boundVal[1]-boundVal[0])*sPP*pixelLength;
+         int offsetToBeginOfRubout = boundVal[0]*sPP*pixelLength+lineLength*boundVal[2];
+      
+         for(int rbl=boundVal[2]; rbl<boundVal[3];rbl++)
+         {
+            memset((char *)imageData+offsetToBeginOfRubout, ruboutVal, lengthToRubout);
+            offsetToBeginOfRubout += lineLength; 
+         }
+      }   
+   } 
 
-      std::cout << "WriteDCM Explicit VR" << std::endl;
-      fh->WriteDcmExplVR(outputFileName);
-      break;
+   switch (mode[0])
+   {
+      case 'A' :
+      case 'a' :
+      // Writting an ACR file
+      // from a full gdcm readable File
+         std::cout << "WriteACR" << std::endl;
+         fh->WriteAcr(outputFileName);
+         break;
 
-   case 'R' :
-   case 'r' :
-             //  Writting a Raw File, 
+      case 'D' : // Not documented in the 'usage', because the method 
+      case 'd' : //                             is known to be bugged. 
+      // Writting a DICOM Implicit VR file
+      // from a full gdcm readable File
+         std::cout << "WriteDCM Implicit VR" << std::endl;
+         fh->WriteDcmImplVR(outputFileName);
+         break;
 
-      std::cout << "WriteRaw" << std::endl;
-      fh->WriteRawData(outputFileName);
-      break;
+      case 'X' :
+      case 'x' :
+      // writting a DICOM Explicit VR 
+      // from a full gdcm readable File
+         std::cout << "WriteDCM Explicit VR" << std::endl;
+         // fh->WriteDcmExplVR(outputFileName);
+         // Try this one :
+         fh->SetWriteTypeToDcmExplVR();
+         fh->Write(outputFileName);
+         break;
 
+      case 'R' :
+      case 'r' :
+      //  Writting a Raw File, 
+         std::cout << "WriteRaw" << std::endl;
+         fh->WriteRawData(outputFileName);
+         break;
+ // Just for fun :
+ // Write a 'Video inverse' version of the file.
+ // *Not* described, on purpose,  in the USAGE  
+      case 'V' :
+      case 'v' :
+         if ( fh->GetFile()->GetBitsAllocated() == 8)
+         {
+            std::cout << "videoinv for 8 bits" << std::endl;
+            for (int i=0; i<dataSize; i++) 
+            {
+               ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
+            }
+         }
+         else
+         {
+            std::cout << "videoinv for 16 bits" << std::endl;    
+            for (int i=0; i<dataSize/2; i++) 
+            {
+               ((uint16_t*)imageData)[i] =  65535 - ((uint16_t*)imageData)[i];
+            }
+         }
+         std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
+         fh->WriteDcmExplVR(outputFileName);
+         break;
    }
-   delete f;
-   delete fh;
+
+   f->Delete();
+   fh->Delete();
    return 0;
 }