+//-->
+//--> WARNING :
+//--> The .tst files *must* be generated on a Little Endian based computer.
+//-->
+/**
+ * /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) --> ?!? 1 or 2 only in DICOM V3 !
+ * - 2 bytes : number of components per pixel (1,2,3) ---> 1 or 3 only in DICOMV3 !
+ * - n bytes : data
+ */
+class TestFile
+{
+public:
+ TestFile();
+ ~TestFile();
+
+ bool IsReadable() {return readable;}
+
+ int GetXSize() {return SizeX;}
+ int GetYSize() {return SizeY;}
+ int GetZSize() {return SizeZ;}
+
+ void SetXSize(int size) {SizeX = size;}
+ void SetYSize(int size) {SizeY = size;}
+ void SetZSize(int size) {SizeZ = size;}
+
+ int GetScalarSize() {return ScalarSize;}
+ void SetScalarSize(int size) {ScalarSize = size;}
+
+ int GetNumberOfComponents() {return Components;}
+ void SetNumberOfComponents(int size) {Components = size;}
+ int GetSwapCode() {return SwapCode;}
+
+ unsigned long GetDataSize() {return GetLineSize()*SizeY*SizeZ;}
+ uint8_t *GetData() {return Data;}
+ void SetData(const uint8_t *newData);
+
+ void Load(const std::string &filename);
+ void Write(const std::string &filename);
+
+private:
+ unsigned long GetLineSize() {return SizeX*ScalarSize*Components;}
+ int ComputeSwapCode(uint32_t tag);
+
+ void NewData();
+ void DeleteData();
+
+ void ReadFile();
+ bool ReadFileHeader(std::ifstream *fp);
+ bool ReadFileData(std::ifstream *fp);
+ void WriteFile();
+ 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 MAX_NUMBER_OF_DIFFERENCE = 10;
+const unsigned int TestFile::HEADER_SIZE = 20;
+
+TestFile::TestFile()
+{
+ fileName = "";
+ readable=false;
+
+ SizeX = 0;
+ SizeY = 0;
+ SizeZ = 0;
+ ScalarSize = 0;
+ Components = 0;
+ Data = NULL;
+
+ //SwapCode = 1234;
+}
+
+TestFile::~TestFile()
+{
+ 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::ComputeSwapCode(uint32_t tag)
+{
+// FIXME : 100 % useless method !
+// "gdcm" was written on disc byte per byte.
+// when you fread it, you'll get *always* "gdcm"
+// whatever the processor indianess is !
+
+ int swap = 0;
+ //std::cout << std::hex << "0x(" << tag << ")" << std::dec << std::endl;
+
+ for(int i=0;i<4;i++)
+ {
+ 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 << std::hex << "0x(" << tag << ")" << std::dec << tag << std::endl;
+ return swap;
+}
+
+void TestFile::NewData()
+{
+ DeleteData();
+ if( GetDataSize() == 0 )
+ return;
+ Data = new uint8_t[GetDataSize()];
+}
+
+void TestFile::DeleteData()
+{
+ if( Data )
+ delete[] Data;
+ Data = NULL;
+}
+
+void TestFile::ReadFile()
+{
+ readable=true;
+ std::ifstream fp(fileName.c_str(),std::ios::in | std::ios::binary);
+
+ if(!fp)
+ {
+ readable=false;
+ return;
+ }
+
+ try
+ {
+ readable=ReadFileHeader(&fp);
+ if(!readable)
+ {
+ std::cout << "Problems when reading Header part" << std::endl;
+ fp.close();
+ return;
+ }
+
+ readable=ReadFileData(&fp);
+ if(!readable)
+ {
+ std::cout << "Problems when reading data" << std::endl;
+ fp.close();
+ return;
+ }
+ }
+ catch(...)
+ {
+ readable=false;
+ fp.close();
+ return;
+ }
+
+ fp.close();
+}
+
+bool TestFile::ReadFileHeader(std::ifstream *fp)
+{
+ uint32_t tag = ReadInt32(fp);
+ SwapCode = ComputeSwapCode(tag);
+ if( SwapCode == 0 )
+ {
+ // We shall *never* come here!
+ 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; // bytes per scalar
+ Components = ReadInt16(fp); // Number of components
+
+ return(true);
+}
+
+bool TestFile::ReadFileData(std::ifstream *fp)
+{
+ DeleteData();
+
+ // Allocate data
+ NewData();
+ if( !Data )
+ return(false);
+
+ // Read data Note : .tst images are *always* created
+ // on little endian processor !
+ fp->read((char *)Data,GetDataSize());
+
+ // Track BigEndian troubles
+ std::cout << " ScalarSize : " << GetScalarSize()
+ << " IsCurrentProcessorBigEndian:"
+ << GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian()
+ << std::endl;
+
+ //if (GetScalarSize() == 1 || GetSwapCode() == 1234)
+ if (GetScalarSize() == 1 || !GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian() )
+ {
+ return true;
+ }
+ // We *know* the .tst files are written in 'Little Endian' format.
+ // We *know* DataSize may be 1 or 2 !
+ uint16_t g;
+
+ std::cout << " Let's swap Pixels" <<std::endl;
+
+ for (unsigned int i=0; i<GetDataSize()/2; i++)
+ {
+ g = ((uint16_t *)Data)[i];
+ g = ( g << 8 | g >> 8 );
+ ((uint16_t *)Data)[i] = g;
+ }
+ return(true);
+}
+
+void TestFile::WriteFile()
+{
+ 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'
+
+ // FIXME : Think of writting an int32, better !
+ // (('g' << 8 + 'd') << 8 + 'c') + 'm'
+ // if you want to use it to check the endianess.
+ // (and upload again *all* the .tst files ...)
+ 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 )