00001 #include <assert.h>
00002 #include "synchronization/guid.h"
00003 #include "IGS_Image.h"
00004 #include <stdio.h>
00005
00006 #include "opengl/glos.h"
00007 #include <gl/gl.h>
00008
00009
00010
00011
00012
00013
00014
00015
00016 IGS_Image::IGS_Image():
00017 guid( GuidGenerator::GenerateNewGuid() ),
00018 semaphore( guid ),
00019 format( GL_LUMINANCE ),
00020 posX( 0 ),
00021 poxY( 0 ),
00022 sizeX( 0 ),
00023 sizeY( 0 ),
00024 type( GL_BYTE )
00025 {
00026 }
00027
00028
00029
00030
00031
00032
00033 IGS_Image::IGS_Image( const IGS_Image& right ):
00034 guid( GuidGenerator::GenerateNewGuid() ),
00035 semaphore( guid )
00036 {
00037 }
00038
00039
00040
00041
00042
00043
00044 IGS_Image::~IGS_Image()
00045 {
00046 }
00047
00048
00049
00050
00051
00052
00053 int IGS_Image::ComputeIndex( const int x, const int y ) const
00054 {
00055 assert( x < this->sizeX );
00056 assert( y < this->sizeY );
00057 assert( x >= 0 );
00058 assert( y >= 0 );
00059 return y * this->sizeX + x;
00060 }
00061
00062
00063
00064
00065
00066
00067 int IGS_Image::ComputeMaxIndex() const
00068 {
00069 return this->sizeX * this->sizeY;
00070 }
00071
00072
00073
00074
00075
00076
00077 void IGS_Image::Draw() const
00078 {
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 if( ( this->sizeX == 0 ) || ( this->sizeY == 0 ) )
00138 {
00139 return;
00140 }
00141
00142 GLenum errorcode;
00143 errorcode = glGetError();
00144
00145 int depth = 0;
00146
00147 ::glDepthFunc( GL_ALWAYS );
00148
00149 ::glMatrixMode( GL_PROJECTION );
00150 errorcode = ::glGetError();
00151 assert( errorcode == GL_NO_ERROR );
00152
00153 ::glPushMatrix();
00154 errorcode = ::glGetError();
00155 assert( errorcode == GL_NO_ERROR );
00156
00157 ::glLoadIdentity();
00158 errorcode = ::glGetError();
00159 assert( errorcode == GL_NO_ERROR );
00160
00161 ::glOrtho( 0, this->sizeX, 0, this->sizeY, 100, -100 );
00162 errorcode = ::glGetError();
00163 assert( errorcode == GL_NO_ERROR );
00164
00165 ::glMatrixMode( GL_MODELVIEW );
00166 errorcode = ::glGetError();
00167 assert( errorcode == GL_NO_ERROR );
00168
00169 ::glPushMatrix();
00170 errorcode = ::glGetError();
00171 assert( errorcode == GL_NO_ERROR );
00172
00173 ::glLoadIdentity();
00174 errorcode = ::glGetError();
00175 assert( errorcode == GL_NO_ERROR );
00176
00177 ::glRasterPos2d( static_cast< double >( this->posX ), static_cast< double >( this->poxY ) );
00178 errorcode = ::glGetError();
00179 assert( errorcode == GL_NO_ERROR );
00180
00181
00182
00183
00184 errorcode = ::glGetError();
00185 assert( errorcode == GL_NO_ERROR );
00186
00187
00188
00189 GLenum formatToUse = this->format;
00190 if( format == GL_DEPTH )
00191 {
00192 formatToUse = GL_LUMINANCE;
00193 }
00194
00195 this->semaphore.Lock();
00196 const void* const buffer = this->GetBuffer();
00197 if( buffer != NULL )
00198 {
00199 ::glDrawPixels
00200 (
00201 this->sizeX,
00202 this->sizeY,
00203 formatToUse,
00204 this->type,
00205 buffer
00206 );
00207 }
00208 this->semaphore.Unlock();
00209 errorcode = glGetError();
00210 assert( errorcode == GL_NO_ERROR );
00211
00212
00213
00214 ::glPopMatrix();
00215 errorcode = ::glGetError();
00216 assert( errorcode == GL_NO_ERROR );
00217
00218 ::glMatrixMode( GL_PROJECTION );
00219 errorcode = ::glGetError();
00220 assert( errorcode == GL_NO_ERROR );
00221
00222 ::glPopMatrix();
00223 errorcode = ::glGetError();
00224 assert( errorcode == GL_NO_ERROR );
00225 }
00226
00227
00228
00229
00230
00231
00232 int IGS_Image::GetSizeX() const
00233 {
00234 return this->sizeX;
00235 }
00236
00237
00238
00239
00240
00241
00242 int IGS_Image::GetSizeY() const
00243 {
00244 return this->sizeY;
00245 }
00246
00247
00248
00249
00250
00251
00252 void IGS_Image::Invalidate()
00253 {
00254 void*& buffer = this->GetBuffer();
00255 if( buffer != NULL )
00256 {
00257 delete[] buffer;
00258 }
00259 }
00260
00261
00262
00263
00264
00265
00266 void IGS_Image::ScreenCapture()
00267 {
00268 void* pixels = this->GetBuffer();
00269 if( pixels == NULL )
00270 {
00271 this->Initialize();
00272 pixels = this->GetBuffer();
00273 assert( pixels != NULL );
00274 }
00275 GLenum errorcode;
00276 errorcode = glGetError();
00277
00278 ::glReadBuffer( GL_BACK );
00279 errorcode = glGetError();
00280 assert( errorcode == GL_NO_ERROR );
00281
00282 GLenum formatToUse = this->format;
00283 if( format == GL_DEPTH )
00284 {
00285 formatToUse = GL_DEPTH_COMPONENT;
00286 }
00287
00288 ::glReadPixels
00289 (
00290 this->posX, this->poxY,
00291 this->sizeX, this->sizeY,
00292 formatToUse,
00293 this->type,
00294 pixels
00295 );
00296 errorcode = glGetError();
00297 if( errorcode != GL_NO_ERROR )
00298 {
00299 assert( errorcode == GL_NO_ERROR );
00300
00301
00302
00303
00304 }
00305 }
00306
00307
00308
00309
00310
00311
00312 void IGS_Image::SetFormat( const GLenum format )
00313 {
00314 this->format = format;
00315 }
00316
00317
00318
00319
00320
00321
00322 void IGS_Image::SetPosition( const int x, const int y )
00323 {
00324 this->posX = x;
00325 this->poxY = y;
00326 }
00327
00328
00329
00330
00331
00332
00333 void IGS_Image::SetResolution( const int xres, const int yres )
00334 {
00335 this->Invalidate();
00336 this->sizeX = xres;
00337 this->sizeY = yres;
00338 }
00339
00340
00341
00342
00343
00344
00345 void IGS_Image::SetType( const GLenum type )
00346 {
00347 this->type = type;
00348 this->Invalidate();
00349 }
00350
00351
00352
00353
00354
00355
00356 int IGS_Image::SizeOfType( const GLenum type )
00357 {
00358 switch( type )
00359 {
00360 case GL_UNSIGNED_BYTE:
00361 {
00362 return sizeof( GLubyte );
00363 }
00364 case GL_BYTE:
00365 {
00366 return sizeof( GLbyte );
00367 }
00368 case GL_BITMAP:
00369 {
00370 assert( false );
00371 return 0;
00372 }
00373 case GL_UNSIGNED_SHORT:
00374 {
00375 return sizeof( GLushort );
00376 }
00377 case GL_SHORT:
00378 {
00379 return sizeof( GLshort );
00380 }
00381 case GL_UNSIGNED_INT:
00382 {
00383 return sizeof( GLuint );
00384 }
00385 case GL_INT:
00386 {
00387 return sizeof( GLint );
00388 }
00389 case GL_FLOAT:
00390 {
00391 return sizeof( GLfloat );
00392 }
00393 default:
00394 {
00395 printf( "IGS_Image::SizeOfType - invalid type\n" );
00396 assert( false );
00397 return 0;
00398 }
00399 }
00400 }
00401
00402
00403
00404
00405
00406 bool IGS_Image::Valid() const
00407 {
00408 const void* buffer = this->GetBuffer();
00409 return ( buffer != NULL );
00410 }