![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsRead3DRaw.hpp 00003 00004 Create 3-dimensional array from a .raw format file. 00005 This .raw format stores 8-bit binary data for xyz position. 00006 00007 SUKITTI PUNAK (04/28/2007) 00008 UPDATE (04/28/2007) 00009 ******************************************************************************/ 00010 #include "TAPsRead3DRaw.hpp" 00011 // Using Inclusion Model (i.e. definitions are included in declarations) 00012 // (this name.cpp is included in name.hpp) 00013 // Each friend is defined directly inside its declaration. 00014 00015 BEGIN_NAMESPACE_TAPs 00016 //============================================================================= 00017 //----------------------------------------------------------------------------- 00018 // Display the contents of an object 00019 template <typename T1, typename T2> 00020 bool Read3DRaw::ReadFile ( 00021 const char * fileName, // I/P: file name .raw 00022 int sizeX, int sizeY, int sizeZ, // I/P: 3D data size X x Y x Z 00023 T2 * const pr3DScalar // O/P: 3D array of type T 00024 ) // Output space must already be allocated 00025 { 00026 //---------------------------------------------------------------- 00027 // Open the input file 00028 std::ifstream inFile( fileName, std::ios::binary ); 00029 if ( !inFile ) { 00030 #ifdef TAPs_USE_WXWIDGETS 00031 wxLogError( wxT( "Can't input file \"%s\"" ), fileName ); 00032 #else 00033 std::cerr << "ERROR: Can't open input file \"" << fileName << "\"!" << std::endl; 00034 std::perror( fileName ); 00035 #endif 00036 return false; 00037 } 00038 int i; 00039 int dataSize = sizeof( T1 ); 00040 char * data = new char[dataSize]; 00041 int fileSize = sizeX*sizeY*sizeZ; 00042 for ( i = 0; i < fileSize; ++i ) { 00043 if ( !inFile.eof() ) { 00044 for ( int n = 0; n < dataSize; ++n ) { 00045 inFile.get( data[n] ); 00046 //if ( (unsigned int)(T2)(data[0]) == 213 ) 00047 // std::cout << i+1 << ": " << (unsigned int)(T2)(data[0]) << std::endl; 00048 } 00049 pr3DScalar[i] = static_cast<T1>( *data ); 00050 //if ( (unsigned int)(T2)(*data) > 0 ) 00051 // std::cout << i << ":- " << (unsigned int)(T2)(*data) << std::endl; 00052 //if ( (unsigned int)pr3DScalar[i] > 0 ) 00053 // std::cout << i << ":-< " << (unsigned int)pr3DScalar[i] << "\n"; 00054 } 00055 else { 00056 break; 00057 } 00058 } 00059 //for ( int a = 0; a < fileSize; ++a ) { 00060 // if ( (unsigned int)pr3DScalar[a] > 0 ) 00061 // std::cout << a+1 << ": " << (unsigned int)pr3DScalar[a] << "\n"; 00062 //} 00063 delete [] data; 00064 //----------------------------------------------------- 00065 // Check if the file size is smaller than the specified size 00066 fileSize *= dataSize; 00067 if ( i < fileSize ) { 00068 #ifdef TAPs_USE_WXWIDGETS 00069 wxLogError( wxT( "The file size is SMALLER than %d!" ), fileSize ); 00070 #else 00071 std::cerr << "ERROR: The file size is SMALLER than " << fileSize << "!" << std::endl; 00072 std::perror( fileName ); 00073 #endif 00074 return false; 00075 } 00076 //----------------------------------------------------- 00077 // Check if the file size is bigger than the specified size 00078 inFile.get(); 00079 if ( !inFile.eof() ) { 00080 #ifdef TAPs_USE_WXWIDGETS 00081 wxLogError( wxT( "The file size is BIGGER than %d!" ), fileSize ); 00082 #else 00083 std::cerr << "ERROR: The file size is BIGGER than " << fileSize << "!" << std::endl; 00084 std::perror( fileName ); 00085 #endif 00086 return false; 00087 } 00088 //----------------------------------------------------- 00089 std::cout << "File Size Read: " << i << " * " << sizeof( T1 ) 00090 << " = " << i * sizeof( T1 ) << std::endl; 00091 //----------------------------------------------------- 00092 inFile.close(); 00093 //----------------------------------------------------- 00094 return true; 00095 } 00096 //----------------------------------------------------------------------------- 00097 //============================================================================= 00098 END_NAMESPACE_TAPs 00099 //----------------------------------------------------------------------------- 00100 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00101 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----