cmtkImage.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 <Pipeline/cmtkImage.h>
00034 
00035 namespace
00036 cmtk
00037 {
00038 
00041 
00042 Image::Image () :
00043   Data( NULL )
00044 {
00045   DataType = TYPE_NONE;
00046 }
00047 
00048 TypedArray::SmartPtr
00049 Image::GetData()
00050 {
00051   if ( ! Data ) 
00052     {
00053     if ( DataType == TYPE_NONE )
00054       return TypedArray::SmartPtr( NULL );
00055     else
00056       {
00057       Data = TypedArray::SmartPtr( TypedArray::Create( DataType, Dims[0] * Dims[1] ) );
00058       this->UpdateModifiedTime();
00059       }
00060     } 
00061   else
00062     {
00063     if ( ( Data->GetType() != DataType ) || ( Data->GetDataSize() != (Dims[0] * Dims[1]) ) ) 
00064       {
00065       Data = TypedArray::SmartPtr( NULL );
00066       this->UpdateModifiedTime();
00067       return this->GetData();
00068       } 
00069     }
00070   return Data;
00071 }
00072 
00073 void
00074 Image::SetData( TypedArray::SmartPtr& data )
00075 {
00076   Data = data;
00077   if ( Data ) 
00078     DataType = Data->GetType();
00079   this->UpdateModifiedTime();
00080 }
00081 
00082 void
00083 Image::SetFromScalarImage
00084 ( ScalarImage *const scalarImage, const bool copyPixelData )
00085 {
00086   if ( scalarImage ) 
00087     {
00088     this->SetDims( scalarImage->GetDims()[0], scalarImage->GetDims()[1] );
00089     TypedArray::SmartPtr pixelData = scalarImage->GetPixelData();
00090     if ( copyPixelData )
00091       pixelData = TypedArray::SmartPtr( pixelData->Clone() );
00092     this->SetData( pixelData );
00093     this->SetSpacing( scalarImage->GetPixelSize() );
00094     this->SetOrigin( scalarImage->GetImageOrigin().begin() );
00095     this->SetDirectionX( scalarImage->GetImageDirectionX().begin() );
00096     this->SetDirectionY( scalarImage->GetImageDirectionY().begin() );
00097     this->UpdateModifiedTime();
00098     }
00099 }
00100 
00101 void
00102 Image::SetFromScalarImage
00103 ( const ScalarImage* scalarImage )
00104 {
00105   if ( scalarImage )
00106     {
00107     this->SetDims( scalarImage->GetDims()[0], scalarImage->GetDims()[1] );
00108     TypedArray::SmartPtr pixelData = scalarImage->GetPixelData();
00109     if ( pixelData )
00110       pixelData = TypedArray::SmartPtr( pixelData->Clone() );
00111     this->SetData( pixelData );
00112     this->SetSpacing( scalarImage->GetPixelSize() );
00113     this->SetOrigin( scalarImage->GetImageOrigin().begin() );
00114     this->SetDirectionX( scalarImage->GetImageDirectionX().begin() );
00115     this->SetDirectionY( scalarImage->GetImageDirectionY().begin() );
00116     this->UpdateModifiedTime();
00117     }
00118 }
00119 
00120 ScalarImage* 
00121 Image::GetScalarImage() const
00122 {
00123   ScalarImage* scalarImage = new ScalarImage( Dims[0], Dims[1] );
00124 
00125   scalarImage->SetPixelData( Data );
00126   scalarImage->SetPixelSize( Spacing );
00127   scalarImage->SetImageOrigin( FixedVector<3,Types::Coordinate>( Origin ) );
00128   scalarImage->SetImageDirectionX( FixedVector<3,Types::Coordinate>( DirectionX ) );
00129   scalarImage->SetImageDirectionY( FixedVector<3,Types::Coordinate>( DirectionY ) );
00130 
00131   return scalarImage;
00132 }
00133 
00134 double
00135 Image::GetDataAt( const int x, const int y, const double def )
00136 {
00137   const TypedArray* data = this->GetData();
00138 
00139   Types::DataItem result;
00140   if ( data->Get( result, x+Dims[0]*y ) ) 
00141     {
00142     return result;
00143     } 
00144   else
00145     {
00146     return def;
00147     }
00148 }
00149 
00150 double
00151 Image::GetDataAt( const int index, const double def )
00152 {
00153   const TypedArray *data = this->GetData();
00154 
00155   Types::DataItem result;
00156   if ( data->Get( result, index ) ) 
00157     {
00158     return result;
00159     } 
00160   else
00161     {
00162     return def;
00163     }
00164 }
00165 
00166 void
00167 Image::SetDataAt( const int x, const int y, const double value )
00168 {
00169   this->GetData()->Set( value, x+Dims[0]*y );
00170 }
00171 
00172 void
00173 Image::SetDataAt( const int index, const double value )
00174 {
00175   this->GetData()->Set( value, index );
00176 }
00177 
00178 double
00179 Image::GetDataAt( const double x, const double y, const double def )
00180 {
00181   const TypedArray *data = this->GetData();
00182 
00183   const unsigned int idxX = static_cast<int>( x / Spacing[0] );
00184   const unsigned int idxY = static_cast<int>( y / Spacing[1] );
00185 
00186   if ( (idxX > Dims[0]-2) || (idxY > Dims[1]-2) )
00187     return def;
00188 
00189   int offset = idxX + Dims[0] * idxY;
00190   
00191   Types::DataItem result[4];
00192   if ( ! data->Get( result[0], offset ) ) 
00193     {
00194     return def;
00195     }
00196   if ( ! data->Get( result[1], offset + 1 ) ) 
00197     {
00198     return def;
00199     }
00200   if ( ! data->Get( result[2], offset + Dims[0] ) ) 
00201     {
00202     return def;
00203     }
00204   if ( ! data->Get( result[3], offset + Dims[0] + 1 ) ) 
00205     {
00206     return def;
00207     }
00208   
00209   const double relX = ( x - idxX * Spacing[0] ) / Spacing[0];
00210   const double relY = ( y - idxY * Spacing[1] ) / Spacing[1];
00211 
00212   return (1-relY) * ( (1-relX) * result[0] + (relX) * result[1] ) + (relY) * ( (1-relX) * result[2] + (relX) * result[3] );
00213 }
00214 
00215 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines