00001 /* 00002 // 00003 // Copyright 2010 SRI International 00004 // 00005 // This file is part of the Computational Morphometry Toolkit. 00006 // 00007 // http://www.nitrc.org/projects/cmtk/ 00008 // 00009 // The Computational Morphometry Toolkit is free software: you can 00010 // redistribute it and/or modify it under the terms of the GNU General Public 00011 // License as published by the Free Software Foundation, either version 3 of 00012 // the License, or (at your option) any later version. 00013 // 00014 // The Computational Morphometry Toolkit is distributed in the hope that it 00015 // will be useful, but WITHOUT ANY WARRANTY; without even the implied 00016 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License along 00020 // with the Computational Morphometry Toolkit. If not, see 00021 // <http://www.gnu.org/licenses/>. 00022 // 00023 // $Revision: 2398 $ 00024 // 00025 // $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $ 00026 // 00027 // $LastChangedBy: torstenrohlfing $ 00028 // 00029 */ 00030 00031 #include "cmtkImageOperationApplyMask.h" 00032 00033 #include <IO/cmtkVolumeIO.h> 00034 00035 cmtk::UniformVolume::SmartPtr 00036 cmtk::ImageOperationApplyMask 00037 ::Apply( cmtk::UniformVolume::SmartPtr& volume ) 00038 { 00039 const std::string maskOrientation = this->m_MaskVolume->m_MetaInformation[META_IMAGE_ORIENTATION]; 00040 const std::string workingOrientation = volume->m_MetaInformation[META_IMAGE_ORIENTATION]; 00041 if ( maskOrientation != workingOrientation ) 00042 { 00043 this->m_MaskVolume = cmtk::UniformVolume::SmartPtr( this->m_MaskVolume->GetReoriented( workingOrientation.c_str() ) ); 00044 } 00045 00046 for ( int dim = 0; dim < 3; ++dim ) 00047 { 00048 if ( this->m_MaskVolume->m_Dims[dim] != volume->m_Dims[dim] ) 00049 { 00050 cmtk::StdErr << "ERROR: mask volume dimensions do not match working volume dimensions.\n"; 00051 exit( 1 ); 00052 } 00053 } 00054 00055 const cmtk::TypedArray* maskData = this->m_MaskVolume->GetData(); 00056 cmtk::TypedArray::SmartPtr& volumeData = volume->GetData(); 00057 00058 const size_t nPixels = volume->GetNumberOfPixels(); 00059 for ( size_t i = 0; i < nPixels; ++i ) 00060 if ( maskData->IsPaddingOrZeroAt( i ) ) 00061 volumeData->SetPaddingAt( i ); 00062 return volume; 00063 } 00064 00065 cmtk::UniformVolume::SmartPtr 00066 cmtk::ImageOperationApplyMask 00067 ::ReadMaskFile( const char* maskFileName, const bool inverse ) 00068 { 00069 cmtk::UniformVolume::SmartPtr maskVolume( cmtk::VolumeIO::ReadOriented( maskFileName ) ); 00070 if ( !maskVolume || !maskVolume->GetData() ) 00071 { 00072 cmtk::StdErr << "ERROR: could not read mask from file " << maskFileName << "\nProgram will terminate now, just to be safe.\n"; 00073 exit( 1 ); 00074 } 00075 00076 // binarize mask to 1/0, convert to char, and also consider "inverse" flag in the process. 00077 cmtk::TypedArray::SmartPtr& maskData = maskVolume->GetData(); 00078 const size_t nPixels = maskData->GetDataSize(); 00079 for ( size_t n = 0; n < nPixels; ++n ) 00080 { 00081 if ( maskData->IsPaddingOrZeroAt( n ) == inverse ) 00082 maskData->Set( 1, n ); 00083 else 00084 maskData->Set( 0, n ); 00085 } 00086 maskVolume->SetData( cmtk::TypedArray::SmartPtr( maskData->Convert( cmtk::TYPE_BYTE ) ) ); 00087 00088 return maskVolume; 00089 }