basic/geometry/IGS/IGS_ImageFloat3.cpp

Go to the documentation of this file.
00001 #include <assert.h>
00002 #include <direct.h>
00003 #include "IGS_ImageFloat1.h" 
00004 #include "IGS_ImageFloat3.h" 
00005 
00006 static int gNumberOfChannels = 3;
00007 
00008 //=============================================================================
00009 // constructor
00010 //
00011 // Description: constructor
00012 //=============================================================================
00013 IGS_ImageFloat3::IGS_ImageFloat3():
00014         pixels( NULL )
00015 {
00016         IGS_Image::SetType( GL_FLOAT );
00017         IGS_Image::SetFormat( GL_RGB );
00018 }
00019 
00020 //=============================================================================
00021 // Destructor
00022 //
00023 // Description: destructor
00024 //=============================================================================
00025 IGS_ImageFloat3::~IGS_ImageFloat3()
00026 {
00027         this->Invalidate();
00028 }
00029 
00030 //=============================================================================
00031 // FileLoad
00032 //
00033 // Description: load the file from disk
00034 //=============================================================================
00035 void IGS_ImageFloat3::FileLoad( const char* filename )
00036 {
00037         char directoryName[ 256 ] = "";
00038         ::_getcwd( directoryName, 256 );
00039         FILE* infile = fopen( filename, "rb" );
00040         assert( infile != NULL );
00041         char header[ 9 ] = "";
00042         fread( header, sizeof( char ), 8, infile );
00043         fread( &sizeX, sizeof( int ), 1, infile );
00044         fread( &sizeY, sizeof( int ), 1, infile );
00045         this->Initialize();
00046         int size = this->ComputeMaxIndex();
00047         fread( pixels, sizeof( float ), size * 3, infile );
00048 }
00049 
00050 //=============================================================================
00051 // FileSave
00052 //
00053 // Description: saves the image to disk
00054 //=============================================================================
00055 void IGS_ImageFloat3::FileSave( const char* filename ) const
00056 {
00057         FILE* outfile = fopen( filename, "wb" );
00058         char header[] = "IGS_FL3X";
00059         fwrite( header, strlen( header ), 1, outfile );
00060         fwrite( &sizeX, sizeof( int ), 1, outfile );
00061         fwrite( &sizeY, sizeof( int ), 1, outfile );
00062         int size = this->ComputeMaxIndex();
00063         fwrite( pixels, sizeof( float ), size * 3, outfile );
00064         fclose( outfile );
00065 }
00066 
00067 //=============================================================================
00068 // GetBuffer
00069 //
00070 // Description: gets a pointer to the underlying buffer
00071 //=============================================================================
00072 const void* const IGS_ImageFloat3::GetBuffer() const
00073 {
00074         return this->pixels;
00075 }
00076 
00077 //=============================================================================
00078 // GetBuffer
00079 //
00080 // Description: gets a pointer to the underlying buffer
00081 //=============================================================================
00082 void*& IGS_ImageFloat3::GetBuffer()
00083 {
00084         return reinterpret_cast< void*& >( this->pixels );
00085 }       
00086 
00087 //=============================================================================
00088 // GetBuffer
00089 //
00090 // Description: gets a pointer to the underlying buffer
00091 //=============================================================================
00092 float& IGS_ImageFloat3::GetChannel( int channelNum, int x, int y )
00093 {
00094         int index = IGS_Image::ComputeIndex( x, y );
00095         index *= gNumberOfChannels;
00096         index += channelNum;
00097         return this->pixels[ index ];
00098 }
00099 
00100 //=============================================================================
00101 // GetBuffer
00102 //
00103 // Description: gets a pointer to the underlying buffer
00104 //=============================================================================
00105 const float& IGS_ImageFloat3::GetChannel( int channelNum, int x, int y ) const
00106 {
00107         int index = IGS_Image::ComputeIndex( x, y );
00108         index *= gNumberOfChannels;
00109         index += channelNum;
00110         return this->pixels[ index ];
00111 }
00112 
00113 //=============================================================================
00114 // GetPixel
00115 //
00116 // Description: initializes the image
00117 //=============================================================================
00118 IGS_Color IGS_ImageFloat3::GetPixel( const int x, const int y ) const
00119 {
00120         const float& r = this->GetChannel( 0, x, y );
00121         const float& g = this->GetChannel( 1, x, y );
00122         const float& b = this->GetChannel( 2, x, y );
00123         return IGS_Color( r, g, b );
00124 }
00125 
00126 //=============================================================================
00127 // Initialize
00128 //
00129 // Description: initializes the image
00130 //=============================================================================
00131 void IGS_ImageFloat3::Initialize()
00132 {
00133         this->semaphore.Lock();
00134         int sizeOfData = IGS_Image::SizeOfType( this->type );
00135         if( pixels != NULL )
00136         {
00137                 delete[] pixels;
00138         }
00139         pixels = new float[ this->sizeX * this->sizeY * sizeOfData * 3 ];
00140         this->semaphore.Unlock();
00141 }
00142 
00143 //=============================================================================
00144 // operator *=
00145 //
00146 // Description: multiplies all the pixel values in the image by the other image
00147 //=============================================================================
00148 IGS_ImageFloat3& IGS_ImageFloat3::operator *=( const IGS_ImageFloat1& right )
00149 {
00150         int i;
00151         for( i = 0; i < this->sizeX; i++ )
00152         {
00153                 int j;
00154                 for( j = 0; j < this->sizeY; j++ )
00155                 {
00156                         this->GetChannel( 0, i, j ) *= right.GetPixel( i, j );
00157                         this->GetChannel( 1, i, j ) *= right.GetPixel( i, j );
00158                         this->GetChannel( 2, i, j ) *= right.GetPixel( i, j );
00159                 }
00160         }
00161         return *this;
00162 }
00163 
00164 //=============================================================================
00165 // operator =
00166 //
00167 // Description: assignment operator
00168 //=============================================================================
00169 IGS_ImageFloat3& IGS_ImageFloat3::operator =( const IGS_ImageFloat3& right )
00170 {
00171         if( ( this->GetSizeX() != right.GetSizeX() ) || ( this->GetSizeY() != right.GetSizeY() ) )
00172         {
00173                 //need to resize this image
00174                 this->SetResolution( right.GetSizeX(), right.GetSizeY() );
00175                 this->Initialize();
00176         }
00177         
00178         int size = this->ComputeMaxIndex() * sizeof( float ) * 3;
00179         memcpy( this->pixels, right.pixels, size );
00180         return *this;
00181 }
00182 
00183 //=============================================================================
00184 // operator =
00185 //
00186 // Description: assignment operator for a color - sets all the pixels to this color
00187 //=============================================================================
00188 IGS_ImageFloat3& IGS_ImageFloat3::operator =( const IGS_Color& color )
00189 {
00190         int i;
00191         for( i = 0; i < this->GetSizeX(); i++ )
00192         {
00193                 int j;
00194                 for( j = 0; j < this->GetSizeY(); j++ )
00195                 {
00196                         this->PutPixel( i, j, color );
00197                 }
00198         }
00199         return *this;
00200 }
00201 
00202 //=============================================================================
00203 // PutPixel
00204 //
00205 // Description: puts a pixel of a specific color in a specific place in the 
00206 //                              image
00207 //=============================================================================
00208 void IGS_ImageFloat3::PutPixel( const int x, const int y, const IGS_Color& color )
00209 {
00210         this->GetChannel( 0, x, y ) = static_cast< float >( color.r );
00211         this->GetChannel( 1, x, y ) = static_cast< float >( color.g );
00212         this->GetChannel( 2, x, y ) = static_cast< float >( color.b );
00213 }

Generated on Sat Apr 1 21:30:32 2006 for Motion Planning Kernel by  doxygen 1.4.6-NO