]> Creatis software - gdcm.git/blobdiff - Example/exReadPapyrus.cxx
Avoid poluating 'Series Descr" with full path name
[gdcm.git] / Example / exReadPapyrus.cxx
index 274bd324ba8437f7fe7e28aaafd83570131e5afb..61a877d193ed555ef3167d7aef01c08c4bf3fbf9 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: exReadPapyrus.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/02/03 15:44:50 $
-  Version:   $Revision: 1.1 $
+  Date:      $Date: 2007/05/23 14:18:05 $
+  Version:   $Revision: 1.8 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 #include "gdcmFile.h"
 #include "gdcmFileHelper.h"
 #include "gdcmDocument.h"
-#include "gdcmValEntry.h"
-#include "gdcmBinEntry.h"
+#include "gdcmDataEntry.h"
 #include "gdcmSeqEntry.h"
 #include "gdcmSQItem.h"
 #include "gdcmDebug.h"
 #include "gdcmUtil.h"
 
+#include "gdcmArgMgr.h"
+#include <iostream>
+
 //#include <fstream>
 
 #ifndef _WIN32
@@ -61,7 +63,7 @@ bool RemoveFile(const char *source)
 }
 
 // ----------------------------------------------------------------------
-// Here we load a supposed to be Papyrus File (gdcm::File compliant)
+// Here we load a supposed to be Papyrus File (GDCM_NAME_SPACE::File compliant)
 // and then try to get the pixels, using low-level SeqEntry accessors.
 // Since it's not a general purpose Papyrus related program
 // (just a light example) we suppose *everything* is clean
@@ -70,45 +72,99 @@ bool RemoveFile(const char *source)
 
 int main(int argc, char *argv[])
 {
-   if (argc < 3)
+
+   START_USAGE(usage)
+   " \n exReadPapyrus :\n                                       ",
+   " Reads a Papyrus V3 File, Writes a Multiframe Dicom V3 File ",
+   "     (just to show gdcm can do it ...)                      ", 
+   "     ==> Well ... It does, but not too much! ",
+   "     ==> This code assumes that Pixel Data is uncompressed; no check is performed :-(",
+   "",
+   " usage: exReadPapyrus filein=inputPapyrusFileName fileout=outputDicomFileName", 
+   "                      [debug]  ", 
+   "        debug    : developper wants to run the program in 'debug mode'        ",
+   FINISH_USAGE
+
+
+   // ----- Initialize Arguments Manager ------
+   
+   GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
+  
+   if (am->ArgMgrDefined("usage")) 
    {
-      std::cerr << "Usage :" << std::endl << 
-      argv[0] << " input_papyrus output_dicom verbose" << std::endl;
-      return 1;
+      am->ArgMgrUsage(usage); // Display 'usage'
+      delete am;
+      return 0;
+   }
+   char *fileName = am->ArgMgrWantString("filein",usage);
+   if ( fileName == NULL )
+   {
+      delete am;
+      return 0;
    }
 
-   std::string filename = argv[1];
-   std::string output = argv[2];
+   char *outputFileName = am->ArgMgrWantString("fileout",usage);
+   if ( outputFileName == NULL )
+   {
+      delete am;
+      return 0;
+   }
 
-   if (argc > 3)
-      gdcm::Debug::DebugOn();
+   if (am->ArgMgrDefined("debug"))
+      GDCM_NAME_SPACE::Debug::DebugOn();
+   // if unused Params we give up
+   if ( am->ArgMgrPrintUnusedLabels() )
+   { 
+      am->ArgMgrUsage(usage);
+      delete am;
+      return 0;
+   }
+
+   delete am;  // we don't need Argument Manager any longer
 
-   if( FileExists( output.c_str() ) )
+   // ----------- End Arguments Manager ---------
+
+   if( FileExists( outputFileName ) )
    {
-      if( !RemoveFile( output.c_str() ) )
+      if( !RemoveFile( outputFileName ) )
       {
          std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
          return 1;
       }
    }
-   gdcm::FileHelper *original = new gdcm::FileHelper( filename );
-   gdcm::File *h = original->GetFile();
+
+   int loadMode = 0x0; // load everything
+   GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
+   f->SetLoadMode( loadMode );
+   f->SetFileName( fileName );
+   bool res = f->Load();  
+
+   if ( !res )
+   {
+      f->Delete();
+      return 0;
+   }
 
    // Look for private Papyrus Sequence
-   gdcm::SeqEntry *seqPapyrus= h->GetSeqEntry(0x0041, 0x1050);
+   GDCM_NAME_SPACE::SeqEntry *seqPapyrus= f->GetSeqEntry(0x0041, 0x1050);
    if (!seqPapyrus)
    {
-      std::cout << "NOT a Papyrus File" << std::endl;
-      delete h;
+      std::cout << "NOT a Papyrus File : " << fileName <<std::endl;
+      f->Delete();
       return 1;
    }
 
-   gdcm::SQItem *sqi = seqPapyrus->GetFirstSQItem();
+//   GDCM_NAME_SPACE::FileHelper *original = new GDCM_NAME_SPACE::FileHelper( fileName );
+//   GDCM_NAME_SPACE::File *h = original->GetFile();
+
+   //GDCM_NAME_SPACE::FileHelper *f1 = new GDCM_NAME_SPACE::FileHelper(f);
+   GDCM_NAME_SPACE::SQItem *sqi = seqPapyrus->GetFirstSQItem();
    if (sqi == 0)
    {
       std::cout << "NO SQItem found within private Papyrus Sequence"
           << std::endl;
-      delete h;
+      f->Delete();
       return 1;
    }
       
@@ -123,12 +179,12 @@ int main(int argc, char *argv[])
 //  Modality, Transfer Syntax, Study Date, Study Time
 // Patient Name, Media Storage SOP Instance UID, etc
 
-   MediaStSOPinstUID   =   h->GetEntryValue(0x0002,0x0002);
-   TransferSyntax      =   h->GetEntryValue(0x0002,0x0010);
-   StudyDate           = sqi->GetEntryValue(0x0008,0x0020);
-   StudyTime           = sqi->GetEntryValue(0x0008,0x0030);
-   Modality            = sqi->GetEntryValue(0x0008,0x0060);
-   PatientName         = sqi->GetEntryValue(0x0010,0x0010);
+   MediaStSOPinstUID   =  f->GetEntryString(0x0002,0x0002);
+   TransferSyntax      =  f->GetEntryString(0x0002,0x0010);
+   StudyDate           = sqi->GetEntryString(0x0008,0x0020);
+   StudyTime           = sqi->GetEntryString(0x0008,0x0030);
+   Modality            = sqi->GetEntryString(0x0008,0x0060);
+   PatientName         = sqi->GetEntryString(0x0010,0x0010);
 
    std::cout << "TransferSyntax " << TransferSyntax << std::endl;
 
@@ -143,17 +199,17 @@ int main(int argc, char *argv[])
 
    // we brutally suppose all the images within a Papyrus file
    // have the same caracteristics.
-   // if you're aware they have not, just move the GetEntryValue
+   // if you're aware they have not, just move the GetEntryString
    // inside the loop
 
    // Get caracteristics of the first image
-   SamplesPerPixel     = sqi->GetEntryValue(0x0028,0x0002);
-   Rows                = sqi->GetEntryValue(0x0028,0x0010);
-   Columns             = sqi->GetEntryValue(0x0028,0x0011);
-   BitsAllocated       = sqi->GetEntryValue(0x0028,0x0100);
-   BitsStored          = sqi->GetEntryValue(0x0028,0x0101);
-   HighBit             = sqi->GetEntryValue(0x0028,0x0102);
-   PixelRepresentation = sqi->GetEntryValue(0x0028,0x0102);
+   SamplesPerPixel     = sqi->GetEntryString(0x0028,0x0002);
+   Rows                = sqi->GetEntryString(0x0028,0x0010);
+   Columns             = sqi->GetEntryString(0x0028,0x0011);
+   BitsAllocated       = sqi->GetEntryString(0x0028,0x0100);
+   BitsStored          = sqi->GetEntryString(0x0028,0x0101);
+   HighBit             = sqi->GetEntryString(0x0028,0x0102);
+   PixelRepresentation = sqi->GetEntryString(0x0028,0x0103);
 
    // just convert those needed to compute PixelArea length
    int iRows            = (uint32_t) atoi( Rows.c_str() );
@@ -170,15 +226,14 @@ int main(int argc, char *argv[])
    //  allocate enough room to get the pixels of all images.
    uint8_t *PixelArea = new uint8_t[lgrImage*nbImages];
    uint8_t *currentPosition = PixelArea;
-  gdcm::BinEntry *pixels;
+   GDCM_NAME_SPACE::DataEntry *pixels;
 
    // declare and open the file
    std::ifstream *Fp;
-   Fp = new std::ifstream(filename.c_str(), std::ios::in | std::ios::binary);
+   Fp = new std::ifstream(fileName, std::ios::in | std::ios::binary);
    if( ! *Fp )
    {
-      std::cout <<  "Cannot open file: " << filename << std::endl;
-      //gdcmDebugMacro( "Cannot open file: " << filename.c_str() );
+      std::cout <<  "Cannot open file: " << fileName << std::endl;
       delete Fp;
       Fp = 0;
       return 0;
@@ -192,15 +247,19 @@ int main(int argc, char *argv[])
    while (sqi)
    {
       std::cout << "One more image read. Keep waiting" << std::endl;
-      Rows = sqi->GetEntryValue(0x0028,0x0010);
+      Rows = sqi->GetEntryString(0x0028,0x0010);
       // minimum integrity check
       if (Rows != previousRows)
       {
          std::cout << "Consistency check failed " << std::endl;
          return 1;
       }
+     
       // get the images pixels
-      pixels = sqi->GetBinEntry(0x7fe0,0x0010);
+      
+      // Oops! The following code assumes Pixel Data is uncompressed :-(
+      
+      pixels = sqi->GetDataEntry(0x7fe0,0x0010);
       offset = pixels->GetOffset();
       // perform a fseek, on offset length on the 'right' length
       Fp->seekg(offset, std::ios::beg);
@@ -215,43 +274,47 @@ int main(int argc, char *argv[])
 
    // build up a new File, with file info + images info + global pixel area.
 
-   std::string NumberOfFrames = gdcm::Util::Format("%d", nbImages); 
+   std::string NumberOfFrames = GDCM_NAME_SPACE::Util::Format("%d", nbImages); 
 
-   gdcm::File *n = new gdcm::File();
+   GDCM_NAME_SPACE::File *n = GDCM_NAME_SPACE::File::New();
 
-   n->InsertValEntry(MediaStSOPinstUID,  0x0002,0x0002);
+   n->InsertEntryString(MediaStSOPinstUID,  0x0002,0x0002);
   // Whe keep default gdcm Transfer Syntax (Explicit VR Little Endian)
   // since using Papyrus one (Implicit VR Little Endian) is a mess
-   //n->InsertValEntry(TransferSyntax,     0x0002,0x0010);
-   n->InsertValEntry(StudyDate,          0x0008,0x0020);
-   n->InsertValEntry(StudyTime,          0x0008,0x0030);
-   n->InsertValEntry(Modality,           0x0008,0x0060);
-   n->InsertValEntry(PatientName,        0x0010,0x0010);
-
-   n->InsertValEntry(SamplesPerPixel,    0x0028,0x0002);
-   n->InsertValEntry(NumberOfFrames,     0x0028,0x0008);
-   n->InsertValEntry(Rows,               0x0028,0x0010);
-   n->InsertValEntry(Columns,            0x0028,0x0011);
-   n->InsertValEntry(BitsAllocated,      0x0028,0x0100);
-   n->InsertValEntry(BitsStored,         0x0028,0x0101);
-   n->InsertValEntry(HighBit,            0x0028,0x0102);
-   n->InsertValEntry(PixelRepresentation,0x0028,0x0102);
+   //n->InsertEntryString(TransferSyntax,     0x0002,0x0010);
+   n->InsertEntryString(StudyDate,          0x0008,0x0020);
+   n->InsertEntryString(StudyTime,          0x0008,0x0030);
+   n->InsertEntryString(Modality,           0x0008,0x0060);
+   n->InsertEntryString(PatientName,        0x0010,0x0010);
+
+   n->InsertEntryString(SamplesPerPixel,    0x0028,0x0002);
+   n->InsertEntryString(NumberOfFrames,     0x0028,0x0008);
+   n->InsertEntryString(Rows,               0x0028,0x0010);
+   n->InsertEntryString(Columns,            0x0028,0x0011);
+   n->InsertEntryString(BitsAllocated,      0x0028,0x0100);
+   n->InsertEntryString(BitsStored,         0x0028,0x0101);
+   n->InsertEntryString(HighBit,            0x0028,0x0102);
+   n->InsertEntryString(PixelRepresentation,0x0028,0x0103);
 
    // create the file
-   gdcm::FileHelper *file = new gdcm::FileHelper(n);
+   GDCM_NAME_SPACE::FileHelper *file = GDCM_NAME_SPACE::FileHelper::New(n);
 
    file->SetImageData(PixelArea,lgrImage*nbImages);
    file->SetWriteTypeToDcmExplVR();
 
    //file->SetPrintLevel(2);
-   file->Print();
+   n->Print();
 
    // Write the file
-   file->Write(argv[2]); 
+   file->Write(outputFileName); 
    if (!file)
    {
-      std::cout <<"Fail to open (write) file:[" << argv[2]<< "]" << std::endl;;
+      std::cout <<"Fail to open (write) file:[" << outputFileName << "]" << std::endl;;
+      n->Delete();
+      file->Delete();
       return 1;  
    }
+   n->Delete();
+   file->Delete();
    return 0;
 }