// ############################################################################ // // File: rawToVTK.cpp // Description: Converts a raw image file to // VTK format (vtkStructuredPoints). // Use a vtkStructuredPointsReader to // read the data. // Date: 2006-04-05 // Author: Erik Vidholm // // Compile instructions: // // g++: g++ rawToVTK.cpp -o rawToVTK // // Example of usage: // // $ rawToVTK myimage.raw myimage.vtk 256 256 256 1.0 1.0 2.5 BINARY // // ############################################################################ #include #include #include #include #include using namespace std; // min template T minOf(const T &a, const U &b) { return (a void byteSwapArray(T *arr, unsigned int arrlen) { int n = sizeof(T); unsigned char *p = (unsigned char*) arr; switch( n ) { case 1: return; case 2: for(unsigned int j=0; j void writeBE(ostream &os, T *ptr, unsigned int n) { if( isLittleEndian() ) { // copy and write chunks of 1 MB = 1048576 bytes unsigned int chunkByteSize=1048576; unsigned int chunkElems=chunkByteSize/sizeof(T); unsigned int elemsWritten=0; T *cpy = new T[chunkElems]; while( elemsWritten < n ) { chunkElems=minOf(n-elemsWritten, (unsigned int) (chunkByteSize/sizeof(T))); memcpy(cpy, ptr+elemsWritten, chunkElems*sizeof(T)); byteSwapArray(cpy, chunkElems); os.write((const char*) cpy, chunkElems*sizeof(T)); elemsWritten += chunkElems; } delete [] cpy; } else { os.write((const char*) ptr, sizeof(T)*n); } } // the write function template bool writeVTK(T *ptr, // data pointer const string &filename, // filename int W, int H, int D, // dimensions double sx = 1.0, // spacing (voxelsize) double sy = 1.0, double sz = 1.0, const string &mode="BINARY") // binary/ascii { ofstream os(filename.c_str(), ofstream::out | ofstream::binary); if( !os ) { cerr << "Unable to open file: " << filename << endl; return false; } os << "# vtk DataFile Version 3.0" << endl; os << "created by writeVTK" << endl; if( mode == "BINARY" || mode == "ASCII" ) os << mode << endl; else { cerr << "Unknown mode!" << endl; return false; } os << "DATASET STRUCTURED_POINTS" << endl; os << "DIMENSIONS " << W << " " << H << " " << D << endl; os << "ORIGIN 0.0 0.0 0.0" << endl; os << "SPACING " << sx << " " << sy << " " << sz << endl; os << "POINT_DATA " << W*H*D << endl; string typestring; if( typeid(T) == typeid(unsigned char) ) typestring = "unsigned_char"; else if( typeid(T) == typeid(char) ) typestring = "char"; else if( typeid(T) == typeid(unsigned short) ) typestring = "unsigned_short"; else if( typeid(T) == typeid(short) ) typestring = "short"; else if( typeid(T) == typeid(unsigned int) ) typestring = "unsigned_int"; else if( typeid(T) == typeid(int) ) typestring = "int"; else if( typeid(T) == typeid(float) ) typestring = "float"; else if( typeid(T) == typeid(double) ) typestring = "double"; else { cerr << "Error: Can not write " << typeid(T).type_info::name() << endl; return false; } os << "SCALARS image_data " << typestring << " 1" << endl; os << "LOOKUP_TABLE default" << endl; if( mode == "BINARY" ) { writeBE( os, ptr, W*H*D ); } else if( mode == "ASCII" ) { for(int z=0; z