Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "cmtkVolumeFromFile.h"
00034
00035 #include <System/cmtkCompressedStream.h>
00036
00037 namespace
00038 cmtk
00039 {
00040
00041 const UniformVolume::SmartPtr
00042 VolumeFromFile::ReadVanderbilt( const char *path )
00043 {
00044 FILE *fp = NULL;
00045 if ( ! (fp = fopen( path, "r" )) )
00046 return UniformVolume::SmartPtr( NULL );
00047
00048 int dims[3] = { 1, 1, 1 };
00049 double calib[3] = { 0, 0, 0 };
00050
00051 char orientation[] = "RAS";
00052
00053
00054 char line[96], key[32], value[64];
00055 while ( !feof( fp ) )
00056 {
00057 fgets( line, 96, fp );
00058 if ( 2 == sscanf( line, "%32[a-zA-Z ]:= %64[0-9.: ]", key, value ) )
00059 {
00060 if ( ! strcmp( key, "Columns " ) )
00061 {
00062 dims[0] = atoi( value );
00063 }
00064 else if ( ! strcmp( key, "Rows " ) )
00065 {
00066 dims[1] = atoi( value );
00067 }
00068 else if ( ! strcmp( key, "Slices " ) )
00069 {
00070 dims[2] = atoi( value );
00071 }
00072 else if ( ! strcmp( key, "Pixel size " ) )
00073 {
00074 sscanf( value, "%lf : %lf", calib, calib+1 );
00075 }
00076 else if ( ! strcmp( key, "Slice thickness " ) )
00077 {
00078 calib[2] = static_cast<Types::Coordinate>( atof( value ) );
00079 }
00080 }
00081 else
00082 {
00083 char axes[3];
00084 if ( 3 == sscanf( line, "Patient orientation := %c : %c : %c", &axes[0], &axes[1], &axes[2] ) )
00085 {
00086 const char *const translation = "PbcdeSgIijkRmnoAqLstuvwxyz";
00087 for ( int i = 0; i < 3; ++i )
00088 {
00089 orientation[i] = translation[axes[i]-'A'];
00090 }
00091 }
00092 }
00093 }
00094 fclose( fp );
00095
00096
00097 Types::Coordinate size[3];
00098 for ( int i = 0; i < 3; ++i )
00099 size[i] = static_cast<Types::Coordinate>( (dims[i] - 1) * calib[i] );
00100
00101
00102 UniformVolume::SmartPtr volume( new UniformVolume( DataGrid::IndexType( dims ), UniformVolume::CoordinateVectorType( size ) ) );
00103 volume->m_MetaInformation[META_IMAGE_ORIENTATION] = volume->m_MetaInformation[META_IMAGE_ORIENTATION_ORIGINAL] = orientation;
00104
00105
00106 char imageFilename[PATH_MAX], *lastSlash;
00107 strcpy( imageFilename, path );
00108 if ( (lastSlash = strrchr( imageFilename, '/' ) ) )
00109 ++lastSlash;
00110 else
00111 lastSlash = imageFilename;
00112 strcpy( lastSlash, "image.bin" );
00113
00114
00115 CompressedStream imageStream( imageFilename );
00116 TypedArray::SmartPtr data = TypedArray::Create( TYPE_SHORT, dims[0] * dims[1] * dims[2] );
00117 imageStream.Read( data->GetDataPtr(), data->GetItemSize(), data->GetDataSize() );
00118
00119 #ifndef WORDS_BIGENDIAN
00120
00121 data->ChangeEndianness();
00122 #endif
00123
00124
00125 volume->SetData( TypedArray::SmartPtr( data ) );
00126
00127 return volume;
00128 }
00129
00130 }