cmtkVolumeFromStudy.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 2004-2010 SRI International
00004 //
00005 //  Copyright 1997-2009 Torsten Rohlfing
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 "cmtkVolumeFromStudy.h"
00034 
00035 #include <System/cmtkCompressedStream.h>
00036 #include <System/cmtkProgress.h>
00037 #include <System/cmtkConsole.h>
00038 #include <System/cmtkMountPoints.h>
00039 
00040 #include <IO/cmtkVolumeIO.h>
00041 #include <IO/cmtkVolumeFromFile.h>
00042 #include <IO/cmtkDICOM.h>
00043 
00044 #include <Base/cmtkVolume.h>
00045 #include <Base/cmtkLandmarkList.h>
00046 #include <Base/cmtkUniformVolume.h>
00047 
00048 #include <string.h>
00049 #include <limits.h>
00050 
00051 #ifdef DEBUG
00052 #  include <stdio.h>
00053 #endif
00054 
00055 #include <memory>
00056 
00057 namespace
00058 cmtk
00059 {
00060 
00063 
00064 const UniformVolume::SmartPtr
00065 VolumeFromStudy::Read
00066 ( const Study* study, const bool verbose )
00067 {
00068   if ( !study ) 
00069     return UniformVolume::SmartPtr( NULL );
00070 
00071   const StudyImageSet* studyImageSet = dynamic_cast<const StudyImageSet*>( study );
00072   if ( studyImageSet ) 
00073     {
00074     VolumeFromStudy vfs;    
00075     return vfs.AssembleVolume( studyImageSet );
00076     } 
00077   else
00078     return VolumeIO::Read( study->GetFileSystemPath(), verbose );
00079 }
00080 
00081 const UniformVolume::SmartPtr
00082 VolumeFromStudy::AssembleVolume( const StudyImageSet* study, const bool verbose )
00083 {
00084   UniformVolume::SmartPtr Result( NULL );
00085   
00086   if ( study->size() < 2 ) 
00087     return Result;
00088   
00089   try
00090     {
00091     if ( verbose )
00092       fprintf( stderr, "\rReading images from path %s ...\n", MountPoints::Translate( study->GetImageDirectory() ) );
00093     
00094     Progress::Begin( 0, study->size(), 1, "Volume image assembly" );
00095     
00096     unsigned int nextPlane = 0;
00097     StudyImageSet::const_iterator it = study->begin();
00098     while ( it != study->end() ) 
00099       {      
00100       if ( verbose )
00101         fprintf( stderr, "\r%s", it->c_str() );
00102       
00103       char fullpath[PATH_MAX];
00104       snprintf( fullpath, sizeof( fullpath ), "%s/%s", MountPoints::Translate( study->GetImageDirectory() ), it->c_str() );
00105       
00106       ScalarImage::SmartPtr image = ScalarImage::SmartPtr( DICOM::Read( fullpath ) );
00107 
00108       // TODO: when returning NULL here, we also should tell
00109       // VolumeFromSlices that we give up, so it can free its
00110       // temporary storage.
00111       if ( !image ) 
00112         return UniformVolume::SmartPtr( NULL );
00113 
00114       if ( ! nextPlane ) 
00115         {
00116         // special treatment for first image in sequence.
00117         if ( study->GetMultiFile() )
00118           InitSequence( image, study->size() );
00119         else
00120           InitSequence( image, study->m_Dims[AXIS_Z] );
00121         }
00122       
00123       const char *error = FillPlane( nextPlane, image );
00124       
00125       Progress::SetProgress( nextPlane );
00126       
00127       if ( error ) 
00128         {
00129         StdErr.printf( "ERROR: %s: %s\n", fullpath, error );
00130         return UniformVolume::SmartPtr( NULL );
00131         }
00132       
00133       ++it;
00134       }
00135     Progress::Done();
00136     
00137     Result = this->FinishVolume();
00138     
00139     TypedArray::SmartPtr data = Result->GetData();
00140     if ( data ) 
00141       {
00142       if ( study->GetPadding() && ! data->GetPaddingFlag() ) 
00143         {
00144         data->SetPaddingValue( study->GetPaddingValue() );
00145         }
00146       }
00147     }
00148   catch (...) 
00149     {
00150     }
00151 
00152   return Result;
00153 }
00154 
00155 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines