test-module.C

00001 /*! @file SceneUnderstanding/test-tensor.C Test the various vision comp
00002  * with simple stimulus*/
00003 
00004 // //////////////////////////////////////////////////////////////////// //
00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00006 // by the University of Southern California (USC) and the iLab at USC.  //
00007 // See http://iLab.usc.edu for information about this project.          //
00008 // //////////////////////////////////////////////////////////////////// //
00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00011 // in Visual Environments, and Applications'' by Christof Koch and      //
00012 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00013 // pending; application number 09/912,225 filed July 23, 2001; see      //
00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00015 // //////////////////////////////////////////////////////////////////// //
00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00017 //                                                                      //
00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00019 // redistribute it and/or modify it under the terms of the GNU General  //
00020 // Public License as published by the Free Software Foundation; either  //
00021 // version 2 of the License, or (at your option) any later version.     //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00026 // PURPOSE.  See the GNU General Public License for more details.       //
00027 //                                                                      //
00028 // You should have received a copy of the GNU General Public License    //
00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00031 // Boston, MA 02111-1307 USA.                                           //
00032 // //////////////////////////////////////////////////////////////////// //
00033 //
00034 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/plugins/SceneUnderstanding/test-module.C $
00036 // $Id: test-module.C 13413 2010-05-15 21:00:11Z itti $
00037 //
00038 
00039 #include "Image/Image.H"
00040 #include "Component/ModelManager.H"
00041 #include "plugins/SceneUnderstanding/TensorVoting.H"
00042 #include "plugins/SceneUnderstanding/V2.H"
00043 #include "plugins/SceneUnderstanding/SFS.H"
00044 #include "Raster/Raster.H"
00045 #include "GUI/DebugWin.H"
00046 #include "GUI/ViewPort.H"
00047 #include <GL/glut.h>
00048 
00049 #include <signal.h>
00050 #include <sys/types.h>
00051 
00052 typedef unsigned char BYTE;
00053 
00054 typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
00055 
00056 
00057 typedef struct
00058 {
00059   int type;
00060   unsigned int    maxX,
00061                   maxY;
00062   unsigned char   *image;
00063 }       PIC;
00064 
00065 void drawQube()
00066 {
00067   glBegin(GL_QUADS);
00068   glColor4f(1, 1, 1, 1);
00069 
00070   // face v0-v1-v2-v3
00071   glNormal3f(0,0,1);
00072   glTexCoord2f(1, 1);  glVertex3f(1,1,1);
00073   glTexCoord2f(0, 1);  glVertex3f(-1,1,1);
00074   glTexCoord2f(0, 0);  glVertex3f(-1,-1,1);
00075   glTexCoord2f(1, 0);  glVertex3f(1,-1,1);
00076 
00077   // face v0-v3-v4-v5
00078   glNormal3f(1,0,0);
00079   glTexCoord2f(0, 1);  glVertex3f(1,1,1);
00080   glTexCoord2f(0, 0);  glVertex3f(1,-1,1);
00081   glTexCoord2f(1, 0);  glVertex3f(1,-1,-1);
00082   glTexCoord2f(1, 1);  glVertex3f(1,1,-1);
00083 
00084   // face v0-v5-v6-v1
00085   glNormal3f(0,1,0);
00086   glTexCoord2f(1, 0);  glVertex3f(1,1,1);
00087   glTexCoord2f(1, 1);  glVertex3f(1,1,-1);
00088   glTexCoord2f(0, 1);  glVertex3f(-1,1,-1);
00089   glTexCoord2f(0, 0);  glVertex3f(-1,1,1);
00090 
00091   // face  v1-v6-v7-v2
00092   glNormal3f(-1,0,0);
00093   glTexCoord2f(1, 1);  glVertex3f(-1,1,1);
00094   glTexCoord2f(0, 1);  glVertex3f(-1,1,-1);
00095   glTexCoord2f(0, 0);  glVertex3f(-1,-1,-1);
00096   glTexCoord2f(1, 0);  glVertex3f(-1,-1,1);
00097 
00098   // face v7-v4-v3-v2
00099   glNormal3f(0,-1,0);
00100   glTexCoord2f(0, 0);  glVertex3f(-1,-1,-1);
00101   glTexCoord2f(1, 0);  glVertex3f(1,-1,-1);
00102   glTexCoord2f(1, 1);  glVertex3f(1,-1,1);
00103   glTexCoord2f(0, 1);  glVertex3f(-1,-1,1);
00104 
00105   // face v4-v7-v6-v5
00106   glNormal3f(0,0,-1);
00107   glTexCoord2f(0, 0);  glVertex3f(1,-1,-1);
00108   glTexCoord2f(1, 0);  glVertex3f(-1,-1,-1);
00109   glTexCoord2f(1, 1);  glVertex3f(-1,1,-1);
00110   glTexCoord2f(0, 1);  glVertex3f(1,1,-1);
00111   glEnd();
00112 }
00113 
00114 void displayCB()
00115 {
00116 
00117   int screenWidth = 320;
00118   int screenHeight = 240;
00119 
00120   LINFO("Draw");
00121   //Draw Qube
00122   glViewport(0, 0, screenWidth, screenHeight);
00123   glMatrixMode(GL_PROJECTION);
00124   glLoadIdentity();
00125   gluPerspective(60.0f, (float)(screenWidth)/screenHeight, 1.0f, 100.0f);
00126   glMatrixMode(GL_MODELVIEW);
00127 
00128   // clear framebuffer
00129   glClearColor(0, 0, 0, 0);
00130   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // | GL_STENCIL_BUFFER_BIT);
00131 
00132 
00133   //Draw to back buffer
00134   glPushAttrib(GL_COLOR_BUFFER_BIT | GL_PIXEL_MODE_BIT); // for GL_DRAW_BUFFER and GL_READ_BUFFER
00135   glDrawBuffer(GL_BACK);
00136   glReadBuffer(GL_BACK);
00137 
00138 
00139   // object position and rot
00140   glPushMatrix();
00141   glTranslatef(0, 0, 1.5);
00142   glRotatef(45, 1, 0, 0);   // pitch
00143   glRotatef(45, 0, 1, 0);   // heading
00144   // draw a cube with the dynamic texture
00145   drawQube();
00146   glPopMatrix();
00147 
00148 
00149 
00150   glPopAttrib(); // GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
00151 
00152   // draw info messages
00153   //glutSwapBuffers();
00154 
00155   glFlush();
00156 
00157 
00158 }
00159 
00160 void setCamera(float posX, float posY, float posZ, float targetX, float targetY, float targetZ)
00161 {
00162     glMatrixMode(GL_MODELVIEW);
00163     glLoadIdentity();
00164     gluLookAt(posX, posY, posZ, targetX, targetY, targetZ, 0, 1, 0); // eye(x,y,z), focal(x,y,z), up(x,y,z)
00165 }
00166 
00167 void initLights()
00168 {
00169     // set up light colors (ambient, diffuse, specular)
00170     GLfloat lightKa[] = {.2f, .2f, .2f, 1.0f};  // ambient light
00171     GLfloat lightKd[] = {.7f, .7f, .7f, 1.0f};  // diffuse light
00172     GLfloat lightKs[] = {1, 1, 1, 1};           // specular light
00173     glLightfv(GL_LIGHT0, GL_AMBIENT, lightKa);
00174     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightKd);
00175     glLightfv(GL_LIGHT0, GL_SPECULAR, lightKs);
00176 
00177     // position the light
00178     float lightPos[4] = {0, 0, 20, 1}; // positional light
00179     glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
00180 
00181     glEnable(GL_LIGHT0);                        // MUST enable each light source after configuration
00182 }
00183 
00184 
00185 PIC UCFReadPic(const char* filename)
00186 {
00187   FILE* infile = fopen(filename,"r");
00188   PIC temp;
00189   int ret;
00190 
00191   /* getting Type from image data */
00192   ret=fread(&temp.type,sizeof(temp.type),1,infile);
00193   switch (temp.type)
00194   {
00195     case 0xF10F:
00196     case 0xF200:
00197     case 0xF201:
00198     case 0xF204:
00199     case 0x0000:
00200       {
00201         //fread(&temp.maxX,sizeof(temp.maxX),1,infile);
00202         //fread(&temp.maxY,sizeof(temp.maxY),1,infile);
00203         unsigned char byte1,byte2,byte3,byte4;
00204         ret=fread(&byte1,sizeof(BYTE),1,infile);
00205         ret=fread(&byte2,sizeof(BYTE),1,infile);
00206         ret=fread(&byte3,sizeof(BYTE),1,infile);
00207         ret=fread(&byte4,sizeof(BYTE),1,infile);
00208         temp.maxX= byte1*16777216+byte2*65536+byte3*256+byte4;
00209         ret=fread(&byte1,sizeof(BYTE),1,infile);
00210         ret=fread(&byte2,sizeof(BYTE),1,infile);
00211         ret=fread(&byte3,sizeof(BYTE),1,infile);
00212         ret=fread(&byte4,sizeof(BYTE),1,infile);
00213         temp.maxY= byte1*16777216+byte2*65536+byte3*256+byte4;
00214         printf("Max: %i %i\n", temp.maxX, temp.maxY);
00215         break;
00216       }
00217     case 0x8000:
00218     case 0x8001:
00219     case 0xB003:
00220     default  :
00221       {
00222         ret=fread(&temp.maxX,sizeof(temp.maxX),1,infile);
00223         ret=fread(&temp.maxY,sizeof(temp.maxY),1,infile);
00224         break;
00225       }
00226   }
00227   if((temp.image=(BYTE*)calloc(temp.maxX*temp.maxY,sizeof(BYTE)))==NULL)
00228   {
00229     temp.maxX = temp.maxY = 0;
00230     temp.image = NULL;
00231     return(temp);
00232   }
00233 
00234   ret=fread(temp.image,sizeof(BYTE),temp.maxX * temp.maxY,infile);
00235   fclose(infile);
00236   return(temp);
00237 }
00238 
00239 
00240 
00241 int main(const int argc, const char **argv)
00242 {
00243 
00244   //MYLOGVERB = LOG_INFO;
00245   //ModelManager manager("Test Vision");
00246 
00247   //nub::ref<SimEventQueueConfigurator>
00248   //  seqc(new SimEventQueueConfigurator(manager));
00249   //manager.addSubComponent(seqc);
00250 
00251   //nub::ref<SimOutputFrameSeries> ofs(new SimOutputFrameSeries(manager));
00252   //manager.addSubComponent(ofs);
00253 
00254   //nub::ref<SimInputFrameSeries> ifs(new SimInputFrameSeries(manager));
00255   //manager.addSubComponent(ifs);
00256 
00257   //nub::ref<TensorVoting> tensorVoting(new TensorVoting(manager));
00258   //nub::ref<V2> v2(new V2(manager));
00259 
00260  // nub::ref<SFS> sfs(new SFS(manager));
00261 
00262    // Request a bunch of option aliases (shortcuts to lists of options):
00263   //REQUEST_OPTIONALIAS_NEURO(manager);
00264 
00265   //if (manager.parseCommandLine(
00266   //      (const int)argc, (const char**)argv, "", 0, 0) == false)
00267   //  return 1;
00268 
00269   //nub::ref<SimEventQueue> seq = seqc->getQ();
00270 
00271 
00272   //Image<PixRGB<byte> > img = Raster::ReadRGB("/home/elazary/images/testImages/stream-output000000.pnm");
00273  // Image<PixRGB<byte> > img = Raster::ReadRGB("/home/elazary/images/testImages/frame.pnm");
00274  //
00275 
00276   //Read the ucf image
00277 //  PIC pic1 = UCFReadPic("/home/elazary/shapeFromShading/shading/ucfimgs/sphere128.ucf");
00278 //  Image<byte> img(pic1.maxX, pic1.maxY, NO_INIT);
00279 //  for(uint j=0; j<pic1.maxY; j++)
00280 //    for(uint i=0; i<pic1.maxX; i++)
00281 //      img.setVal(i,j, pic1.image[i*pic1.maxX+j]);
00282 
00283   //manager.start();
00284 
00285 //  sfs->evolve(img);
00286   //v2->evolve(tf);
00287   //v2->evolve(img);
00288   //tensorVoting->evolve();
00289 
00290 
00291   Display *display = XOpenDisplay(0);
00292 
00293   if ( !display )
00294   {
00295     printf( "Failed to open X display\n" );
00296     exit(1);
00297   }
00298 
00299   // Get a matching FB config
00300   static int visual_attribs[] =
00301     {
00302       GLX_X_RENDERABLE    , True,
00303       GLX_DRAWABLE_TYPE   , GLX_WINDOW_BIT,
00304       GLX_RENDER_TYPE     , GLX_RGBA_BIT,
00305       GLX_X_VISUAL_TYPE   , GLX_TRUE_COLOR,
00306       GLX_RED_SIZE        , 8,
00307       GLX_GREEN_SIZE      , 8,
00308       GLX_BLUE_SIZE       , 8,
00309       GLX_ALPHA_SIZE      , 8,
00310       GLX_DEPTH_SIZE      , 24,
00311       GLX_STENCIL_SIZE    , 8,
00312       GLX_DOUBLEBUFFER    , True,
00313       //GLX_SAMPLE_BUFFERS  , 1,
00314       //GLX_SAMPLES         , 4,
00315       None
00316     };
00317 
00318   printf( "Getting matching framebuffer configs\n" );
00319   int fbcount;
00320   GLXFBConfig *fbc = glXChooseFBConfig( display, DefaultScreen( display ),
00321                                         visual_attribs, &fbcount );
00322   if ( !fbc )
00323   {
00324     printf( "Failed to retrieve a framebuffer config\n" );
00325     exit(1);
00326   }
00327   printf( "Found %d matching FB configs.\n", fbcount );
00328 
00329   // Pick the FB config/visual with the most samples per pixel
00330   printf( "Getting XVisualInfos\n" );
00331   int best_fbc = -1, worst_fbc = -1, best_num_samp = -1, worst_num_samp = 999;
00332 
00333   int i;
00334   for ( i = 0; i < fbcount; i++ )
00335   {
00336     XVisualInfo *vi = glXGetVisualFromFBConfig( display, fbc[i] );
00337     if ( vi )
00338     {
00339       int samp_buf, samples;
00340       glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf );
00341       glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLES       , &samples  );
00342 
00343       printf( "  Matching fbconfig %d, visual ID 0x%2x: SAMPLE_BUFFERS = %d,"
00344               " SAMPLES = %d\n",
00345               i, (uint)vi -> visualid, samp_buf, samples );
00346 
00347       if ( (best_fbc < 0 || samp_buf ) && (samples > best_num_samp) )
00348         best_fbc = i, best_num_samp = samples;
00349       if ( worst_fbc < 0 || !samp_buf || samples < worst_num_samp )
00350         worst_fbc = i, worst_num_samp = samples;
00351     }
00352     XFree( vi );
00353   }
00354 
00355   // Get a visual
00356   int fbc_id = best_fbc;
00357   //int fbc_id = worst_fbc;
00358 
00359   XVisualInfo *vi = glXGetVisualFromFBConfig( display, fbc[ fbc_id ]  );
00360   printf( "Chosen visual ID = 0x%x\n", (uint)vi->visualid );
00361 
00362   printf( "Creating colormap\n" );
00363   XSetWindowAttributes swa;
00364   swa.colormap = XCreateColormap( display, RootWindow( display, vi->screen ),
00365                                   vi->visual, AllocNone );
00366   swa.background_pixmap = None ;
00367   swa.border_pixel      = 0;
00368   swa.event_mask        = StructureNotifyMask;
00369 
00370   printf( "Creating window\n" );
00371   Window win = XCreateWindow( display, RootWindow( display, vi->screen ),
00372                               0, 0, 320, 240, 0, vi->depth, InputOutput,
00373                               vi->visual,
00374                               CWBorderPixel|CWColormap|CWEventMask, &swa );
00375   if ( !win )
00376   {
00377     printf( "Failed to create window.\n" );
00378     exit(1);
00379   }
00380 
00381   XStoreName( display, win, "GL 3.0 Window");
00382 
00383   printf( "Mapping window\n" );
00384   XMapWindow( display, win );
00385 
00386   // See if GL driver supports glXCreateContextAttribsARB()
00387   //   Create an old-style GLX context first, to get the correct function ptr.
00388   glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;
00389 
00390   GLXContext ctx_old = glXCreateContext( display, vi, 0, True );
00391   glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)
00392            glXGetProcAddress( (const GLubyte *) "glXCreateContextAttribsARB" );
00393 
00394   GLXContext ctx = ctx_old;
00395   XFree( fbc );
00396 
00397   // Verifying that context is a direct context
00398   printf( "Verifying that context is direct\n" );
00399   if ( ! glXIsDirect ( display, ctx ) )
00400   {
00401     printf( "Indirect GLX rendering context obtained" );
00402     exit(1);
00403   }
00404 
00405   printf( "Making context current\n" );
00406   glXMakeCurrent( display, win, ctx );
00407 
00408 
00409 
00410   for(uint i=0; i<10; i++)
00411   {
00412     displayCB();
00413     glXSwapBuffers ( display, win );
00414 
00415     Image<PixRGB<byte> > img(320,240, ZEROS);
00416 
00417     //glPixelStorei(GL_PACK_ALIGNMENT,1);
00418     //glReadBuffer(GL_BACK);
00419     glReadPixels (0, 0, 320, 240,
00420         GL_RGB, GL_UNSIGNED_BYTE, (unsigned char*)img.getArrayPtr());
00421     SHOWIMG(img);
00422   }
00423   // stop all our ModelComponents
00424   //manager.stop();
00425 
00426   return 0;
00427 }
00428 
Generated on Sun May 8 08:41:09 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3