GLTexImage.cpp
Go to the documentation of this file.
00001 
00002 //      File:           GLTexImage.cpp
00003 //      Author:         Changchang Wu
00004 //      Description : implementation of the GLTexImage class.
00005 //
00006 //
00007 //
00008 //      Copyright (c) 2007 University of North Carolina at Chapel Hill
00009 //      All Rights Reserved
00010 //
00011 //      Permission to use, copy, modify and distribute this software and its
00012 //      documentation for educational, research and non-profit purposes, without
00013 //      fee, and without a written agreement is hereby granted, provided that the
00014 //      above copyright notice and the following paragraph appear in all copies.
00015 //      
00016 //      The University of North Carolina at Chapel Hill make no representations
00017 //      about the suitability of this software for any purpose. It is provided
00018 //      'as is' without express or implied warranty. 
00019 //
00020 //      Please send BUG REPORTS to ccwu@cs.unc.edu
00021 //
00023 
00024 
00025 #include "GL/glew.h"
00026 #include <stdio.h>
00027 #include <iostream>
00028 #include <vector>
00029 #include <algorithm>
00030 #include <stdlib.h>
00031 #include <math.h>
00032 using namespace std;
00033 
00034 
00035 
00036 #include "GlobalUtil.h"
00037 
00038 #include "GLTexImage.h" 
00039 #include "FrameBufferObject.h"
00040 #include "ShaderMan.h"
00041 
00042 #ifndef SIFTGPU_NO_DEVIL
00043     #include "IL/il.h"
00044     #if  defined(_WIN64)
00045             #pragma comment(lib, "../../lib/DevIL64.lib")
00046     #elif  defined(_WIN32) 
00047             #pragma comment(lib, "../../lib/DevIL.lib")
00048     #endif
00049 #endif
00050 
00051 // Construction/Destruction
00053 
00054 
00055 GLTexImage::GLTexImage()
00056 {
00057         _imgWidth = _imgHeight = 0;
00058         _texWidth = _texHeight = 0;
00059         _drawWidth = _drawHeight = 0;
00060         _texID = 0;
00061 
00062 }
00063 
00064 GLTexImage::~GLTexImage()
00065 {
00066         if(_texID) glDeleteTextures(1, &_texID);
00067 }
00068 
00069 int GLTexImage::CheckTexture()
00070 {
00071         if(_texID)
00072         {
00073                 GLint tw, th;
00074                 BindTex();
00075                 glGetTexLevelParameteriv(_texTarget, 0, GL_TEXTURE_WIDTH , &tw);
00076                 glGetTexLevelParameteriv(_texTarget, 0, GL_TEXTURE_HEIGHT , &th);
00077                 UnbindTex();
00078                 return tw == _texWidth && th == _texHeight;
00079         }else
00080         {
00081                 return _texWidth == 0 && _texHeight ==0;
00082 
00083         }
00084 }
00085 //set a dimension that is smaller than the actuall size
00086 //for drawQuad
00087 void GLTexImage::SetImageSize( int width,  int height)
00088 {
00089         _drawWidth  = _imgWidth =  width;
00090         _drawHeight = _imgHeight =  height;
00091 }
00092 
00093 void GLTexImage::InitTexture( int width,  int height, int clamp_to_edge)
00094 {
00095 
00096         if(_texID && width == _texWidth && height == _texHeight ) return;
00097         if(_texID==0)   glGenTextures(1, &_texID); 
00098 
00099         _texWidth = _imgWidth = _drawWidth = width;
00100         _texHeight = _imgHeight = _drawHeight = height;
00101 
00102         BindTex();
00103 
00104         if(clamp_to_edge)
00105         {
00106                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
00107                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
00108         }else
00109         {
00110                 //out of bound tex read returns 0??
00111                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); 
00112                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); 
00113         }
00114         glTexParameteri(_texTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
00115         glTexParameteri(_texTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
00116         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00117 
00118         glTexImage2D(_texTarget, 0, _iTexFormat, 
00119                 _texWidth, _texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 
00120         CheckErrorsGL("glTexImage2D");
00121 
00122 
00123         UnbindTex();
00124 
00125 }
00126 
00127 
00128 void GLTexImage::InitTexture( int width,  int height, int clamp_to_edge, GLuint format)
00129 {
00130 
00131         if(_texID && width == _texWidth && height == _texHeight ) return;
00132         if(_texID==0)   glGenTextures(1, &_texID); 
00133 
00134         _texWidth = _imgWidth = _drawWidth = width;
00135         _texHeight = _imgHeight = _drawHeight = height;
00136 
00137         BindTex();
00138 
00139         if(clamp_to_edge)
00140         {
00141                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
00142                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
00143         }else
00144         {
00145                 //out of bound tex read returns 0??
00146                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); 
00147                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); 
00148         }
00149         glTexParameteri(_texTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
00150         glTexParameteri(_texTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
00151         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00152 
00153         glTexImage2D(_texTarget, 0, format, _texWidth, _texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 
00154 
00155         UnbindTex();
00156 
00157 }
00158 void  GLTexImage::BindTex()
00159 {
00160         glBindTexture(_texTarget, _texID);
00161 }
00162 
00163 void  GLTexImage::UnbindTex()
00164 {
00165         glBindTexture(_texTarget, 0);
00166 }
00167 
00168 
00169 void  GLTexImage::DrawQuad()
00170 {
00171         glBegin (GL_QUADS);
00172                 glTexCoord2i ( 0                        ,   0   );                              glVertex2i   ( 0                        ,               0   ); 
00173                 glTexCoord2i ( 0                        ,   _drawHeight  );             glVertex2i   ( 0                        ,               _drawHeight   ); 
00174                 glTexCoord2i ( _drawWidth   ,   _drawHeight  );         glVertex2i   ( _drawWidth       ,               _drawHeight   ); 
00175                 glTexCoord2i ( _drawWidth       ,   0   );                              glVertex2i   ( _drawWidth       ,               0   ); 
00176         glEnd ();
00177         glFlush();
00178 }
00179 
00180 void GLTexImage::FillMargin(int marginx, int marginy)
00181 {
00182         //
00183         marginx = min(marginx, _texWidth - _imgWidth);
00184         marginy = min(marginy, _texHeight - _imgHeight);
00185         if(marginx >0 || marginy > 0)
00186         {
00187                 GlobalUtil::FitViewPort(_imgWidth + marginx, _imgHeight + marginy);
00188                 AttachToFBO(0);
00189                 BindTex();
00190                 ShaderMan::UseShaderMarginCopy(_imgWidth, _imgHeight);
00191                 DrawMargin(_imgWidth + marginx, _imgHeight + marginy);
00192         }
00193 }
00194 
00195 void GLTexImage::ZeroHistoMargin()
00196 {
00197         ZeroHistoMargin(_imgWidth, _imgHeight);
00198 }
00199 
00200 void GLTexImage::ZeroHistoMargin(int width, int height)
00201 {
00202         int marginx = width & 0x01;
00203         int marginy = height & 0x01;
00204         if(marginx >0 || marginy > 0)
00205         {
00206                 int right = width + marginx;
00207                 int bottom = height + marginy;
00208                 GlobalUtil::FitViewPort(right, bottom);
00209                 AttachToFBO(0);
00210                 ShaderMan::UseShaderZeroPass();
00211                 glBegin(GL_QUADS);
00212                 if(right > width && _texWidth > width)
00213                 {
00214                         glTexCoord2i ( width    ,   0   );                              glVertex2i   ( width    ,               0   ); 
00215                         glTexCoord2i ( width    ,   bottom  );                  glVertex2i   ( width    ,               bottom   ); 
00216                         glTexCoord2i ( right    ,   bottom  );                  glVertex2i   ( right    ,               bottom   ); 
00217                         glTexCoord2i ( right    ,   0   );                              glVertex2i   ( right    ,               0   );  
00218                 }
00219                 if(bottom>height && _texHeight > height)
00220                 {
00221                         glTexCoord2i ( 0                ,   height );           glVertex2i   ( 0                ,               height   ); 
00222                         glTexCoord2i ( 0                ,   bottom      );              glVertex2i   ( 0                ,               bottom           ); 
00223                         glTexCoord2i ( width    ,   bottom      );              glVertex2i   ( width    ,               bottom           ); 
00224                         glTexCoord2i ( width    ,   height      );              glVertex2i   ( width    ,               height   ); 
00225                 }
00226                 glEnd();
00227                 glFlush();
00228         }
00229 
00230 }
00231 
00232 void GLTexImage::DrawMargin(int right, int bottom)
00233 {
00234         glBegin(GL_QUADS);
00235         if(right > _drawWidth)
00236         {
00237                 glTexCoord2i ( _drawWidth       ,   0   );                              glVertex2i   ( _drawWidth       ,               0   ); 
00238                 glTexCoord2i ( _drawWidth       ,   bottom  );                  glVertex2i   ( _drawWidth       ,               bottom   ); 
00239                 glTexCoord2i ( right            ,   bottom  );                  glVertex2i   ( right            ,               bottom   ); 
00240                 glTexCoord2i ( right            ,   0   );                              glVertex2i   ( right            ,               0   );  
00241         }
00242         if(bottom>_drawHeight)
00243         {
00244                 glTexCoord2i ( 0                        ,   _drawHeight );              glVertex2i   ( 0                        ,               _drawHeight   ); 
00245                 glTexCoord2i ( 0                        ,   bottom              );              glVertex2i   ( 0                        ,               bottom           ); 
00246                 glTexCoord2i ( _drawWidth       ,   bottom              );              glVertex2i   ( _drawWidth       ,               bottom           ); 
00247                 glTexCoord2i ( _drawWidth       ,   _drawHeight );              glVertex2i   ( _drawWidth       ,               _drawHeight      ); 
00248         }
00249         glEnd();
00250         glFlush();
00251 
00252 
00253 }
00254 
00255 
00256 void GLTexImage::DrawQuadMT4()
00257 {
00258         int w = _drawWidth, h = _drawHeight;
00259         glBegin (GL_QUADS);
00260                 glMultiTexCoord2i( GL_TEXTURE0, 0               ,   0  );       
00261                 glMultiTexCoord2i( GL_TEXTURE1, -1              ,   0  ); 
00262                 glMultiTexCoord2i( GL_TEXTURE2, 1               ,   0  ); 
00263                 glMultiTexCoord2i( GL_TEXTURE3, 0               ,   -1  ); 
00264                 glMultiTexCoord2i( GL_TEXTURE4, 0               ,   1  ); 
00265                 glVertex2i   ( 0                        ,               0   ); 
00266 
00267                 glMultiTexCoord2i( GL_TEXTURE0, 0               ,   h  );       
00268                 glMultiTexCoord2i( GL_TEXTURE1, -1              ,   h  ); 
00269                 glMultiTexCoord2i( GL_TEXTURE2, 1               ,   h ); 
00270                 glMultiTexCoord2i( GL_TEXTURE3, 0               ,   h -1 ); 
00271                 glMultiTexCoord2i( GL_TEXTURE4, 0               ,   h +1 ); 
00272                 glVertex2i   ( 0                        ,               h   ); 
00273 
00274 
00275                 glMultiTexCoord2i( GL_TEXTURE0, w               ,   h  );       
00276                 glMultiTexCoord2i( GL_TEXTURE1, w-1             ,   h  ); 
00277                 glMultiTexCoord2i( GL_TEXTURE2, w+1             ,   h  ); 
00278                 glMultiTexCoord2i( GL_TEXTURE3, w               ,   h-1  ); 
00279                 glMultiTexCoord2i( GL_TEXTURE4, w               ,   h+1  ); 
00280                 glVertex2i   ( w        ,               h   ); 
00281 
00282                 glMultiTexCoord2i( GL_TEXTURE0, w               ,   0  );       
00283                 glMultiTexCoord2i( GL_TEXTURE1, w-1             ,   0  ); 
00284                 glMultiTexCoord2i( GL_TEXTURE2, w+1             ,   0  ); 
00285                 glMultiTexCoord2i( GL_TEXTURE3, w               ,   -1  ); 
00286                 glMultiTexCoord2i( GL_TEXTURE4, w               ,   1  ); 
00287                 glVertex2i   ( w        ,               0   ); 
00288         glEnd ();
00289         glFlush();
00290 }
00291 
00292 
00293 void GLTexImage::DrawQuadMT8()
00294 {
00295         int w = _drawWidth;
00296         int h = _drawHeight;
00297         glBegin (GL_QUADS);
00298                 glMultiTexCoord2i( GL_TEXTURE0, 0               ,   0  );       
00299                 glMultiTexCoord2i( GL_TEXTURE1, -1              ,   0  ); 
00300                 glMultiTexCoord2i( GL_TEXTURE2, 1               ,   0  ); 
00301                 glMultiTexCoord2i( GL_TEXTURE3, 0               ,   -1  ); 
00302                 glMultiTexCoord2i( GL_TEXTURE4, 0               ,   1  ); 
00303                 glMultiTexCoord2i( GL_TEXTURE5, -1              ,   -1  ); 
00304                 glMultiTexCoord2i( GL_TEXTURE6, -1              ,   1  ); 
00305                 glMultiTexCoord2i( GL_TEXTURE7, 1               ,   -1  ); 
00306                 glVertex2i   ( 0                        ,               0   ); 
00307 
00308                 glMultiTexCoord2i( GL_TEXTURE0, 0               ,   h    );     
00309                 glMultiTexCoord2i( GL_TEXTURE1, -1              ,   h    ); 
00310                 glMultiTexCoord2i( GL_TEXTURE2, 1               ,   h    ); 
00311                 glMultiTexCoord2i( GL_TEXTURE3, 0               ,   h  -1  ); 
00312                 glMultiTexCoord2i( GL_TEXTURE4, 0               ,   h  +1  ); 
00313                 glMultiTexCoord2i( GL_TEXTURE5, -1              ,   h  -1  ); 
00314                 glMultiTexCoord2i( GL_TEXTURE6, -1              ,   h  +1  ); 
00315                 glMultiTexCoord2i( GL_TEXTURE7, 1               ,   h  -1  ); 
00316                 glVertex2i   ( 0                        ,               h   ); 
00317 
00318 
00319                 glMultiTexCoord2i( GL_TEXTURE0, w               ,   h    );     
00320                 glMultiTexCoord2i( GL_TEXTURE1, w-1             ,   h    ); 
00321                 glMultiTexCoord2i( GL_TEXTURE2, w+1             ,   h    ); 
00322                 glMultiTexCoord2i( GL_TEXTURE3, w               ,   h  -1  ); 
00323                 glMultiTexCoord2i( GL_TEXTURE4, w               ,   h  +1  ); 
00324                 glMultiTexCoord2i( GL_TEXTURE5, w-1             ,   h  -1  ); 
00325                 glMultiTexCoord2i( GL_TEXTURE6, w-1             ,   h  +1  ); 
00326                 glMultiTexCoord2i( GL_TEXTURE7, w+1             ,   h  -1  ); 
00327                 glVertex2i   ( w        ,               h   ); 
00328 
00329                 glMultiTexCoord2i( GL_TEXTURE0, w               ,   0  );       
00330                 glMultiTexCoord2i( GL_TEXTURE1, w-1             ,   0  ); 
00331                 glMultiTexCoord2i( GL_TEXTURE2, w+1             ,   0  ); 
00332                 glMultiTexCoord2i( GL_TEXTURE3, w               ,   -1  ); 
00333                 glMultiTexCoord2i( GL_TEXTURE4, w               ,   1  ); 
00334                 glMultiTexCoord2i( GL_TEXTURE5, w-1             ,   -1  ); 
00335                 glMultiTexCoord2i( GL_TEXTURE6, w-1             ,   1  ); 
00336                 glMultiTexCoord2i( GL_TEXTURE7, w+1             ,   -1  ); 
00337                 glVertex2i   ( w        ,               0   ); 
00338         glEnd ();
00339         glFlush();
00340 }
00341 
00342 
00343 
00344 
00345 void GLTexImage::DrawImage()
00346 {
00347         DrawQuad();
00348 }
00349 
00350 
00351 
00352 void GLTexImage::FitTexViewPort()
00353 {
00354         GlobalUtil::FitViewPort(_drawWidth, _drawHeight);
00355 }
00356 
00357 void GLTexImage::FitRealTexViewPort()
00358 {
00359         GlobalUtil::FitViewPort(_texWidth, _texHeight);
00360 }
00361 
00362 void  GLTexImage::AttachToFBO(int i)
00363 {
00364         glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, i+GL_COLOR_ATTACHMENT0_EXT, _texTarget, _texID, 0 );
00365 }
00366 
00367 void  GLTexImage::DetachFBO(int i)
00368 {
00369         glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, i+GL_COLOR_ATTACHMENT0_EXT, _texTarget, 0, 0 );
00370 }
00371 
00372 
00373 void GLTexImage::DrawQuad(float x1, float x2, float y1, float y2)
00374 {
00375 
00376         glBegin (GL_QUADS);
00377                 glTexCoord2f ( x1       ,   y1   );     glVertex2f   ( x1       ,               y1   ); 
00378                 glTexCoord2f ( x1       ,   y2  );              glVertex2f   ( x1       ,               y2   ); 
00379                 glTexCoord2f ( x2   ,   y2  );          glVertex2f   ( x2       ,               y2   ); 
00380                 glTexCoord2f ( x2       ,   y1   );     glVertex2f   ( x2       ,               y1   ); 
00381         glEnd ();
00382         glFlush();
00383 }
00384 
00385 void GLTexImage::TexConvertRGB()
00386 {
00387         //change 3/22/09
00388         FrameBufferObject fbo;
00389         //GlobalUtil::FitViewPort(1, 1); 
00390         FitTexViewPort();
00391 
00392         AttachToFBO(0);
00393         ShaderMan::UseShaderRGB2Gray();
00394         glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
00395         DrawQuad();
00396         ShaderMan::UnloadProgram();
00397         DetachFBO(0);
00398 }
00399 
00400 void GLTexImage::DrawQuadDS(int scale)
00401 {
00402         DrawScaledQuad(float(scale));
00403 }
00404 
00405 void GLTexImage::DrawQuadUS(int scale)
00406 {
00407         DrawScaledQuad(1.0f/scale);
00408 }
00409 
00410 void GLTexImage::DrawScaledQuad(float texscale)
00411 {
00412 
00414         float to = 0.5f -0.5f * texscale;
00415         float tx =  _imgWidth*texscale +to;
00416         float ty = _imgHeight*texscale +to;
00417         glBegin (GL_QUADS);
00418                 glTexCoord2f ( to       ,   to   );     glVertex2i   ( 0                        ,               0   ); 
00419                 glTexCoord2f ( to       ,   ty  );              glVertex2i   ( 0                        ,               _imgHeight   ); 
00420                 glTexCoord2f ( tx       ,       ty );           glVertex2i   ( _imgWidth        ,               _imgHeight   ); 
00421                 glTexCoord2f ( tx       ,   to   );     glVertex2i   ( _imgWidth        ,               0   ); 
00422         glEnd ();
00423         glFlush();
00424 }
00425 
00426 
00427 void GLTexImage::DrawQuadReduction(int w , int h)
00428 {
00429         float to = -0.5f;
00430         float tx = w*2 +to;
00431         float ty = h*2 +to;
00432         glBegin (GL_QUADS);
00433                 glMultiTexCoord2f ( GL_TEXTURE0, to     ,       to   ); 
00434                 glMultiTexCoord2f ( GL_TEXTURE1, to     +1,     to   ); 
00435                 glMultiTexCoord2f ( GL_TEXTURE2, to     ,       to+1  ); 
00436                 glMultiTexCoord2f ( GL_TEXTURE3, to     +1,     to+1  ); 
00437                 glVertex2i   ( 0                        ,               0   ); 
00438 
00439                 glMultiTexCoord2f ( GL_TEXTURE0, to     ,   ty  );      
00440                 glMultiTexCoord2f ( GL_TEXTURE1, to     +1, ty  );      
00441                 glMultiTexCoord2f ( GL_TEXTURE2, to     ,   ty +1 );    
00442                 glMultiTexCoord2f ( GL_TEXTURE3, to     +1, ty +1 );    
00443                 glVertex2i   ( 0                        ,               h   ); 
00444 
00445                 glMultiTexCoord2f ( GL_TEXTURE0, tx     ,       ty );   
00446                 glMultiTexCoord2f ( GL_TEXTURE1, tx     +1,     ty );   
00447                 glMultiTexCoord2f ( GL_TEXTURE2, tx     ,       ty +1);         
00448                 glMultiTexCoord2f ( GL_TEXTURE3, tx     +1,     ty +1);         
00449 
00450                 glVertex2i   ( w        ,               h   ); 
00451 
00452                 glMultiTexCoord2f ( GL_TEXTURE0, tx     ,   to   ); 
00453                 glMultiTexCoord2f ( GL_TEXTURE1, tx     +1, to   ); 
00454                 glMultiTexCoord2f ( GL_TEXTURE2, tx     ,   to +1  ); 
00455                 glMultiTexCoord2f ( GL_TEXTURE3, tx     +1, to +1  ); 
00456                 glVertex2i   ( w        ,               0   ); 
00457         glEnd ();
00458 
00459         glFlush();
00460 }
00461 
00462 
00463 void GLTexImage::DrawQuadReduction()
00464 {
00465         float to = -0.5f;
00466         float tx = _drawWidth*2 +to;
00467         float ty = _drawHeight*2 +to;
00468         glBegin (GL_QUADS);
00469                 glMultiTexCoord2f ( GL_TEXTURE0, to     ,       to   ); 
00470                 glMultiTexCoord2f ( GL_TEXTURE1, to     +1,     to   ); 
00471                 glMultiTexCoord2f ( GL_TEXTURE2, to     ,       to+1  ); 
00472                 glMultiTexCoord2f ( GL_TEXTURE3, to     +1,     to+1  ); 
00473                 glVertex2i   ( 0                        ,               0   ); 
00474 
00475                 glMultiTexCoord2f ( GL_TEXTURE0, to     ,   ty  );      
00476                 glMultiTexCoord2f ( GL_TEXTURE1, to     +1, ty  );      
00477                 glMultiTexCoord2f ( GL_TEXTURE2, to     ,   ty +1 );    
00478                 glMultiTexCoord2f ( GL_TEXTURE3, to     +1, ty +1 );    
00479                 glVertex2i   ( 0                        ,               _drawHeight   ); 
00480 
00481                 glMultiTexCoord2f ( GL_TEXTURE0, tx     ,       ty );   
00482                 glMultiTexCoord2f ( GL_TEXTURE1, tx     +1,     ty );   
00483                 glMultiTexCoord2f ( GL_TEXTURE2, tx     ,       ty +1);         
00484                 glMultiTexCoord2f ( GL_TEXTURE3, tx     +1,     ty +1);         
00485 
00486                 glVertex2i   ( _drawWidth       ,               _drawHeight   ); 
00487 
00488                 glMultiTexCoord2f ( GL_TEXTURE0, tx     ,   to   ); 
00489                 glMultiTexCoord2f ( GL_TEXTURE1, tx     +1, to   ); 
00490                 glMultiTexCoord2f ( GL_TEXTURE2, tx     ,   to +1  ); 
00491                 glMultiTexCoord2f ( GL_TEXTURE3, tx     +1, to +1  ); 
00492                 glVertex2i   ( _drawWidth       ,               0   ); 
00493         glEnd ();
00494 
00495         glFlush();
00496 }
00497 
00498 void GLTexPacked::TexConvertRGB()
00499 {
00500         //update the actual size of daw area
00501         _drawWidth  = (1 + _imgWidth) >> 1;
00502         _drawHeight = (1 + _imgHeight) >> 1;
00504         FrameBufferObject fbo;
00505         GLuint oldTexID = _texID;
00506         glGenTextures(1, &_texID);
00507         glBindTexture(_texTarget, _texID);
00508         glTexImage2D(_texTarget, 0, _iTexFormat, _texWidth,     _texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,       NULL);
00509 
00510         //input
00511         glBindTexture(_texTarget, oldTexID);
00512         //output 
00513         AttachToFBO(0);
00514         //program
00515         ShaderMan::UseShaderRGB2Gray();
00516         //draw buffer
00517         glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
00518         //run
00519         DrawQuadDS(2);
00520         ShaderMan::UnloadProgram();
00521 
00522         glDeleteTextures(1, &oldTexID);
00523         DetachFBO(0);
00524 }
00525 
00526 
00527 void GLTexPacked::SetImageSize( int width,  int height)
00528 {
00529         _imgWidth =  width;             _drawWidth = (width + 1) >> 1;
00530         _imgHeight =  height;   _drawHeight = (height + 1) >> 1;
00531 }
00532 
00533 void GLTexPacked::InitTexture( int width,  int height, int clamp_to_edge)
00534 {
00535 
00536         if(_texID && width == _imgWidth && height == _imgHeight ) return;
00537         if(_texID==0)   glGenTextures(1, &_texID); 
00538 
00539         _imgWidth = width;
00540         _imgHeight = height;
00541         if(GlobalUtil::_PreciseBorder)
00542         {
00543                 _texWidth = (width + 2) >> 1;
00544                 _texHeight = (height + 2) >> 1;
00545         }else
00546         {
00547                 _texWidth = (width + 1) >> 1;
00548                 _texHeight = (height + 1) >> 1;
00549         }
00550         _drawWidth = (width + 1) >> 1;
00551         _drawHeight = (height + 1) >> 1;
00552 
00553         BindTex();
00554 
00555         if(clamp_to_edge)
00556         {
00557                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
00558                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
00559         }else
00560         {
00561                 //out of bound tex read returns 0??
00562                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); 
00563                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); 
00564         }
00565         glTexParameteri(_texTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
00566         glTexParameteri(_texTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
00567         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00568 
00569         glTexImage2D(_texTarget, 0, _iTexFormat, 
00570                 _texWidth, _texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 
00571 
00572         UnbindTex();
00573 
00574 }
00575 
00576 
00577 void  GLTexPacked::DrawImage()
00578 {
00579         float x1 =0,  y1 = 0; //border..
00580         float x2 = _imgWidth*0.5f +x1;
00581         float y2 = _imgHeight*0.5f + y1; 
00582         glBegin (GL_QUADS);
00583                 glTexCoord2f ( x1       ,   y1  );                      glVertex2i   ( 0                        ,               0   ); 
00584                 glTexCoord2f ( x1       ,   y2 );                       glVertex2i   ( 0                        ,               _imgHeight   ); 
00585                 glTexCoord2f ( x2   ,   y2  );                  glVertex2i   ( _imgWidth        ,               _imgHeight   ); 
00586                 glTexCoord2f ( x2       ,   y1   );             glVertex2i   ( _imgWidth        ,               0   ); 
00587         glEnd ();
00588         glFlush();
00589 }
00590 
00591 void GLTexPacked::DrawQuadUS(int scale)
00592 {
00593         int tw =_drawWidth, th = _drawHeight;
00594         float texscale = 1.0f / scale;
00595         float x1 = 0.5f - 0.5f*scale, y1 = x1;
00596         float x2 = tw * texscale + x1;
00597         float y2 = th * texscale + y1;
00598         float step = texscale *0.5f;
00599         glBegin (GL_QUADS);
00600                 glMultiTexCoord2f( GL_TEXTURE0, x1              ,   y1      );  
00601                 glMultiTexCoord2f( GL_TEXTURE1, x1+step ,   y1      );  
00602                 glMultiTexCoord2f( GL_TEXTURE2, x1      ,   y1 +step);  
00603                 glMultiTexCoord2f( GL_TEXTURE3, x1+step ,   y1 +step);  
00604                 glVertex2i   ( 0                        ,               0   ); 
00605 
00606                 glMultiTexCoord2f( GL_TEXTURE0, x1              ,       y2      );      
00607                 glMultiTexCoord2f( GL_TEXTURE1, x1+step ,   y2      );
00608                 glMultiTexCoord2f( GL_TEXTURE2, x1      ,   y2 +step);  
00609                 glMultiTexCoord2f( GL_TEXTURE3, x1+step ,   y2 +step);  
00610                 glVertex2i   ( 0                        ,       th   ); 
00611 
00612                 glMultiTexCoord2f( GL_TEXTURE0, x2              ,   y2      );
00613                 glMultiTexCoord2f( GL_TEXTURE1, x2+step ,   y2      );
00614                 glMultiTexCoord2f( GL_TEXTURE2, x2      ,   y2 +step);
00615                 glMultiTexCoord2f( GL_TEXTURE3, x2+step ,   y2 +step);
00616                 glVertex2i   ( tw       ,       th   ); 
00617 
00618                 glMultiTexCoord2f( GL_TEXTURE0, x2              ,   y1      );  
00619                 glMultiTexCoord2f( GL_TEXTURE1, x2+step ,   y1      );
00620                 glMultiTexCoord2f( GL_TEXTURE2, x2      ,   y1 +step);  
00621                 glMultiTexCoord2f( GL_TEXTURE3, x2+step ,   y1 +step);          
00622                 glVertex2i   ( tw       ,       0   ); 
00623         glEnd ();
00624 }
00625 
00626 void GLTexPacked::DrawQuadDS(int scale)
00627 {
00628         int tw = _drawWidth;
00629         int th = _drawHeight;
00630         float x1 = 0.5f - 0.5f*scale;
00631         float x2 = tw * scale + x1;
00632         float y1 = 0.5f - 0.5f * scale;
00633         float y2 = th * scale + y1;
00634         int step = scale / 2;
00635 
00636         glBegin (GL_QUADS);
00637                 glMultiTexCoord2f( GL_TEXTURE0, x1              ,   y1      );  
00638                 glMultiTexCoord2f( GL_TEXTURE1, x1+step ,   y1      );  
00639                 glMultiTexCoord2f( GL_TEXTURE2, x1      ,   y1 +step);  
00640                 glMultiTexCoord2f( GL_TEXTURE3, x1+step ,   y1 +step);  
00641                 glVertex2i   ( 0                        ,               0   ); 
00642 
00643                 glMultiTexCoord2f( GL_TEXTURE0, x1              ,       y2      );      
00644                 glMultiTexCoord2f( GL_TEXTURE1, x1+step ,   y2      );
00645                 glMultiTexCoord2f( GL_TEXTURE2, x1      ,   y2 +step);  
00646                 glMultiTexCoord2f( GL_TEXTURE3, x1+step ,   y2 +step);  
00647                 glVertex2i   ( 0                        ,       th   ); 
00648 
00649                 glMultiTexCoord2f( GL_TEXTURE0, x2              ,   y2      );
00650                 glMultiTexCoord2f( GL_TEXTURE1, x2+step ,   y2      );
00651                 glMultiTexCoord2f( GL_TEXTURE2, x2      ,   y2 +step);
00652                 glMultiTexCoord2f( GL_TEXTURE3, x2+step ,   y2 +step);
00653                 glVertex2i   ( tw       ,       th   ); 
00654 
00655                 glMultiTexCoord2f( GL_TEXTURE0, x2              ,   y1      );  
00656                 glMultiTexCoord2f( GL_TEXTURE1, x2+step ,   y1      );
00657                 glMultiTexCoord2f( GL_TEXTURE2, x2      ,   y1 +step);  
00658                 glMultiTexCoord2f( GL_TEXTURE3, x2+step ,   y1 +step);          
00659                 glVertex2i   ( tw       ,       0   ); 
00660         glEnd ();
00661 }
00662 
00663 void GLTexPacked::ZeroHistoMargin()
00664 {
00665         int marginx = (((_imgWidth  + 3) /4)*4) - _imgWidth;
00666         int marginy = (((-_imgHeight + 3)/4)*4) - _imgHeight;
00667         if(marginx >0 || marginy > 0)
00668         {
00669                 int tw = (_imgWidth + marginx ) >> 1;   
00670                 int th = (_imgHeight + marginy ) >> 1;  
00671                 tw = min(_texWidth, tw );
00672                 th = min(_texHeight, th);
00673                 GlobalUtil::FitViewPort(tw, th);
00674                 AttachToFBO(0);
00675                 BindTex();
00676                 ShaderMan::UseShaderZeroPass();
00677                 DrawMargin(tw, th, 1, 1);
00678         }
00679 }
00680 
00681 
00682 void GLTexPacked::FillMargin(int marginx, int marginy)
00683 {
00684         //
00685         marginx = min(marginx, _texWidth * 2 - _imgWidth);
00686         marginy = min(marginy, _texHeight * 2 - _imgHeight);
00687         if(marginx >0 || marginy > 0)
00688         {
00689                 int tw = (_imgWidth + marginx + 1) >> 1;
00690                 int th = (_imgHeight + marginy + 1) >> 1;
00691                 GlobalUtil::FitViewPort(tw, th);
00692                 BindTex();
00693                 AttachToFBO(0);
00694                 ShaderMan::UseShaderMarginCopy(_imgWidth , _imgHeight);
00695                 DrawMargin(tw, th, marginx, marginy);
00696         }
00697 }
00698 void GLTexPacked::DrawMargin(int right, int bottom, int mx, int my)
00699 {
00700         int tw = (_imgWidth >>1);
00701         int th = (_imgHeight >>1);
00702         glBegin(GL_QUADS);
00703         if(right>tw && mx)
00704         {
00705                 glTexCoord2i ( tw       ,   0   );                              glVertex2i   ( tw       ,               0   ); 
00706                 glTexCoord2i ( tw       ,   bottom  );                  glVertex2i   ( tw       ,               bottom   ); 
00707                 glTexCoord2i ( right,   bottom  );                      glVertex2i   ( right,           bottom   ); 
00708                 glTexCoord2i ( right,   0   );                          glVertex2i   ( right,           0   );  
00709         }
00710         if(bottom>th && my)
00711         {
00712                 glTexCoord2i ( 0        ,   th  );              glVertex2i   ( 0        ,               th   ); 
00713                 glTexCoord2i ( 0        ,   bottom      );      glVertex2i   ( 0        ,               bottom   ); 
00714                 glTexCoord2i ( tw       ,   bottom      );      glVertex2i   ( tw       ,               bottom   ); 
00715                 glTexCoord2i ( tw       ,   th  );              glVertex2i   ( tw       ,               th       ); 
00716         }
00717         glEnd();
00718         glFlush();
00719 
00720 }
00721 
00722 
00723 void GLTexImage::UnbindMultiTex(int n)
00724 {
00725         for(int i = n-1; i>=0; i--)
00726         {
00727                 glActiveTexture(GL_TEXTURE0+i);
00728                 glBindTexture(_texTarget, 0);
00729         }
00730 }
00731 
00732 template <class Uint> int 
00733 
00734 #if !defined(_MSC_VER) || _MSC_VER > 1200
00735 GLTexInput::
00736 #endif
00737 
00738 DownSamplePixelDataI(unsigned int gl_format, int width, int height, int ds, 
00739                                                                         const Uint * pin, Uint * pout)  
00740 {
00741         int step, linestep;
00742         int i, j; 
00743         int ws = width/ds;
00744         int hs = height/ds;
00745         const Uint * line = pin, * p;
00746         Uint *po = pout;
00747         switch(gl_format)
00748         {
00749         case GL_LUMINANCE:
00750         case GL_LUMINANCE_ALPHA:
00751                 step = ds * (gl_format == GL_LUMINANCE? 1: 2); 
00752                 linestep = width * step; 
00753                 for(i = 0 ; i < hs; i++, line+=linestep)
00754                 {
00755                         for(j = 0, p = line; j < ws; j++, p+=step)
00756                         {
00757                                 *po++ = *p;
00758                         }
00759                 }
00760                 break;
00761         case GL_RGB:
00762         case GL_RGBA:
00763                 step = ds * (gl_format == GL_RGB? 3: 4); 
00764                 linestep = width * step; 
00765 
00766                 for(i = 0 ; i < hs; i++, line+=linestep)
00767                 {
00768                         for(j = 0, p = line; j < ws; j++, p+=step)
00769                         {
00770                                 //*po++ = int(p[0]*0.299 + p[1] * 0.587 + p[2]* 0.114 + 0.5);
00771                                 *po++ = ((19595*p[0] + 38470*p[1] + 7471*p[2]+ 32768)>>16);
00772                         }
00773                 }
00774                 break;
00775         case GL_BGR:
00776         case GL_BGRA:
00777                 step = ds * (gl_format == GL_BGR? 3: 4); 
00778                 linestep = width * step; 
00779                 for(i = 0 ; i < hs; i++, line+=linestep)
00780                 {
00781                         for(j = 0, p = line; j < ws; j++, p+=step)
00782                         {
00783                                 *po++ = ((7471*p[0] + 38470*p[1] + 19595*p[2]+ 32768)>>16);
00784                         }
00785                 }
00786                 break;
00787         default:
00788                 return 0;
00789         }
00790 
00791         return 1;
00792 
00793 }
00794 
00795 
00796 template <class Uint> int 
00797 
00798 #if !defined(_MSC_VER) || _MSC_VER > 1200
00799 GLTexInput::
00800 #endif
00801 
00802 DownSamplePixelDataI2F(unsigned int gl_format, int width, int height, int ds, 
00803                                                                         const Uint * pin, float * pout, int skip)       
00804 {
00805         int step, linestep;
00806         int i, j; 
00807         int ws = width/ds - skip;
00808         int hs = height/ds;
00809         const Uint * line = pin, * p;
00810         float *po = pout;
00811     const float factor = (sizeof(Uint) == 1? 255.0f : 65535.0f); 
00812         switch(gl_format)
00813         {
00814         case GL_LUMINANCE:
00815         case GL_LUMINANCE_ALPHA:
00816                 step = ds * (gl_format == GL_LUMINANCE? 1: 2); 
00817                 linestep = width * step; 
00818                 for(i = 0 ; i < hs; i++, line+=linestep)
00819                 {
00820                         for(j = 0, p = line; j < ws; j++, p+=step)
00821                         {
00822                                 *po++ = (*p) / factor; 
00823                         }
00824                 }
00825                 break;
00826         case GL_RGB:
00827         case GL_RGBA:
00828                 step = ds * (gl_format == GL_RGB? 3: 4); 
00829                 linestep = width * step; 
00830 
00831                 for(i = 0 ; i < hs; i++, line+=linestep)
00832                 {
00833                         for(j = 0, p = line; j < ws; j++, p+=step)
00834                         {
00835                                 //*po++ = int(p[0]*0.299 + p[1] * 0.587 + p[2]* 0.114 + 0.5);
00836                                 *po++ = ((19595*p[0] + 38470*p[1] + 7471*p[2]) / (65535.0f * factor));
00837                         }
00838                 }
00839                 break;
00840         case GL_BGR:
00841         case GL_BGRA:
00842                 step = ds * (gl_format == GL_BGR? 3: 4); 
00843                 linestep = width * step; 
00844                 for(i = 0 ; i < hs; i++, line+=linestep)
00845                 {
00846                         for(j = 0, p = line; j < ws; j++, p+=step)
00847                         {
00848                                 *po++ = ((7471*p[0] + 38470*p[1] + 19595*p[2]) / (65535.0f * factor));
00849                         }
00850                 }
00851                 break;
00852         default:
00853                 return 0;
00854         }
00855         return 1;
00856 }
00857 
00858 int GLTexInput::DownSamplePixelDataF(unsigned int gl_format, int width, int height, int ds, const float * pin, float * pout, int skip)  
00859 {
00860         int step, linestep;
00861         int i, j; 
00862         int ws = width/ds - skip;
00863         int hs = height/ds;
00864         const float * line = pin, * p;
00865         float *po = pout;
00866         switch(gl_format)
00867         {
00868         case GL_LUMINANCE:
00869         case GL_LUMINANCE_ALPHA:
00870                 step = ds * (gl_format == GL_LUMINANCE? 1: 2); 
00871                 linestep = width * step; 
00872                 for(i = 0 ; i < hs; i++, line+=linestep)
00873                 {
00874                         for(j = 0, p = line; j < ws; j++, p+=step)
00875                         {
00876                                 *po++ = *p;
00877                         }
00878                 }
00879                 break;
00880         case GL_RGB:
00881         case GL_RGBA:
00882                 step = ds * (gl_format == GL_RGB? 3: 4); 
00883                 linestep = width * step; 
00884                 for(i = 0 ; i < hs; i++, line+=linestep)
00885                 {
00886                         for(j = 0, p = line; j < ws; j++, p+=step)
00887                         {
00888                                 *po++ = (0.299f*p[0] + 0.587f*p[1] + 0.114f*p[2]);
00889                         }
00890                 }
00891                 break;
00892         case GL_BGR:
00893         case GL_BGRA:
00894                 step = ds * (gl_format == GL_BGR? 3: 4); 
00895                 linestep = width * step; 
00896                 for(i = 0 ; i < hs; i++, line+=linestep)
00897                 {
00898                         for(j = 0, p = line; j < ws; j++, p+=step)
00899                         {
00900                                 *po++ = (0.114f*p[0] + 0.587f*p[1] + 0.299f * p[2]); 
00901                         }
00902                 }
00903                 break;
00904         default:
00905                 return 0;
00906         }
00907 
00908         return 1;
00909 
00910 }
00911 
00912 int GLTexInput::SetImageData( int width,  int height, const void * data, 
00913                                                          unsigned int gl_format, unsigned int gl_type )
00914 {
00915         int simple_format = IsSimpleGlFormat(gl_format, gl_type);//no cpu code to handle other formats
00916         int ws, hs, done = 1;
00917         
00918         if(_converted_data) {delete [] _converted_data; _converted_data  = NULL; }
00919 
00920         _rgb_converted = 1; 
00921     _data_modified = 0; 
00922 
00923         if( simple_format 
00924                 && ( width > _texMaxDim || height > _texMaxDim || GlobalUtil::_PreProcessOnCPU) 
00925                 && GlobalUtil::_octave_min_default >0   )
00926         {
00927                 _down_sampled = GlobalUtil::_octave_min_default; 
00928                 ws = width >> GlobalUtil::_octave_min_default;
00929                 hs = height >> GlobalUtil::_octave_min_default;
00930         }else
00931         {
00932                 _down_sampled = 0; 
00933                 ws = width;
00934                 hs = height;
00935         }
00936         
00937         if ( ws > _texMaxDim || hs > _texMaxDim)
00938         {
00939                 if(simple_format)
00940                 {
00941                         if(GlobalUtil::_verbose) std::cout<<"Automatic down-sampling is used\n";
00942                         do
00943                         {
00944                                 _down_sampled ++;
00945                                 ws >>= 1;
00946                                 hs >>= 1;
00947                         }while(ws > _texMaxDim || hs > _texMaxDim);
00948                 }else
00949                 {
00950                         std::cerr<<"Input images is too big to fit into a texture\n";
00951                         return 0;
00952                 }
00953         }
00954 
00955         _texWidth = _imgWidth = _drawWidth = ws;        
00956         _texHeight = _imgHeight = _drawHeight = hs;
00957 
00958         if(GlobalUtil::_verbose)
00959         {
00960                 std::cout<<"Image size :\t"<<width<<"x"<<height<<"\n";
00961                 if(_down_sampled >0)    std::cout<<"Down sample to \t"<<ws<<"x"<<hs<<"\n";
00962         }
00963 
00964 
00965     if(GlobalUtil::_UseCUDA || GlobalUtil::_UseOpenCL)
00966     {
00968         int tWidth = TruncateWidthCU(_imgWidth);
00969         int skip = _imgWidth - tWidth; 
00970         //skip = 0;
00971         if(!simple_format) 
00972         {
00973             std::cerr << "Input format not supported under current settings.\n";
00974             return 0;
00975         }else if(_down_sampled > 0 || gl_format != GL_LUMINANCE || gl_type != GL_FLOAT)
00976         {
00977                     _converted_data = new float [_imgWidth * _imgHeight];
00978             if(gl_type == GL_UNSIGNED_BYTE)
00979                         DownSamplePixelDataI2F(gl_format, width, height, 1<<_down_sampled, 
00980                                         ((const unsigned char*) data), _converted_data, skip);
00981                 else if(gl_type == GL_UNSIGNED_SHORT)
00982                         DownSamplePixelDataI2F(gl_format, width, height, 1<<_down_sampled, 
00983                                         ((const unsigned short*) data), _converted_data, skip);
00984                 else
00985                         DownSamplePixelDataF(gl_format, width, height, 1<<_down_sampled, (float*)data, _converted_data, skip);
00986             _rgb_converted = 2;  //indidates a new data copy
00987             _pixel_data = _converted_data;
00988         }else
00989         {
00990             //Luminance data that doesn't need to down sample
00991             _rgb_converted = 1;
00992             _pixel_data = data;
00993             if(skip > 0)
00994             {
00995                 for(int i = 1; i < _imgHeight; ++i)
00996                 {   
00997                     float * dst = ((float*)data) + i * tWidth, * src = ((float*)data) + i * _imgWidth;
00998                     for(int j = 0; j < tWidth; ++j) *dst++ = * src++;
00999                 }
01000             }
01001         }
01002         _texWidth = _imgWidth = _drawWidth = tWidth;
01003         _data_modified = 1; 
01004     }else
01005     {
01006             if(_texID ==0)              glGenTextures(1, &_texID); 
01007             glBindTexture(_texTarget, _texID); 
01008             CheckErrorsGL("glBindTexture");
01009                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
01010                 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
01011             glPixelStorei(GL_UNPACK_ALIGNMENT , 1); 
01012 
01013             if(simple_format && ( _down_sampled> 0 || (gl_format != GL_LUMINANCE && GlobalUtil::_PreProcessOnCPU) ))
01014             {
01015 
01016                     if(gl_type == GL_UNSIGNED_BYTE)
01017                     {
01018                             unsigned char * newdata = new unsigned char [_imgWidth * _imgHeight];
01019                             DownSamplePixelDataI(gl_format, width, height, 1<<_down_sampled, ((const unsigned char*) data), newdata);
01020                             glTexImage2D(_texTarget, 0, GL_LUMINANCE32F_ARB, //internal format changed
01021                     _imgWidth, _imgHeight, 0,
01022                                         GL_LUMINANCE, GL_UNSIGNED_BYTE, newdata);
01023                             delete newdata;
01024                     }else if(gl_type == GL_UNSIGNED_SHORT)
01025                     {
01026                             unsigned short * newdata = new unsigned short [_imgWidth * _imgHeight];
01027                             DownSamplePixelDataI(gl_format, width, height, 1<<_down_sampled, ((const unsigned short*) data), newdata);
01028                         
01029                             glTexImage2D(_texTarget, 0, GL_LUMINANCE32F_ARB,   //internal format changed
01030                     _imgWidth, _imgHeight, 0,
01031                                         GL_LUMINANCE, GL_UNSIGNED_SHORT, newdata);
01032                             delete newdata;
01033                     }else if(gl_type == GL_FLOAT)
01034                     {
01035                             float * newdata = new float [_imgWidth * _imgHeight];
01036                             DownSamplePixelDataF(gl_format, width, height, 1<<_down_sampled, (float*)data, newdata);
01037                             glTexImage2D(_texTarget, 0, GL_LUMINANCE32F_ARB, //internal format changed
01038                     _imgWidth, _imgHeight, 0,
01039                                         GL_LUMINANCE, GL_FLOAT, newdata);
01040                             delete newdata;
01041                     }else
01042                     {
01043                             //impossible
01044                             done = 0;
01045                             _rgb_converted = 0;
01046                     }
01047                     GlobalUtil::FitViewPort(1, 1);  //this used to be necessary
01048             }else
01049             {
01050                     //ds must be 0 here if not simpleformat
01051                     if(gl_format == GL_LUMINANCE || gl_format == GL_LUMINANCE_ALPHA)
01052             {
01053                 //use one channel internal format if data is intensity image 
01054                 glTexImage2D(_texTarget, 0, GL_LUMINANCE32F_ARB, 
01055                 _imgWidth, _imgHeight, 0, gl_format,    gl_type, data); 
01056                             GlobalUtil::FitViewPort(1, 1); //this used to be necessary
01057             }
01058                     else
01059             {
01060                     //convert RGB 2 GRAY if needed
01061                 glTexImage2D(_texTarget, 0,  _iTexFormat, _imgWidth, _imgHeight, 0, gl_format, gl_type, data); 
01062                 if(ShaderMan::HaveShaderMan())
01063                                 TexConvertRGB();
01064                         else
01065                                 _rgb_converted = 0;  //In CUDA mode, the conversion will be done by CUDA kernel
01066             }
01067             }
01068             UnbindTex();
01069     }
01070         return done;
01071 }
01072 
01073 
01074 GLTexInput::~GLTexInput()
01075 {
01076     if(_converted_data) delete [] _converted_data; 
01077 }
01078 
01079 
01080 int GLTexInput::LoadImageFile(char *imagepath, int &w, int &h )
01081 {
01082 #ifndef SIFTGPU_NO_DEVIL
01083     static int devil_loaded = 0; 
01084         unsigned int imID;
01085         int done = 1;
01086 
01087     if(devil_loaded == 0)
01088     {
01089             ilInit();
01090             ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
01091             ilEnable(IL_ORIGIN_SET);
01092         devil_loaded = 1; 
01093     }
01094 
01096         ilGenImages(1, &imID);
01097         ilBindImage(imID); 
01098 
01099         if(ilLoadImage(imagepath))
01100         {
01101                 w = ilGetInteger(IL_IMAGE_WIDTH);
01102                 h = ilGetInteger(IL_IMAGE_HEIGHT);
01103                 int ilformat = ilGetInteger(IL_IMAGE_FORMAT);
01104 
01105                 if(SetImageData(w, h, ilGetData(), ilformat, GL_UNSIGNED_BYTE)==0)
01106                 {
01107                         done =0;
01108                 }else   if(GlobalUtil::_verbose)
01109                 {
01110                         std::cout<<"Image loaded :\t"<<imagepath<<"\n";
01111                 }
01112 
01113         }else
01114         {
01115                 std::cerr<<"Unable to open image [code = "<<ilGetError()<<"]\n";
01116                 done = 0;
01117         }
01118 
01119         ilDeleteImages(1, &imID); 
01120 
01121         return done;
01122 #else
01123         FILE * file = fopen(imagepath, "rb"); if (file ==NULL) return 0;
01124 
01125         char buf[8];    int  width, height, cn, g, done = 1; 
01126 
01127         if(fscanf(file, "%s %d %d %d", buf, &width, &height, &cn )<4 ||  cn > 255 || width < 0 || height < 0)
01128         {
01129                 fclose(file);
01130         std::cerr << "ERROR: fileformat not supported\n";
01131                 return 0;
01132         }else
01133     {
01134         w = width;
01135         h = height; 
01136     }
01137     unsigned char * data = new unsigned char[width * height];
01138         unsigned char * pixels = data;
01139         if (strcmp(buf, "P5")==0 )
01140         {
01141                 fscanf(file, "%c",buf);//skip one byte
01142                 fread(pixels, 1, width*height, file);
01143         }else if (strcmp(buf, "P2")==0 )
01144         {
01145                 for (int i = 0 ; i< height; i++)
01146                 {
01147                         for ( int j = 0; j < width; j++)
01148                         {
01149                                 fscanf(file, "%d", &g);
01150                                 *pixels++ = (unsigned char) g;
01151                         }
01152                 }
01153         }else if (strcmp(buf, "P6")==0 )
01154         {
01155                 fscanf(file, "%c", buf);//skip one byte
01156                 int j, num = height*width;
01157         unsigned char buf[3];
01158                 for ( j =0 ; j< num; j++)
01159                 {
01160                         fread(buf,1,3, file);
01161                         *pixels++=int(0.10454f* buf[2]+0.60581f* buf[1]+0.28965f* buf[0]);
01162                 }
01163         }else if (strcmp(buf, "P3")==0 )
01164         {
01165                 int r, g, b;
01166                 int i , num =height*width;
01167                 for ( i = 0 ; i< num; i++)
01168                 {
01169                         fscanf(file, "%d %d %d", &r, &g, &b);
01170                         *pixels++ = int(0.10454f* b+0.60581f* g+0.28965f* r);
01171                 }
01172 
01173         }else
01174         {
01175         std::cerr << "ERROR: fileformat not supported\n";
01176                 done = 0;
01177         }
01178     if(done)    SetImageData(width, height, data, GL_LUMINANCE, GL_UNSIGNED_BYTE);
01179         fclose(file);
01180     delete data; 
01181     if(GlobalUtil::_verbose && done) std::cout<< "Image loaded :\t" << imagepath << "\n";
01182         return 1;
01183 #endif
01184 }
01185 
01186 int GLTexImage::CopyToPBO(GLuint pbo, int width, int height, GLenum format)
01187 {
01189     if(format != GL_RGBA && format != GL_LUMINANCE) return 0; 
01190 
01191         FrameBufferObject fbo;
01192     GLint bsize, esize = width * height * sizeof(float) * (format == GL_RGBA ? 4 : 1);
01193         AttachToFBO(0);
01194         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
01195         glGetBufferParameteriv(GL_PIXEL_PACK_BUFFER_ARB, GL_BUFFER_SIZE, &bsize);
01196         if(bsize < esize) 
01197         {
01198                 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, esize,   NULL, GL_STATIC_DRAW_ARB);
01199                 glGetBufferParameteriv(GL_PIXEL_PACK_BUFFER_ARB, GL_BUFFER_SIZE, &bsize);
01200         }
01201         if(bsize >= esize)
01202         {
01203                 glReadPixels(0, 0, width, height, format, GL_FLOAT, 0);
01204         }
01205         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
01206         DetachFBO(0);
01207 
01208         return bsize >= esize;
01209 }
01210 
01211 void GLTexInput::VerifyTexture()
01212 {
01213     //for CUDA or OpenCL the texture is not generated by default
01214     if(!_data_modified) return;
01215     if(_pixel_data== NULL) return;
01216     InitTexture(_imgWidth, _imgHeight);
01217     BindTex();
01218         glTexImage2D(   _texTarget, 0, GL_LUMINANCE32F_ARB, //internal format changed
01219                     _imgWidth, _imgHeight, 0,
01220                                         GL_LUMINANCE, GL_FLOAT, _pixel_data);
01221     UnbindTex();
01222     _data_modified = 0; 
01223 }
01224 
01225 void GLTexImage::CopyFromPBO(GLuint pbo, int width, int height, GLenum format)
01226 {
01227         InitTexture(max(width, _texWidth), max(height, _texHeight));
01228         SetImageSize(width, height);
01229         if(width > 0 && height > 0)
01230         {
01231                 BindTex();
01232                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
01233                 glTexSubImage2D(GlobalUtil::_texTarget, 0, 0, 0, width, height, format, GL_FLOAT, 0);
01234         GlobalUtil::CheckErrorsGL("GLTexImage::CopyFromPBO->glTexSubImage2D");
01235                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
01236                 UnbindTex();
01237         }
01238 }
01239 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


rgbd_registration
Author(s): Ross Kidson
autogenerated on Thu May 23 2013 15:36:53