+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 )
+{