cmtkVolumeFromFileMetaFile.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 #include <System/cmtkConsole.h>
00037 #include <Base/cmtkUniformVolume.h>
00038 
00039 #include <stdio.h>
00040 #include <string.h>
00041 #include <stdlib.h>
00042 
00043 namespace
00044 cmtk
00045 {
00046 
00049 
00050 void
00051 VolumeFromFile::WriteMetaImage
00052 ( const char* pathHdr, const UniformVolume& volume )
00053 {
00054   const TypedArray* data = volume.GetData();
00055   if ( ! data ) return;
00056 
00057 #ifdef _MSC_VER
00058   FILE *outfile = fopen( pathHdr, "wb" );
00059 #else
00060   FILE *outfile = fopen( pathHdr, "w" );
00061 #endif
00062 
00063   if ( ! outfile )
00064     {
00065     StdErr << "Could not open file " << pathHdr << " for writing.\n";
00066     return;
00067     }
00068 
00069   fprintf( outfile, "ObjectType = Image\n" );
00070   fprintf( outfile, "NDims = 3\n" );
00071   fprintf( outfile, "BinaryData = True\n" );
00072 #ifndef WORDS_BIGENDIAN
00073   fprintf( outfile, "BinaryDataByteOrderMSB = False\n" );
00074   fprintf( outfile, "ElementByteOrderMSB = False\n" );
00075 #else
00076   fprintf( outfile, "BinaryDataByteOrderMSB = True\n" );
00077   fprintf( outfile, "ElementByteOrderMSB = True\n" );
00078 #endif
00079   const AffineXform::MatrixType matrix = volume.GetImageToPhysicalMatrix();
00080   fprintf( outfile, "TransformMatrix = %lf %lf %lf %lf %lf %lf %lf %lf %lf\n", 
00081            (double)matrix[0][0], (double)matrix[0][1], (double)matrix[0][2], 
00082            (double)matrix[1][0], (double)matrix[1][1], (double)matrix[1][2], 
00083            (double)matrix[2][0], (double)matrix[2][1], (double)matrix[2][2] );
00084   fprintf( outfile, "Offset = %lf %lf %lf\n", (double)matrix[3][0], (double)matrix[3][1], (double)matrix[3][2] );
00085   fprintf( outfile, "CenterOfRotation = 0 0 0\n" );
00086   fprintf( outfile, "ElementSpacing = %f %f %f\n", volume.m_Delta[AXIS_X], volume.m_Delta[AXIS_Y], volume.m_Delta[AXIS_Z] );
00087   fprintf( outfile, "DimSize = %d %d %d\n", volume.m_Dims[AXIS_X], volume.m_Dims[AXIS_Y], volume.m_Dims[AXIS_Z] );
00088   fprintf( outfile, "AnatomicalOrientation = %s\n", volume.m_MetaInformation[META_SPACE].c_str() );
00089   fprintf( outfile, "ElementNumberOfChannels = 1\n" );
00090   
00091   fputs( "ElementType = ", outfile ) ;
00092   switch ( data->GetType() )
00093     {
00094     case TYPE_BYTE:
00095       fputs( "MET_UCHAR\n", outfile );
00096       break;
00097     case TYPE_CHAR:
00098       fputs( "MET_CHAR\n", outfile );
00099       break;
00100     case TYPE_SHORT:
00101       fputs( "MET_SHORT\n", outfile );
00102       break;
00103     case TYPE_USHORT:
00104       fputs( "MET_USHORT\n", outfile );
00105       break;
00106     case TYPE_INT:
00107       fputs( "MET_INT\n", outfile );
00108       break;
00109     case TYPE_UINT:
00110       fputs( "MET_UINT\n", outfile );
00111       break;
00112     case TYPE_FLOAT:
00113       fputs( "MET_FLOAT\n", outfile );
00114       break;
00115     case TYPE_DOUBLE:
00116       fputs( "MET_DOUBLE\n", outfile );
00117       break;
00118     default:
00119       fputs( "MET_UNKNOWN\n", outfile );
00120       break;      
00121     }
00122   fprintf( outfile, "ElementDataFile = LOCAL\n" );
00123 
00124 #ifdef __IGNORE__
00125   // re-arrange data in y-direction for maximum compatibility
00126   const int* dims = volume.GetDims();
00127   for ( int z = 0; z < dims[2]; ++z )
00128     {
00129     for ( int y = 0; y < dims[1]; ++y )
00130       {
00131       const void* dataPtr = data->GetDataPtr( volume.GetOffsetOf( 0, dims[1]-1-y, z ) );
00132       fwrite( dataPtr, data->GetItemSize(), dims[0], outfile );
00133       }
00134     }
00135 #endif
00136 
00137   fwrite( data->GetDataPtr(), data->GetItemSize(), data->GetDataSize(), outfile );
00138   fclose( outfile );
00139 }
00140 
00141 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines