]> Creatis software - gdcm.git/blobdiff - Testing/TestCopyRescaleDicom.cxx
ENH: Pass 2 at cleaning the JPEG mess. Still some work to do, but things are getting...
[gdcm.git] / Testing / TestCopyRescaleDicom.cxx
index 9b29da1fe148efe9488582acafb35c128ccf3d5a..dc9b2a9244e4db63308419364fbcc626e29a4adf 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: TestCopyRescaleDicom.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/12/07 16:55:56 $
-  Version:   $Revision: 1.1 $
+  Date:      $Date: 2005/01/26 16:43:10 $
+  Version:   $Revision: 1.12 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -15,8 +15,8 @@
      PURPOSE.  See the above copyright notices for more information.
                                                                                 
 =========================================================================*/
-#include "gdcmHeader.h"
 #include "gdcmFile.h"
+#include "gdcmFileHelper.h"
 #include "gdcmValEntry.h"
 #include "gdcmBinEntry.h"
 
@@ -43,79 +43,74 @@ int CopyRescaleDicom(std::string const & filename,
 
    //////////////// Step 1:
    std::cout << "      1...";
-   gdcm::Header *originalH = new gdcm::Header( filename );
-   gdcm::Header *copyH     = new gdcm::Header( );
-
-   //First of all copy the header field by field
-
-   // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
-   // It was commented out by Mathieu, that was a *good* idea
-   // (the user does NOT have to know the way we implemented the Header !)
-   // Waiting for a 'clean' solution, I keep the method ...JPRx
+   gdcm::File *originalF = new gdcm::File( filename );
+   gdcm::File *copyF     = new gdcm::File( );
 
+   //First of all copy the file, field by field
 
    //////////////// Step 2:
    std::cout << "2...";
-   originalH->Initialize();
-   gdcm::DocEntry* d = originalH->GetNextEntry();
-
+   // Copy of the header content
+   gdcm::DocEntry* d = originalF->GetFirstEntry();
    while(d)
    {
-      if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
+      if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
+      {
+         copyF->InsertBinEntry( b->GetBinArea(),b->GetLength(),
+                                b->GetGroup(),b->GetElement(),
+                                b->GetVR() );
+      }
+      else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
       {   
-          copyH->ReplaceOrCreateByNumber( 
-                              v->GetValue(),
-                              v->GetGroup(), 
-                              v->GetElement(),
-                              v->GetVR() ); 
+          copyF->InsertValEntry( v->GetValue(),
+                                 v->GetGroup(),v->GetElement(),
+                                 v->GetVR() ); 
+      }
+      else
+      {
+       // We skip pb of SQ recursive exploration
       }
 
-      d=originalH->GetNextEntry();
+      d=originalF->GetNextEntry();
    }
 
-   gdcm::File *original = new gdcm::File( originalH );
-   gdcm::File *copy     = new gdcm::File( copyH );
+   gdcm::FileHelper *original = new gdcm::FileHelper( originalF );
+   gdcm::FileHelper *copy     = new gdcm::FileHelper( copyF );
 
    size_t dataSize = original->GetImageDataSize();
-   uint8_t* imageData = original->GetImageData();
-
-   size_t rescaleSize = dataSize / 2;
-#ifdef VERSION_1
-   uint8_t *rescaleImage = new uint8_t[dataSize];
-#endif
 
-   //2 case now, if 16 bits input then try to downscale to 8bits just for
-   //fun:
-
-   const std::string & bitsStored    = originalH->GetEntryByNumber(0x0028,0x0101);
-//   std::cout << "BitsStored:    " << bitsStored << std::endl;
+   size_t rescaleSize;
+   uint8_t *rescaleImage;
 
+   const std::string & bitsStored    = originalF->GetEntryValue(0x0028,0x0101);
    if( bitsStored == "16" )
    {
-      uint16_t* imageData16 = (uint16_t*)original->GetImageData();
-      copyH->ReplaceOrCreateByNumber( "8", 0x0028, 0x0100); // BitsAllocated
-      copyH->ReplaceOrCreateByNumber( "8", 0x0028, 0x0101); // BitsStored
-      copyH->ReplaceOrCreateByNumber( "7", 0x0028, 0x0102); // HighBit
-      copyH->ReplaceOrCreateByNumber( "0", 0x0028, 0x0103); //Pixel Representation
+      std::cout << "Rescale...";
+      copyF->InsertValEntry( "8", 0x0028, 0x0100); // BitsAllocated
+      copyF->InsertValEntry( "8", 0x0028, 0x0101); // BitsStored
+      copyF->InsertValEntry( "7", 0x0028, 0x0102); // HighBit
+      copyF->InsertValEntry( "0", 0x0028, 0x0103); //Pixel Representation
  
       // We assume the value were from 0 to uint16_t max
+      rescaleSize = dataSize / 2;
+      rescaleImage = new uint8_t[dataSize];
+
+      uint16_t* imageData16 = (uint16_t*)original->GetImageData();
       for(unsigned int i=0; i<rescaleSize; i++)
       {
-#ifdef VERSION_1
          rescaleImage[i] = imageData16[i]/256;
-#else
-         imageData[i]  = imageData16[i]/256;
-#endif
       }
-#ifdef VERSION_1
-      copy->SetImageData(rescaleImage, rescaleSize);
-#endif 
    }
    else
    {
-      copy->SetImageData(imageData, dataSize);
+      std::cout << "Same...";
+      rescaleSize = dataSize;
+      rescaleImage = new uint8_t[dataSize];
+      memcpy(rescaleImage,original->GetImageData(),dataSize);
    }
 
+   copy->SetImageData(rescaleImage, rescaleSize);
+
    //////////////// Step 3:
    std::cout << "3...";
    copy->SetWriteModeToRGB();
@@ -126,27 +121,29 @@ int CopyRescaleDicom(std::string const & filename,
 
       delete original;
       delete copy;
-      delete originalH;
-      delete copyH;
+      delete originalF;
+      delete copyF;
+      delete[] rescaleImage;
 
       return 1;
    }
 
    delete copy;
-   delete copyH;
+   delete copyF;
 
    //////////////// Step 4:
    std::cout << "4...";
-   copy = new gdcm::File( output );
+   copy = new gdcm::FileHelper( output );
 
    //Is the file written still gdcm parsable ?
-   if ( !copy->GetHeader()->IsReadable() )
+   if ( !copy->GetFile()->IsReadable() )
    { 
       std::cout << " Failed" << std::endl
                 << "        " << output << " not readable" << std::endl;
 
       delete original;
-      delete originalH;
+      delete originalF;
+      delete[] rescaleImage;
 
       return 1;
    }
@@ -156,7 +153,27 @@ int CopyRescaleDicom(std::string const & filename,
    size_t    dataSizeWritten = copy->GetImageDataSize();
    uint8_t* imageDataWritten = copy->GetImageData();
 
-   if (dataSize != dataSizeWritten)
+   if (originalF->GetXSize() != copy->GetFile()->GetXSize() ||
+       originalF->GetYSize() != copy->GetFile()->GetYSize() ||
+       originalF->GetZSize() != copy->GetFile()->GetZSize())
+   {
+      std::cout << "Failed" << std::endl
+         << "        X Size differs: "
+         << "X: " << originalF->GetXSize() << " # " 
+                  << copy->GetFile()->GetXSize() << " | "
+         << "Y: " << originalF->GetYSize() << " # " 
+                  << copy->GetFile()->GetYSize() << " | "
+         << "Z: " << originalF->GetZSize() << " # " 
+                  << copy->GetFile()->GetZSize() << std::endl;
+      delete original;
+      delete copy;
+      delete originalF;
+      delete[] rescaleImage;
+
+      return 1;
+   }
+
+   if (rescaleSize != dataSizeWritten)
    {
       std::cout << " Failed" << std::endl
                 << "        Pixel areas lengths differ: "
@@ -164,12 +181,13 @@ int CopyRescaleDicom(std::string const & filename,
 
       delete original;
       delete copy;
-      delete originalH;
+      delete originalF;
+      delete[] rescaleImage;
 
       return 1;
    }
 
-   if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
+   if (int res = memcmp(rescaleImage, imageDataWritten, rescaleSize) !=0)
    {
       (void)res;
       std::cout << " Failed" << std::endl
@@ -177,7 +195,8 @@ int CopyRescaleDicom(std::string const & filename,
 
       delete original;
       delete copy;
-      delete originalH;
+      delete originalF;
+      delete[] rescaleImage;
 
       return 1;
    }
@@ -185,10 +204,8 @@ int CopyRescaleDicom(std::string const & filename,
 
    delete original;
    delete copy;
-   delete originalH;
-#ifdef VERSION_1
+   delete originalF;
    delete[] rescaleImage;
-#endif
 
    return 0;
 }
@@ -223,12 +240,12 @@ int TestCopyRescaleDicom(int argc, char* argv[])
              << std::endl;
    std::cout << "   apply the following to each filename.xxx: "
              << std::endl;
-   std::cout << "   step 1: parse the image (as gdcmHeader) and call"
+   std::cout << "   step 1: parse the image (as gdcmFile) and call"
              << " IsReadable(). After that, call GetImageData() and "
              << "GetImageDataSize() "
              << std::endl;
    std::cout << "   step 2: create a copy of the readed file and the new"
-             << " pixel datas are set to the copy"
+             << " pixel data are set to the copy"
              << std::endl;
    std::cout << "   step 3: write the copy of the image"
              << std::endl;