]> Creatis software - gdcm.git/blobdiff - Example/exExtractOverlaysACR.cxx
Unify exExtractOverlaysACR an exExtractOverlaysDCM codes
[gdcm.git] / Example / exExtractOverlaysACR.cxx
index c992b01ab8fef2a743b6a46c5019e090f81075bc..72f478f97b10767e9a69b5cbd60393c46db46d4b 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: exExtractOverlaysACR.cxx,v $
   Language:  C++
-  Date:      $Date: 2007/09/12 10:43:47 $
-  Version:   $Revision: 1.1 $
+  Date:      $Date: 2007/09/14 08:23:34 $
+  Version:   $Revision: 1.2 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 #include "gdcmCommon.h"
 #include "gdcmDebug.h"
 #include "gdcmDocEntry.h"
+#include "gdcmArgMgr.h"
+
 #include <iostream>
-#include <stdio.h> // for fseek... FIXME
 #include <stdlib.h> // for atoi
 
-#include "gdcmArgMgr.h" 
-
- // WARNING :
- // unfinished : DO NOT to be used as is !
+ /* SIEMENS_GBS_III-16-ACR_NEMA_1.acr
  
- /*
  // Example (sorry, we've got no more than this one ...)
  
-0028|0010[US] [Rows] [256] x(100)
-0028|0011[US] [Columns] [256] x(100)
-0028|0030[DS] [Pixel Spacing] [01.56\1.56]
-0028|0100[US] [Bits Allocated] [16] x(10)
-0028|0101[US] [Bits Stored] [12] x(c)
-0028|0102[US] [High Bit] [11] x(b)
-0028|0103[US] [Pixel Representation] [0] x(0)
+0028|0010[US] [Rows] [256] x(100)
+0028|0011[US] [Columns] [256] x(100)
+0028|0030[DS] [Pixel Spacing] [01.56\1.56]
+0028|0100[US] [Bits Allocated] [16] x(10)
+0028|0101[US] [Bits Stored] [12] x(c)
+0028|0102[US] [High Bit] [11] x(b)
+0028|0103[US] [Pixel Representation] [0] x(0)
  
-6000|0000[UL] [Group Length] [96] x(60)
-6000|0010[US] [Rows] [256] x(100)
-6000|0011[US] [Columns] [256] x(100)
-6000|0040[CS] [Overlay Type] [R ]
-6000|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
-6000|0100[US] [Overlay Bits Allocated] [16] x(10)
-6000|0102[US] [Overlay Bit Position] [12] x(c)
+6000|0000[UL] [Group Length] [96] x(60)
+6000|0010[US] [Rows] [256] x(100)
+6000|0011[US] [Columns] [256] x(100)
+6000|0040[CS] [Overlay Type] [R ]
+6000|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
+6000|0100[US] [Overlay Bits Allocated] [16] x(10)
+6000|0102[US] [Overlay Bit Position] [12] x(c)
 ...
 ...
-6006|0000[UL] [Group Length] [96] x(60)
-6006|0010[US] [Rows] [256] x(100)
-6006|0011[US] [Columns] [256] x(100)
-6006|0040[CS] [Overlay Type] [R ]
-6006|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
-6006|0100[US] [Overlay Bits Allocated] [16] x(10)
-6006|0102[US] [Overlay Bit Position] [15] x(f)
+6006|0000[UL] [Group Length] [96] x(60)
+6006|0010[US] [Rows] [256] x(100)
+6006|0011[US] [Columns] [256] x(100)
+6006|0040[CS] [Overlay Type] [R ]
+6006|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
+6006|0100[US] [Overlay Bits Allocated] [16] x(10)
+6006|0102[US] [Overlay Bit Position] [15] x(f)
  */
  
 int main(int argc, char *argv[])
-{ 
+{
    START_USAGE(usage)
    " \n exExtractOverlaysACR :\n                                              ",
    " Extract ACR-NEMA style overlays from an image                            ",
-   " usage: exExtractOverlaysACR filein=inputDirectoryName  [debug]           ",
+   " usage: exExtractOverlaysACR filein=inputFileName  [debug]                ",
    "        debug    : developper wants to run the program in 'debug mode'    ",
    FINISH_USAGE
 
@@ -77,10 +74,8 @@ int main(int argc, char *argv[])
       delete am;
       return 0;
    }
-   
-   // "SIEMENS_GBS_III-16-ACR_NEMA_1.acr"
-   
-   const char *fileName  = am->ArgMgrWantString("filein", usage);   
+
+   const char *fileName  = am->ArgMgrWantString("filein", usage);
 
    if (am->ArgMgrDefined("debug"))
       GDCM_NAME_SPACE::Debug::DebugOn();
@@ -98,30 +93,21 @@ int main(int argc, char *argv[])
 
    delete am;  // we don't need Argument Manager any longer
 
+   // ========================== Now, we can do the job! ================ 
 
-   // ========================== Now, we can do the job! ================   
    GDCM_NAME_SPACE::File *f;
 
 // ============================================================
 //   Read the input image.
 // ============================================================
 
-   //std::cout << argv[1] << std::endl;
-
    f = GDCM_NAME_SPACE::File::New( );
 
    f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
    f->SetFileName( fileName );
    bool res = f->Load();  
 
-   if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
-   {
-      std::cout << "---------------------------------------------" << std::endl;
-      f->Print();
-      std::cout << "---------------------------------------------" << std::endl;
-   }
+
    if (!res) {
        std::cout << "Sorry, " << fileName <<"  not a gdcm-readable "
            << "DICOM / ACR File"
@@ -129,7 +115,7 @@ int main(int argc, char *argv[])
       f->Delete();
       return 0;
    }
-   std::cout << " ... is readable " << std::endl;
+   std::cout << fileName << " ... is readable " << std::endl;
 
 // ============================================================
 //   Check whether image contains Overlays ACR-NEMA style.
@@ -149,56 +135,57 @@ int main(int argc, char *argv[])
       f->Delete();
       return 0;
    }
-   std::cout << " File is read! " << std::endl;
+   std::cout << fileName << " is read! " << std::endl;
 
    
 // ============================================================
 //   Load the pixels in memory.
 // ============================================================
 
-   int nx = f->GetXSize();
-   int ny = f->GetYSize();
-
    GDCM_NAME_SPACE::FileHelper *fh1 = GDCM_NAME_SPACE::FileHelper::New(f);
    fh1->SetKeepOverlays(true);
    uint16_t *pixels = (uint16_t *)fh1->GetImageDataRaw();
    int lgt = fh1->GetImageDataRawSize();
-   
+
    if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
-      std::cout << "Pixels read as expected : length = " << lgt << std::endl;   
-   
+      std::cout << "Pixels read as expected : length = " << lgt << std::endl;
+
 // ============================================================
 //   Prepare the stuff
 // ============================================================
-                                         
-   uint8_t *tabPixels = new uint8_t[nx*ny]; // uint8 is enought to hold 1 bit !
-   
-   uint16_t currentOvlGroup = 0x6000;
+
+   unsigned int nx = f->GetXSize();
+   unsigned int ny = f->GetYSize();
+   unsigned int nxy=nx*ny;   
+   uint16_t currentOvlGroup;
+   int i;
+
+   std::ostringstream str;
+
+   uint8_t *outputData = new uint8_t[nxy]; // uint8 is enought to hold 1 bit !
+
    std::string strOvlBitPosition;
    int ovlBitPosition;
    uint16_t mask;
-   int i = 0;
    uint16_t overlayLocation;
-   std::ostringstream str;
-
    std::string strOverlayLocation;
-   
+
    GDCM_NAME_SPACE::File *fileToBuild = 0;
    GDCM_NAME_SPACE::FileHelper *fh = 0;
 
 // ============================================================
-//   Get each overlay Bit into an image
+//   Get each overlay Bit into the image
 // ============================================================
-   for(i=0, currentOvlGroup=0x6000; i<32; i+=2 ,currentOvlGroup+=2)   
+   for(i=0, currentOvlGroup=0x6000; i<32; i+=2 ,currentOvlGroup+=2)
    {
       if ( (strOvlBitPosition = f->GetEntryString(currentOvlGroup, 0x0102)) 
                                                  == GDCM_NAME_SPACE::GDCM_UNFOUND )
           continue;
-  
-      if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
-         std::cout << "Current Overlay Group " << std::hex << currentOvlGroup 
+
+      if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
+         std::cout << "Current Overlay Group " << std::hex << currentOvlGroup
                    << " OvlBitPosition " << strOvlBitPosition << std::endl;
-      
+
       strOverlayLocation = f->GetEntryString(currentOvlGroup, 0x0200);
       if ( strOverlayLocation != GDCM_NAME_SPACE::GDCM_UNFOUND )
       {
@@ -213,56 +200,54 @@ int main(int argc, char *argv[])
             continue;
          }
       }
+
+      // ============================================================
+      //  DICOM Overlay Image data generation
+      // ============================================================
+
       ovlBitPosition = atoi(strOvlBitPosition.c_str());
       mask = 1 << ovlBitPosition;
-       
-      if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )      
-         std::cout << "Mask :[" <<std::hex << mask << "]" << std::endl;          
-      for (int j=0; j<nx*ny ; j++)
+
+      if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
+         std::cout << "Mask :[" <<std::hex << mask << "]" << std::endl;
+      for (int j=0; j<nxy; j++)
       {
          if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
             if (pixels[j] >= 0x1000)// if it contains at least one overlay bit
                printf("%d : %04x\n",j, pixels[j]);
 
          if ( (pixels[j] & mask) == 0 )
-            tabPixels[j] = 0;
+            outputData[j] = 0;
          else
-            tabPixels[j] = 128;
+            outputData[j] = 128;
       }
-      if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
-         std::cout << "About to built empty file"  << std::endl;
+   // ============================================================
+   //   Write a new file
+   // ============================================================
 
       fileToBuild = GDCM_NAME_SPACE::File::New();
-
-      if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
-         std::cout << "Finish to built empty file"  << std::endl;
-
       str.str("");
       str << nx;
-      fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
+      fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
       str.str("");
       str << ny;
-      fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
+      fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
 
-      fileToBuild->InsertEntryString("8",0x0028,0x0100); // Bits Allocated
-      fileToBuild->InsertEntryString("8",0x0028,0x0101); // Bits Stored
-      fileToBuild->InsertEntryString("7",0x0028,0x0102); // High Bit
-      fileToBuild->InsertEntryString("0",0x0028,0x0103); // Pixel Representation
-      fileToBuild->InsertEntryString("1",0x0028,0x0002); // Samples per Pixel
+      fileToBuild->InsertEntryString("8",0x0028,0x0100, "US"); // Bits Allocated
+      fileToBuild->InsertEntryString("8",0x0028,0x0101, "US"); // Bits Stored
+      fileToBuild->InsertEntryString("7",0x0028,0x0102, "US"); // High Bit
+      fileToBuild->InsertEntryString("0",0x0028,0x0103, "US"); // Pixel Representation
+      fileToBuild->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
+      fileToBuild->InsertEntryString("MONOCHROME2 ",0x0028,0x0004, "LO");
 
-      fileToBuild->InsertEntryString("MONOCHROME2 ",0x0028,0x0004);
+      // feel free to add any field (Dicom Data Entry) you like, here.
+      // ...
       // Other mandatory fields will be set automatically,
       // just before Write(), by FileHelper::CheckMandatoryElements()
 
-      if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
-         std::cout << "-------------About to built FileHelper"  << std::endl;
-
       fh = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
 
-      if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
-         std::cout << "-------------Finish to built FileHelper"  << std::endl;
-
-      fh->SetImageData(tabPixels,nx*ny);
+      fh->SetImageData(outputData,nx*ny);
       fh->SetWriteTypeToDcmExplVR();
 
 std::ostringstream tmp;
@@ -281,28 +266,21 @@ str << fileName << ".ovly." << tmp.str() << ".dcm" << std::ends;
       {
          std::cout << "Failed\n"
                    << "File [" << str.str() << "] is unwrittable" << std::endl;
-         /*
-         fh->Delete();
-         if (fileToBuild)
-            fileToBuild->Delete();
-         delete pixels;
-         delete tabPixels;
-         return 0;
-        */
       }
       else
       {
          std::cout << "File written successfully [" << str.str()  << "]" << std::endl;
       }
-   }
-    
+
+   } // end on loop on 60xx
+
    if (f)
       fh->Delete();
    if (fileToBuild)
       fileToBuild->Delete();
    f->Delete();
    delete pixels;
-   delete tabPixels;
+   delete outputData;
 
    return 0;
 }