cmtkXformIO.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: 2412 $
00026 //
00027 //  $LastChangedDate: 2010-10-06 11:46:16 -0700 (Wed, 06 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkXformIO.h"
00034 #include <System/cmtkConsole.h>
00035 #include <System/cmtkFileUtils.h>
00036 #include <System/cmtkMountPoints.h>
00037 
00038 #include <IO/cmtkFileFormat.h>
00039 #include <IO/cmtkClassStream.h>
00040 #include <IO/cmtkClassStreamAffineXform.h>
00041 #include <IO/cmtkTypedStreamStudylist.h>
00042 #include <IO/cmtkAffineXformITKIO.h>
00043 
00044 namespace
00045 cmtk
00046 {
00047 
00050 
00051 Xform::SmartPtr 
00052 XformIO::Read( const char* path, const bool verbose )
00053 {
00054   const char* realPath = MountPoints::Translate( path );
00055   
00056   switch ( FileFormat::Identify( realPath ) ) 
00057     {
00058     case FILEFORMAT_NRRD: 
00059     {
00060 #ifdef CMTK_BUILD_NRRD
00061     return Self::ReadNrrd( realPath, verbose );
00062 #else
00063     StdErr << "ERROR: " << realPath << " is a Nrrd file, but Nrrd support is not enabled.\n"
00064            << "  Please re-configure software using either '--with-nrrd' or '--with-nrrd-teem' switch.\n";
00065     return Xform::SmartPtr( NULL );
00066 #endif
00067     }
00068     case FILEFORMAT_ITK_TFM:
00069       return AffineXformITKIO::Read( path );
00070     case FILEFORMAT_STUDYLIST: 
00071     {
00072     if ( verbose ) 
00073       {
00074       StdErr << "Reading transformation from studylist " << realPath << "\n";
00075       }
00076     
00077     TypedStreamStudylist studylist( realPath );
00078     if ( studylist.GetWarpXform() )
00079       return studylist.GetWarpXform();
00080     else
00081       return studylist.GetAffineXform();
00082     }
00083     case FILEFORMAT_TYPEDSTREAM: 
00084     {
00085     if ( verbose ) 
00086       {
00087       StdErr << "Reading transformation from typedstream file " << realPath << "\n";
00088       }
00089     
00090     ClassStream stream( realPath, ClassStream::READ );
00091     WarpXform* warpXform;
00092     stream >> warpXform;
00093     
00094     if ( warpXform ) 
00095       return Xform::SmartPtr( warpXform );
00096     
00097     stream.Open( realPath, ClassStream::READ );
00098     try
00099       {
00100       AffineXform affineXform;
00101       stream >> affineXform;
00102       return Xform::SmartPtr( new AffineXform( affineXform ) );
00103       }
00104     catch (...)
00105       {
00106       return Xform::SmartPtr( NULL );
00107       }
00108     }
00109     default:
00110       StdErr << "The file/directory " << realPath << " does not seem to be in a supported transformation format\n";
00111     }
00112   return Xform::SmartPtr( NULL );
00113 }
00114 
00115 void 
00116 XformIO::Write
00117 ( const Xform* xform, const char *path, const bool verbose )
00118 {
00119   FileFormatID fileFormat = FILEFORMAT_TYPEDSTREAM;
00120 
00121   const char* suffix = strrchr( path, '.' );
00122   if ( suffix )
00123     {
00124     if ( ! strcmp( ".nrrd", suffix ) || ! strcmp( ".nhdr", suffix ) )
00125       {
00126       fileFormat = FILEFORMAT_NRRD;
00127       }
00128     else
00129       {
00130       if ( ! strcmp( ".tfm", suffix ) || ! strcmp( ".txt", suffix ) )
00131         {
00132         fileFormat = FILEFORMAT_ITK_TFM;
00133         }      
00134       }
00135     }
00136   
00137   char absolutePath[PATH_MAX];
00138   FileUtils::GetAbsolutePath( absolutePath, path );
00139   
00140   switch ( fileFormat )
00141     {
00142     case FILEFORMAT_NRRD:
00143 #ifdef CMTK_BUILD_NRRD
00144       WriteNrrd( xform, absolutePath, verbose );
00145 #else
00146       StdErr << "ERROR: XformIO::Write -- Nrrd support not configured.\n";
00147 #endif
00148       break;
00149     case FILEFORMAT_ITK_TFM:
00150     {
00151     const AffineXform* affineXform = dynamic_cast<const AffineXform*>( xform );
00152     if ( affineXform )
00153       AffineXformITKIO::Write( path, *affineXform );
00154     break;
00155     }
00156     case FILEFORMAT_TYPEDSTREAM:
00157     {
00158     ClassStream stream( absolutePath, ClassStream::WRITE );
00159     
00160     const AffineXform* affineXform = dynamic_cast<const AffineXform*>( xform );
00161     if ( affineXform )
00162       stream << *affineXform;
00163     
00164     const SplineWarpXform* splineWarpXform = dynamic_cast<const SplineWarpXform*>( xform );
00165     if ( splineWarpXform )
00166       stream << *splineWarpXform;
00167     }
00168     break;
00169     default:
00170       break;
00171     }
00172 }
00173 
00174 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines