cmtkVolumeFromFileVanderbilt.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2398 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
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   // parse header file for image dimensios etc.
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   // convert pixel dimensions to volume dimensions.
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   // create volume, for the time being with empty data array.
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   // generate image filename from header file path.
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   // open image file and read data.
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   // change endianness from Sun to whatever we're currently on.
00121   data->ChangeEndianness();
00122 #endif
00123   
00124   // set volume data array with what we just read.
00125   volume->SetData( TypedArray::SmartPtr( data ) );
00126   
00127   return volume;
00128 }
00129 
00130 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines