cmtkImageRGB.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: 2464 $
00026 //
00027 //  $LastChangedDate: 2010-10-19 09:54:33 -0700 (Tue, 19 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torsten_at_home $
00030 //
00031 */
00032 
00033 #include <cmtkconfig.h>
00034 
00035 #include <Pipeline/cmtkImageRGB.h>
00036 
00037 namespace
00038 cmtk
00039 {
00040 
00043 
00044 ImageRGB *ImageRGB::New()
00045 {
00046   return new ImageRGB;
00047 }
00048 
00049 ImageRGB::ImageRGB()
00050 {
00051   Data = NULL;
00052   DataSize = 0;
00053   AlphaChannel = IMAGE_RGB;
00054   BytesPerPixel = 3;
00055 }
00056 
00057 ImageRGB::~ImageRGB()
00058 {
00059   Memory::DeleteArray( this->Data );
00060 }
00061 
00062 byte* 
00063 ImageRGB::GetDataPtr( const bool forceAlloc )
00064 {
00065   if ( ! forceAlloc ) return Data;
00066 
00067   if ( Data == NULL ) 
00068     {
00069     DataSize = BytesPerPixel * this->Plane::GetNumPixels();
00070     Data = Memory::AllocateArray<byte>( DataSize );
00071     } 
00072   else
00073     {
00074     if ( DataSize != (BytesPerPixel * this->Plane::GetNumPixels()) ) 
00075       {
00076       Memory::DeleteArray( Data );
00077       Data = NULL;
00078       return this->GetDataPtr( true /* forceAlloc */ );
00079       } 
00080     }
00081   return Data;
00082 }
00083 
00084 void ImageRGB::SetAlphaChannel( const ImageAlphaToggle alphaChannel, const bool convertData )
00085 {
00086   if ( alphaChannel != AlphaChannel ) 
00087     {
00088     AlphaChannel = alphaChannel;
00089     BytesPerPixel = (AlphaChannel == IMAGE_RGB) ? 3 : 4;
00090     
00091     byte *oldData = Data;
00092     // fake initialization by setting Data to NULL
00093     Data = NULL;
00094     this->GetDataPtr( true /* forceAlloc */ );
00095     
00096     // convert old image data
00097     if ( convertData ) 
00098       {
00099       byte *fromPtr = oldData;
00100       byte *toPtr = Data;
00101       unsigned int numberOfPixels = this->GetNumPixels();
00102       
00103       if ( AlphaChannel == IMAGE_RGB ) 
00104         {
00105         // from RGBA to RGB: remove alpha value
00106         for ( unsigned int i = 0; i < numberOfPixels; ++i, fromPtr += 4, toPtr += 3 ) 
00107           {
00108           toPtr[0] = fromPtr[0];
00109           toPtr[1] = fromPtr[1];
00110           toPtr[2] = fromPtr[2];
00111           }
00112         } 
00113       else
00114         {
00115         // from RGB to RGBA: set alpha to opaque
00116         for ( unsigned int i = 0; i < numberOfPixels; ++i, fromPtr += 3, toPtr += 4 ) 
00117           {
00118           toPtr[0] = fromPtr[0];
00119           toPtr[1] = fromPtr[1];
00120           toPtr[2] = fromPtr[2];
00121           toPtr[3] = 255;
00122           }
00123         }
00124       }
00125     
00126     Memory::DeleteArray( oldData );
00127     }
00128 }
00129 
00130 void ImageRGB::GetPixel( RGBA& rgb, const int index )
00131 {
00132   byte* pixelPtr = Data + (BytesPerPixel * index);
00133   rgb.R = pixelPtr[0];
00134   rgb.G = pixelPtr[1];
00135   rgb.B = pixelPtr[2];
00136   if ( AlphaChannel == IMAGE_RGBA )
00137     rgb.Alpha = pixelPtr[3];
00138   else
00139     rgb.Alpha = 255;
00140 }
00141 
00142 void ImageRGB::SetPixel( const int index, const RGBA& rgb )
00143 {
00144   byte* pixelPtr = Data + (BytesPerPixel * index);
00145   pixelPtr[0] = rgb.R;
00146   pixelPtr[1] = rgb.G;
00147   pixelPtr[2] = rgb.B;
00148   if ( AlphaChannel == IMAGE_RGBA )
00149     pixelPtr[3] = rgb.Alpha;
00150 }
00151 
00152 void ImageRGB::GetPixel( RGB& rgb, const int index )
00153 {
00154   byte* pixelPtr = Data + (BytesPerPixel * index);
00155   rgb.R = pixelPtr[0];
00156   rgb.G = pixelPtr[1];
00157   rgb.B = pixelPtr[2];
00158 }
00159 
00160 void ImageRGB::SetPixel( const int index, const RGB& rgb )
00161 {
00162   byte* pixelPtr = Data + (BytesPerPixel * index);
00163   pixelPtr[0] = rgb.R;
00164   pixelPtr[1] = rgb.G;
00165   pixelPtr[2] = rgb.B;
00166 }
00167 
00168 bool 
00169 ImageRGB::IsGreyscale() const
00170 {
00171   unsigned int numPixels = this->GetNumPixels();
00172   const byte *data = this->GetDataPtr();
00173   byte increment = (this->GetAlphaChannel() == IMAGE_RGB) ? 3 : 4;
00174 
00175   for ( unsigned int idx = 0; idx < numPixels; ++idx, data += increment ) 
00176     {
00177     if ( (data[0] != data[1]) || (data[1] != data[2]) )
00178       return 0;
00179     }
00180   
00181   return 1;
00182 }
00183 
00184 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines