cmtkImageToImageRGB.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/cmtkImageToImageRGB.h>
00034 
00035 namespace
00036 cmtk
00037 {
00038 
00041 
00042 ImageToImageRGB::ImageToImageRGB() :
00043   CheckerboxPadding( true )
00044 {
00045   this->m_Image = NULL;
00046   this->m_Colormap = NULL;
00047   AlphaMode = AlphaModeNone;
00048 
00049   this->RegisterInput( &this->m_Image );
00050   this->RegisterInput( &this->m_Colormap );
00051 }
00052 
00053 ImageToImageRGB::~ImageToImageRGB()
00054 {
00055   if ( this->m_Image ) 
00056     this->m_Image->Unregister();
00057   if ( this->m_Colormap ) 
00058     this->m_Colormap->Unregister();
00059 }
00060 
00061 void ImageToImageRGB::SetInput( Image *const image )
00062 {
00063   this->ReplaceObject( this->m_Image, image );
00064 }
00065 
00066 void ImageToImageRGB::SetColormap( Colormap *const colormap )
00067 {
00068   this->ReplaceObject( this->m_Colormap, colormap );
00069 }
00070 
00071 void ImageToImageRGB::Execute()
00072 {
00073   if ( (this->m_Image == NULL) || (this->m_Colormap == NULL) ) return;
00074   const TypedArray *inPtr = this->m_Image->GetData(); 
00075   if ( !inPtr ) return;
00076 
00077   ImageRGB *output = this->GetOutput();
00078   output->CopyStructure( this->m_Image );
00079 
00080   if ( AlphaMode == AlphaModeNone )
00081     output->SetAlphaChannel( IMAGE_RGB );
00082   else
00083     output->SetAlphaChannel( IMAGE_RGBA );
00084   
00085   void *outPtr = output->GetDataPtr( true /* forceAlloc */ );
00086 
00087   switch ( AlphaMode ) 
00088     {
00089     case AlphaModeNone:
00090       this->m_Colormap->Apply( outPtr, inPtr );
00091       if ( inPtr->GetPaddingFlag() )
00092         this->MarkPaddingData( output->GetDims( AXIS_X ), output->GetDims( AXIS_Y ), static_cast<RGB*>( outPtr ), inPtr );
00093       break;
00094     case AlphaModeConst:
00095       this->m_Colormap->Apply( outPtr, inPtr, true /* generateAlpha */ );
00096       if ( inPtr->GetPaddingFlag() )
00097         this->MarkPaddingData( output->GetDims( AXIS_X ), output->GetDims( AXIS_Y ), static_cast<RGBA*>( outPtr ), inPtr );
00098       break;
00099     }
00100   
00101   this->UpdateExecuteTime();
00102 }
00103 
00104 template<class T> 
00105 void
00106 ImageToImageRGB::MarkPaddingData
00107 ( const unsigned int dimsx, const unsigned int dimsy, T *const rgba, const TypedArray* data ) const
00108 {
00109   T* p = rgba;
00110   unsigned int idx = 0;
00111 
00112   byte bright = 170;
00113   byte dark = 80;
00114   if ( !this->CheckerboxPadding )
00115     bright = dark = 0;
00116   
00117   for ( unsigned int y = 0; y < dimsy; ++y ) 
00118     {
00119     for ( unsigned int x = 0; x < dimsx; ++x, ++idx, ++p ) 
00120       {
00121       if ( data->PaddingDataAt( idx ) ) 
00122         {
00123         if ( ((x >> 4)&1) ^ ((y >> 4)&1) ) 
00124           {
00125           p->R = p->G = p->B = bright;
00126           } 
00127         else
00128           {
00129           p->R = p->G = p->B = dark;
00130           }
00131         }
00132       }
00133     }
00134 }
00135 
00136 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines