]> Creatis software - gdcm.git/blobdiff - Testing/TestAllReadCompareDicom.cxx
Should suppress some warnings from Borland compiler
[gdcm.git] / Testing / TestAllReadCompareDicom.cxx
index 46d51a6cace7f797e9a2709d0306c6758c16115e..56f5646b85814d3ae0bbb79aa6b1cabc31154077 100644 (file)
-#include "gdcmHeader.h"
+/*=========================================================================
+                                                                                
+  Program:   gdcm
+  Module:    $RCSfile: TestAllReadCompareDicom.cxx,v $
+  Language:  C++
+  Date:      $Date: 2005/04/19 10:05:36 $
+  Version:   $Revision: 1.39 $
+                                                                                
+  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
+  l'Image). All rights reserved. See Doc/License.txt or
+  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
+                                                                                
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+                                                                                
+=========================================================================*/
+#include "gdcmDirList.h"
 #include "gdcmFile.h"
+#include "gdcmFileHelper.h"
+
+#include <iostream>
 
 //Generated file:
 #include "gdcmDataImages.h"
 
-int TestAllReadCompareDicom(int argc, char* argv[]) 
+/**
+ * /brief   File Read/Writer specific for the TestAllReadCompareDicom test
+ * /remarks The Test file format is (only in little endian) :
+ *  - 4 bytes : 'gdcm'
+ *  - 4 bytes : size X
+ *  - 4 bytes : size Y
+ *  - 4 bytes : size Z
+ *  - 2 bytes : scalar size (8,16,32)
+ *  - 2 bytes : number of components per pixel (1,2,3)
+ *  - n bytes : datas
+ */
+class TestFile
 {
-   if ( argc > 1 )
+public:
+   TestFile(void);
+   ~TestFile(void);
+
+   bool IsReadable(void) {return readable;}
+   int GetXSize(void) {return sizeX;};
+   void SetXSize(int size) {sizeX = size;};
+   int GetYSize(void) {return sizeY;};
+   void SetYSize(int size) {sizeY = size;};
+   int GetZSize(void) {return sizeZ;};
+   void SetZSize(int size) {sizeZ = size;};
+   int GetScalarSize(void) {return scalarSize;};
+   void SetScalarSize(int size) {scalarSize = size;};
+   int GetNumberOfComponents(void) {return components;};
+   void SetNumberOfComponents(int size) {components = size;};
+
+   unsigned long GetDataSize(void) {return GetLineSize()*sizeY*sizeZ;}
+   uint8_t *GetData(void) {return data;}
+   void SetData(const uint8_t *newData);
+
+   void Load(const std::string &filename);
+   void Write(const std::string &filename);
+
+private:
+   unsigned long GetLineSize(void) {return sizeX*scalarSize*components;}
+   int GetSwapCode(uint32_t tag);
+
+   void NewData(void);
+   void DeleteData(void);
+
+   void ReadFile(void);
+   bool ReadFileHeader(std::ifstream *fp);
+   bool ReadFileData(std::ifstream *fp);
+   void WriteFile(void);
+   bool WriteFileHeader(std::ofstream *fp);
+   bool WriteFileData(std::ofstream *fp);
+
+   uint8_t  ReadInt8 (std::ifstream *fp)
+#if !(__GNUC__==2  && __GNUC_MINOR__<=96)
+ throw( std::ios::failure );
+#else
+ ;
+#endif
+   uint16_t ReadInt16(std::ifstream *fp)
+#if !(__GNUC__==2  && __GNUC_MINOR__<=96)
+  throw( std::ios::failure );
+#else
+ ;
+#endif
+   uint32_t ReadInt32(std::ifstream *fp)
+#if !(__GNUC__==2  && __GNUC_MINOR__<=96)
+  throw( std::ios::failure );
+#else
+ ;
+#endif
+   void WriteInt8 (std::ofstream *fp,uint8_t  value);
+   void WriteInt16(std::ofstream *fp,uint16_t value);
+   void WriteInt32(std::ofstream *fp,uint32_t value);
+
+   std::string fileName;
+   bool readable;
+
+   int sizeX;
+   int sizeY;
+   int sizeZ;
+   uint16_t scalarSize;
+   uint16_t components;
+   uint8_t *data;
+   int swapCode;
+
+   static const unsigned int HEADER_SIZE;
+};
+
+const unsigned int TestFile::HEADER_SIZE = 20;
+
+TestFile::TestFile(void)
+{
+   fileName = "";
+   readable=false;
+
+   sizeX = 0;
+   sizeY = 0;
+   sizeZ = 0;
+   scalarSize = 0;
+   components = 0;
+   data = NULL;
+
+   swapCode = 1234;
+}
+
+TestFile::~TestFile(void)
+{
+   DeleteData();
+}
+
+void TestFile::SetData(const uint8_t *newData)
+{
+   DeleteData();
+   NewData();
+   if( data )
+      memcpy(data,newData,GetDataSize());
+}
+
+void TestFile::Load(const std::string &filename)
+{
+   fileName = filename;
+   ReadFile();
+}
+
+void TestFile::Write(const std::string &filename)
+{
+   fileName = filename;
+   WriteFile();
+}
+
+int TestFile::GetSwapCode(uint32_t tag)
+{
+   int swap = 0;
+   for(int i=0;i<4;i++)
    {
-      std::cerr << "   Usage: " << argv[0]
-                << " (no arguments needed)." << std::endl;
-      return 1;
+      switch(tag&0x000000FF)
+      {
+         case 'g':
+            swap += (i+1)*1000;
+            break;
+         case 'd':
+            swap += (i+1)*100;
+            break;
+         case 'c':
+            swap += (i+1)*10;
+            break;
+         case 'm':
+            swap += (i+1);
+            break;
+         default:
+            return 0;
+      }
+      tag >>= 8;
    }
-   
-   std::cout << "   Description (Test::TestAllReadCompareDicom): "
-             << std::endl;
-   std::cout << "   For all images in gdcmData (and not blacklisted in "
-                "Test/CMakeLists.txt)"
-             << std::endl;
-   std::cout << "   apply the following to each filename.xxx: "
-             << std::endl;
-   std::cout << "   step 1: parse the image (as gdcmHeader) and call"
-             << " IsReadable(). "
-             << std::endl;
-   std::cout << "   step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
-             << std::endl
-             << "           (with format DICOM V3, explicit Value"
-             << "Representation)"
-             << std::endl;
-   std::cout << "   step 3a: when image NOT found on step 2, write "
-             << std::endl
-             << "            GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
-             << std::endl
-             << "           (with format DICOM V3, explicit Value"
-             << "Representation)"
-             << std::endl;
-   std::cout << "   step 3b: when image found on step 2, and when IsReadable()"
-             << std::endl
-             << "            compare it (in memory with memcmp) with the"
-             << std::endl
-             << "            image we are testing (the one of step 1). "
-             << std::endl << std::endl;
+   return swap;
+}
 
-   int i = 0;
-   while( gdcmDataImages[i] != 0 )
+void TestFile::NewData(void)
+{
+   DeleteData();
+   if( GetDataSize() == 0 )
+      return;
+   data = new uint8_t[GetDataSize()];
+}
+
+void TestFile::DeleteData(void)
+{
+   if( data )
+      delete[] data;
+   data = NULL;
+}
+
+void TestFile::ReadFile(void)
+{
+   readable=true;
+   std::ifstream fp(fileName.c_str(),std::ios::in | std::ios::binary);
+
+   if(!fp)
    {
-      ////// Check for existence of reference baseline directory
+      readable=false;
+      return;
+   }
 
-      std::string baseLineDir = GDCM_DATA_ROOT;
-      baseLineDir += "/BaselineDicom/";
+   try
+   {
+      readable=ReadFileHeader(&fp);
+      if(!readable)
+      {
+         std::cout << "Problems when reading Header part" << std::endl;
+         fp.close();
+         return;
+      }
 
-      FILE* testFILE = fopen( baseLineDir.c_str(), "r" );
-      if (! testFILE )
+      readable=ReadFileData(&fp);
+      if(!readable)
       {
-        std::cerr << "   The reference baseline directory " << std::endl
-                  << "      "
-                  << baseLineDir << std::endl
-                  << "   couldn't be opened."
-                  << std::endl;
-        return 1;
+         std::cout << "Problems when reading datas" << std::endl;
+         fp.close();
+         return;
       }
-      else
-        fclose( testFILE );
+   }
+   catch(...)
+   {
+      readable=false;
+      fp.close();
+      return;
+   }
 
-      ////// Step 1 (see above description):
+   fp.close();
+}
 
-      std::string filename = GDCM_DATA_ROOT;
-      filename += "/";  //doh!
-      filename += gdcmDataImages[i];
-   
+bool TestFile::ReadFileHeader(std::ifstream *fp)
+{
+   uint32_t tag = ReadInt32(fp);
+   swapCode = GetSwapCode(tag);
+   if( swapCode == 0 )
+   {
+      std::cout << "TestFile: Bad tag - Must be 'gdcm'" << std::endl;
+      return(false);
+   }
+
+   sizeX = ReadInt32(fp); // Size X
+   sizeY = ReadInt32(fp); // Size Y
+   sizeZ = ReadInt32(fp); // Size Z
+   scalarSize = ReadInt16(fp)/8; // bits per scalar
+   components = ReadInt16(fp);   // Number of components
+
+   return(true);
+}
+
+bool TestFile::ReadFileData(std::ifstream *fp)
+{
+   DeleteData();
+
+   // Allocate datas
+   NewData();
+   if( !data )
+      return(false);
+
+   // Read datas
+   fp->read((char *)data,GetDataSize());
+
+   return(true);
+}
+
+void TestFile::WriteFile(void)
+{
+   std::ofstream fp(fileName.c_str(),std::ios::out | std::ios::binary);
+
+   if(!fp)
+   {
+      readable=false;
+      return;
+   }
+
+   WriteFileHeader(&fp);
+   WriteFileData(&fp);
+
+   fp.close();
+}
+
+bool TestFile::WriteFileHeader(std::ofstream *fp)
+{
+   WriteInt8(fp,'g'); // Bitmap tag - must be 'g'
+   WriteInt8(fp,'d'); // Bitmap tag - must be 'd'
+   WriteInt8(fp,'c'); // Bitmap tag - must be 'c'
+   WriteInt8(fp,'m'); // Bitmap tag - must be 'm'
+   WriteInt32(fp,sizeX); // Size X
+   WriteInt32(fp,sizeY); // Size Y
+   WriteInt32(fp,sizeZ); // Size Z
+   WriteInt16(fp,scalarSize*8); // bits per scalar
+   WriteInt16(fp,components);   // number of components
+
+   return(true);
+}
+
+bool TestFile::WriteFileData(std::ofstream *fp)
+{
+   fp->write((char *)data,GetDataSize());
+
+   return(true);
+}
+
+uint8_t  TestFile::ReadInt8 (std::ifstream *fp)
+#if !(__GNUC__==2  && __GNUC_MINOR__<=96)
+   throw( std::ios::failure )
+#endif
+{
+   uint8_t g;
+   fp->read ((char*)&g, (size_t)1);
+#if !(__GNUC__==2  && __GNUC_MINOR__<=96)
+   if ( fp->fail() )
+      throw std::ios::failure( "TestFile::ReadInt8() - file error." );
+   if( fp->eof() )
+      throw std::ios::failure( "TestFile::ReadInt8() - EOF." );
+#endif
+   return g;
+}
+
+uint16_t TestFile::ReadInt16(std::ifstream *fp)
+#if !(__GNUC__==2  && __GNUC_MINOR__<=96)
+   throw( std::ios::failure )
+#endif
+{
+   uint16_t g;
+   fp->read ((char*)&g, (size_t)2);
+#if !(__GNUC__==2  && __GNUC_MINOR__<=96)
+   if ( fp->fail() )
+      throw std::ios::failure( "TestFile::ReadInt16() - file error." );
+   if( fp->eof() )
+      throw std::ios::failure( "TestFile::ReadInt16() - EOF." );
+#endif
+
+#if defined(GDCM_WORDS_BIGENDIAN)
+   g = ( g << 8 |  g >> 8  );
+#endif
+   return g;
+}
+
+uint32_t TestFile::ReadInt32(std::ifstream *fp)
+#if !(__GNUC__==2  && __GNUC_MINOR__<=96)
+   throw( std::ios::failure )
+#endif
+{
+   uint32_t g;
+   fp->read ((char*)&g, (size_t)4);
+#if !(__GNUC__==2  && __GNUC_MINOR__<=96)
+   if ( fp->fail() )
+      throw std::ios::failure( "TestFile::ReadInt32() - file error." );
+   if( fp->eof() )
+      throw std::ios::failure( "TestFile::ReadInt32() - EOF." );
+#endif
+
+#if defined(GDCM_WORDS_BIGENDIAN)
+   g = (  (g<<24)               | ((g<<8)  & 0x00ff0000) | 
+       (  (g>>8)  & 0x0000ff00) |  (g>>24)               );
+#endif
+   return g;
+}
+
+void TestFile::WriteInt8 (std::ofstream *fp,uint8_t value)
+{
+   fp->write((char*)&value, (size_t)1);
+}
+
+void TestFile::WriteInt16(std::ofstream *fp,uint16_t value)
+{
+#if defined(GDCM_WORDS_BIGENDIAN)
+   value = ( value << 8 |  value >> 8  );
+#endif
+   fp->write((char*)&value, (size_t)2);
+}
+
+void TestFile::WriteInt32(std::ofstream *fp,uint32_t value)
+{
+#if defined(GDCM_WORDS_BIGENDIAN)
+   value = (  (value<<24)               | ((value<<8)  & 0x00ff0000) | 
+           (  (value>>8)  & 0x0000ff00) |  (value>>24)               );
+#endif
+   fp->write((char*)&value, (size_t)4);
+}
+
+int InternalTest(std::string const &filename, 
+                 std::string const &referenceFileName )
+{
       std::cout << "   Testing: " << filename << std::endl;
+      std::cout << "      ";
 
-      gdcmFile* tested = new gdcmFile( filename.c_str(), false, true );
-      if( !tested->GetHeader()->IsReadable() )
+      ////// Step 1:
+      std::cout << "1...";
+      gdcm::FileHelper *tested = new gdcm::FileHelper( filename );
+      if( !tested->GetFile()->IsReadable() )
       {
-        std::cout << "      Image not gdcm compatible:"
+        std::cout << " Failed" << std::endl
+                   << "      Image not gdcm compatible:"
                   << filename << std::endl;
         delete tested;
         return 1;
       }
 
       ////// Step 2:
-
       ////// Check for existence of reference baseline dicom file:
+      std::cout << "2...";
 
-      std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
-      std::string::size_type slash_pos = referenceFileName.rfind( "." );
-      if ( slash_pos != std::string::npos )
+      TestFile *reference = new TestFile();
+      std::ifstream refFile(referenceFileName.c_str(),
+                            std::ios::binary|std::ios::in);
+      if(!refFile)
       {
-         referenceFileName.replace( slash_pos + 1, 3, "dcm" );
+         std::cout << " Failed" << std::endl
+                   << "      Image not found:"
+                   << referenceFileName << std::endl;
+         reference->SetXSize(tested->GetFile()->GetXSize());
+         reference->SetYSize(tested->GetFile()->GetYSize());
+         reference->SetZSize(tested->GetFile()->GetZSize());
+         reference->SetScalarSize(tested->GetFile()->GetPixelSize());
+         reference->SetNumberOfComponents(tested->GetFile()->GetNumberOfScalarComponents());
+         reference->SetData(tested->GetImageData());
+         reference->Write(referenceFileName);
       }
+      else
+         refFile.close();
 
-      testFILE = fopen( referenceFileName.c_str(), "r" );
-      if (! testFILE )
+      reference->Load(referenceFileName);
+      if(!reference->IsReadable())
       {
-      ////// Step 3a:
-
-         int testedDataSize    = tested->GetImageDataSize();
-         void* testedImageData = tested->GetImageData(); // Kludge
-         tested->WriteDcmExplVR( referenceFileName );
-         std::cerr << "      Creating reference baseline file :" << std::endl
-                   << "      " << referenceFileName 
-                   << std::endl;
+        std::cout << " Failed" << std::endl
+                   << "      Image not Testing compatible:"
+                  << filename << std::endl;
+         delete reference;
          delete tested;
-         delete (char*)testedImageData;
-         continue; 
+         return 1;
       }
-      else
-         fclose( testFILE );
 
-      ////// When reference file is not gdcm readable test is failed:
-  
-      gdcmFile* reference = new gdcmFile( referenceFileName.c_str(),
-                                          false, true );
-      if( !reference->GetHeader()->IsReadable() )
+      ////// Step 3:
+      std::string PixelType = tested->GetFile()->GetPixelType();
+      std::cout << "3...";
+      int testedDataSize    = tested->GetImageDataSize();
+      uint8_t *testedImageData = tested->GetImageData();
+    
+      int    referenceDataSize = reference->GetDataSize();
+      uint8_t *referenceImageData = reference->GetData();
+
+      // Test the image size
+      if (tested->GetFile()->GetXSize() != reference->GetXSize() ||
+          tested->GetFile()->GetYSize() != reference->GetYSize() ||
+          tested->GetFile()->GetZSize() != reference->GetZSize())
       {
-         std::cout << "      Reference image " << std::endl
-                   << "      " << referenceFileName <<std::endl
-                   << "      is not gdcm compatible." << std::endl;
-         delete tested;
+         std::cout << "Failed" << std::endl
+                   << "        Size differs: "
+                   << "X: " << tested->GetFile()->GetXSize() << " # " 
+                   << reference->GetXSize() << " | "
+                   << "Y: " << tested->GetFile()->GetYSize() << " # " 
+                   << reference->GetYSize() << " | "
+                   << "Z: " << tested->GetFile()->GetZSize() << " # " 
+                   << reference->GetZSize() << std::endl;
          delete reference;
+         delete tested;
          return 1;
       }
 
-      ////// Step 3b:
-
-      int testedDataSize    = tested->GetImageDataSize();
-      void* testedImageData = tested->GetImageData();
-    
-      int    referenceDataSize = reference->GetImageDataSize();
-      void* referenceImageData = reference->GetImageData();
+      // Test the pixel size
+      if (tested->GetFile()->GetPixelSize() != reference->GetScalarSize() ||
+          tested->GetFile()->GetNumberOfScalarComponents() != reference->GetNumberOfComponents())
+      {
+         std::cout << "Failed" << std::endl
+                   << "        Pixel size differs: " << std::endl
+                   << "        Scalar size: " << tested->GetFile()->GetPixelSize() << " # " 
+                   << reference->GetScalarSize() << std::endl
+                   << "        Number of scalar: " << tested->GetFile()->GetNumberOfScalarComponents() << " # " 
+                   << reference->GetNumberOfComponents() << std::endl;
+         delete reference;
+         delete tested;
+         return 1;
+      }
 
+      // Test the data size
       if (testedDataSize != referenceDataSize)
       {
-         std::cout << "        Pixel areas lengths differ: "
+         std::cout << " Failed" << std::endl
+                   << "        pixel ("
+                   << PixelType
+                   <<") areas lengths differ: "
                    << testedDataSize << " # " << referenceDataSize
+                   << std::endl
+                   << "        Image size: ("
+                   << tested->GetFile()->GetXSize() << ","
+                   << tested->GetFile()->GetYSize() << ","
+                   << tested->GetFile()->GetZSize() << ")"
                    << std::endl;
          delete tested;
          delete reference;
-         delete (char*)testedImageData;
-         delete (char*)referenceImageData;
          return 1;
       }
 
+      // Test the data content
       if (int res = memcmp(testedImageData, referenceImageData,
                            testedDataSize) != 0 )
       {
-         std::cout << "        Pixel differ (as expanded in memory)."
+         (void)res;
+         std::cout << " Failed" << std::endl
+                   << "        pixel (" 
+                   << PixelType
+                   << ") differ (as expanded in memory)."
                    << std::endl;
          delete tested;
          delete reference;
-         delete (char*)testedImageData;
-         delete (char*)referenceImageData;
          return 1;
       }
-      std::cout << "      Passed." << std::endl ;
 
       //////////////// Clean up:
       delete tested;
       delete reference;
-      delete (char*)testedImageData;
-      delete (char*)referenceImageData;
+
+      std::cout << "OK." << std::endl;
+      
+      return 0;
+}
+
+int TestAllReadCompareDicom(int argc, char *argv[]) 
+{
+   if ( argc == 3 )
+   {
+      // The test is specified a specific filename, use it instead of looping
+      // over all images
+      const std::string input = argv[1];
+      const std::string reference = argv[2];
+      return InternalTest( input, reference );
+   }
+   else if ( argc > 3 || argc == 2 )
+   {
+      std::cerr << "   Usage: " << argv[0]
+                << " (no arguments needed)." << std::endl;
+      std::cerr << "or   Usage: " << argv[0]
+                << " filename.dcm reference.dcm" << std::endl;
+      return 1;
+   }
+   // else other cases:
+   
+   std::cout << "   Description (Test::TestAllReadCompareDicom): "
+             << std::endl;
+   std::cout << "   For all images in gdcmData (and not blacklisted in "
+                "Test/CMakeLists.txt)"
+             << std::endl;
+   std::cout << "   apply the following to each filename.xxx: "
+             << std::endl;
+   std::cout << "   step 1: parse the image (as gdcmFile) and call"
+             << " IsReadable(). "
+             << std::endl;
+   std::cout << "   step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
+             << std::endl
+             << "           (with format DICOM V3, explicit Value"
+             << "Representation)"
+             << std::endl;
+   std::cout << "   step 3a: when image NOT found on step 2, write "
+             << std::endl
+             << "            GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
+             << std::endl
+             << "           (with format DICOM V3, explicit Value"
+             << "Representation)"
+             << std::endl;
+   std::cout << "   step 3b: when image found on step 2, and when IsReadable()"
+             << std::endl
+             << "            compare it (in memory with memcmp) with the"
+             << std::endl
+             << "            image we are testing (the one of step 1). "
+             << std::endl << std::endl;
+
+   int i = 0;
+   int result = 0;
+   while( gdcmDataImages[i] != 0 )
+   {
+      ////// Check for existence of reference baseline directory
+
+      std::string baseLineDir = GDCM_DATA_ROOT;
+      baseLineDir += "/BaselineDicom";
+
+      if( !gdcm::DirList::IsDirectory(baseLineDir) )
+      {
+         std::cerr << "   The reference baseline directory " << std::endl
+                   << "      "
+                   << baseLineDir << std::endl
+                   << "   couldn't be opened."
+                   << std::endl;
+         return 1;
+      }
+
+      ////// Step 1 (see above description):
+      std::string filename = GDCM_DATA_ROOT;
+      filename += "/";
+      filename += gdcmDataImages[i];
+      
+      baseLineDir += '/';
+      std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
+      std::string::size_type slash_pos = referenceFileName.rfind( "." );
+      if( slash_pos != std::string::npos )
+      {
+         referenceFileName.replace( slash_pos + 1, 3, "tst" );
+      }
+
+      if( InternalTest( filename, referenceFileName ) != 0 )
+      {
+         result++;
+      }
    }
 
-   return 0;
+   return result;
 }