indexing-playback.C

Go to the documentation of this file.
00001 /*!@file GameBoard/indexing-playback.C the application for playing back the indexing experiment*/
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 //
00035 // 2008-02-20 16:14:27Z nnoori $
00036 //
00037 
00038 #ifndef INVT_HAVE_LIBSDL_IMAGE
00039 
00040 #include <cstdio>
00041 int main()
00042 {
00043         fprintf(stderr, "The SDL_image library must be installed to use this program\n");
00044         return 1;
00045 }
00046 
00047 #else
00048 
00049 
00050 
00051 /*
00052 Attention: SDL_image is needed for linking
00053 
00054 */
00055 
00056 #include "Component/ModelManager.H"
00057 #include "Image/Image.H"
00058 #include "Psycho/PsychoDisplay.H"
00059 #include "Psycho/EyeTrackerConfigurator.H"
00060 #include "Psycho/EyeTracker.H"
00061 #include "Psycho/PsychoOpts.H"
00062 #include "Component/EventLog.H"
00063 #include "Component/ComponentOpts.H"
00064 #include "Raster/Raster.H"
00065 #include "Util/MathFunctions.H"
00066 #include "Util/Types.H"
00067 #include <iostream>
00068 #include <fstream>
00069 #include <SDL/SDL.h>
00070 #include <SDL/SDL_image.h>
00071 #include "AppPsycho/psycho-skin-resize.h"
00072 #include <stdio.h>
00073 #include <stdlib.h>
00074 #include <map>
00075 #include <time.h>
00076 #include "AppPsycho/psycho-skin-mapgenerator.h"
00077 #include <sstream>
00078 
00079 
00080 using namespace std;
00081 
00082 // ModelManager manager("Psycho Skin");
00083 // nub::soft_ref<PsychoDisplay> d(new PsychoDisplay(manager));
00084 bool programQuit = false ;
00085 map<string,string> skinFileMap ;
00086 const int NUMBER_OF_CLASSES = 6;
00087 const int IMAGE_WIDTH = 128 ;
00088 const string classes[NUMBER_OF_CLASSES]={"class1","class2","class3","class4","class5","class6"};
00089 
00090 
00091 ////////////////////////////////////
00092 
00093 template <class T>
00094                 string stringify(T &i)
00095 {
00096         ostringstream o ;
00097         o << i ;
00098         return o.str();
00099 }
00100 
00101 ///////////////////////////////////
00102 
00103 
00104 SDL_Surface *getAFreshSurface(int w ,  int h){
00105 
00106         SDL_Surface *sur = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
00107                         0x00000000, 0x00000000, 0x00000000, 0x00000000);
00108 
00109         return sur ;
00110 
00111 }
00112 
00113 /////////////////////////////////////////////
00114 SDL_Surface *load_image( string filename )
00115 {
00116 //The image that's loaded
00117         SDL_Surface* loadedImage = NULL;
00118 
00119 //The optimized image that will be used
00120         SDL_Surface* optimizedImage = NULL;
00121 
00122 //Load the image
00123         loadedImage = IMG_Load( filename.c_str() );
00124 
00125 //If the image loaded
00126         if( loadedImage != NULL )
00127         {
00128 //Create an optimized image
00129                 optimizedImage = SDL_DisplayFormat( loadedImage );
00130 
00131 //Free the old image
00132                 SDL_FreeSurface( loadedImage );
00133 
00134 //If the image was optimized just fine
00135                 if( optimizedImage != NULL )
00136                 {
00137         //Map the color key
00138                         Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
00139 
00140         //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
00141                         SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
00142                 }
00143         }/*else{
00144                 emergencyExit() ;
00145         }*/
00146 
00147 //Return the optimized image
00148         return optimizedImage;
00149 }
00150 
00151 ///////////////////////////////////
00152 void dumpSurface(SDL_Surface& surface){
00153         SDL_FreeSurface( &surface );
00154 }
00155 
00156 /////////////////////////////////////////////
00157 void apply_surface( int x, int y, SDL_Surface& source, SDL_Surface& destination , SDL_Rect& clip )
00158 {
00159 //Make a temporary rectangle to hold the offsets
00160         SDL_Rect offset;
00161 
00162 //Give the offsets to the rectangle
00163         offset.x = x;
00164         offset.y = y;
00165 
00166 //Blit the surface
00167         SDL_BlitSurface( &source, &clip, &destination, &offset );
00168 }
00169 
00170 
00171 /////////////////////////////////////////
00172 extern "C" int main( int argc, char* argv[] ){
00173 
00174         ModelManager manager("Psycho Skin");
00175         nub::soft_ref<PsychoDisplay> d(new PsychoDisplay(manager));
00176 
00177         MYLOGVERB = LOG_INFO;  // suppress debug messages
00178 
00179         manager.addSubComponent(d);
00180         nub::soft_ref<EventLog> el(new EventLog(manager));
00181         manager.addSubComponent(el);
00182         d->setEventLog(el);
00183         // Parse command-line:
00184         if (manager.parseCommandLine(argc, argv,"<profile.psymap> <path-to-skin> <logfilename>", 3, 3)==false)
00185                 return(1);
00186         manager.setOptionValString(&OPT_EventLogFileName,manager.getExtraArg(2).c_str() );
00187         manager.start();
00188         d->clearScreen() ;
00189         int trialSize = 0 ;
00190 
00191         ////
00192         vector<string>* evalVector = new vector<string>() ;
00193         vector<int>* elapsedTimeVector = new vector<int>() ;
00194         vector<int>* classVector = new vector<int>() ;
00195         vector<Matrix*>* patternVector = new vector<Matrix*>() ;
00196         vector<Matrix*>* mapVector = new vector<Matrix*>() ;
00197         ////
00198         //we read the psymap file and keep the trial information in seperate maps
00199         const char* filename = manager.getExtraArg(0).c_str();
00200         ifstream inFile(filename, ios::in);
00201         string pathToSkin = manager.getExtraArg(1);
00202         map<int,SDL_Surface*>* classesMap = new map<int,SDL_Surface*>();
00203         int maxMapRows = 0 ;
00204         int maxMapCols = 0 ;
00205         int maxPatternRows = 0 ;
00206         int maxPatternCols = 0 ;
00207         bool exitFlag = false ;
00208         if (!inFile)
00209         {
00210                 LINFO("profile  not found!");
00211                 exitFlag = true ;
00212         } else{
00213                 char ch[1000];
00214                 while (inFile.getline(ch , 1000)){
00215                         string line = ch;
00216                         LINFO("reads : %s", line.c_str());
00217                         if(line[0]=='1' && line[2]=='1') evalVector->push_back("TP");
00218                         if(line[0]=='1' && line[2]=='0') evalVector->push_back("FN");
00219                         if(line[0]=='0' && line[2]=='1') evalVector->push_back("FP");
00220                         if(line[0]=='0' && line[2]=='0') evalVector->push_back("TN");
00221                         line = line.substr(4);
00222                         uint position = line.find(",");
00223                         char rf[10] ;
00224                         strcpy(rf,line.substr(0,position).c_str());
00225                         elapsedTimeVector->push_back(atoi(rf)) ;
00226                         line = line.substr(position+1);
00227                         position = line.find(",");
00228                         strcpy(rf,line.substr(position-1,1).c_str());
00229                         classVector->push_back(atoi(rf)) ;
00230                         line = line.substr(position+1) ;
00231                         position = line.find(",");
00232                         patternVector->push_back(getPattenByString(line.substr(0,position+1)));
00233                         maxPatternRows = max(maxPatternRows,(patternVector->back())->getNumOfRows());
00234                         maxPatternCols = max(maxPatternCols,(patternVector->back())->getNumOfColumns());
00235                         line = line.substr(position+1) ;
00236                         mapVector->push_back(getMapByString(line)) ;
00237                         maxMapRows = max(maxMapRows , (mapVector->back())->getNumOfRows()) ;
00238                         maxMapCols = max(maxMapCols,(mapVector->back())->getNumOfColumns()) ;
00239                         d -> pushEvent("profile  "+line.substr(position+1)+" started reading" ) ;
00240                         trialSize++;
00241                 }
00242 
00243         }
00244 
00245         float resizeFactor = (float)(d->getHeight())*0.7f/(float)((maxMapRows+maxPatternRows+2)*IMAGE_WIDTH);
00246 
00247         for( int i = 0 ; i < 6 ; i++ ){
00248                 string filepath ;
00249                 filepath += pathToSkin+"/"+classes[i]+"/ball.png" ;
00250                 SDL_Surface* tmpSurface = load_image(filepath) ;
00251                 if(tmpSurface == NULL){
00252                         exitFlag = true ;
00253                         LINFO("path to skin images is invalid! ");
00254                         break ;
00255                 }
00256                 tmpSurface = SDL_Resize(tmpSurface , resizeFactor , 6 );
00257                 (*classesMap)[i] = tmpSurface;
00258         }
00259 
00260         if(exitFlag){
00261                 manager.stop();
00262                 evalVector->~vector<string>() ;
00263                 elapsedTimeVector-> ~vector<int>();
00264                 patternVector -> ~vector<Matrix*>() ;
00265                 mapVector -> ~vector<Matrix*>() ;
00266                 classesMap ->~map<int,SDL_Surface*>() ;
00267                 classVector -> ~vector<int>() ;
00268                 return(0) ;
00269         }
00270         int bgWidth = (int)((max(maxMapCols,maxPatternCols))*IMAGE_WIDTH*resizeFactor);
00271         int bgHeight = (int)((maxMapRows+maxPatternRows+2)*IMAGE_WIDTH*resizeFactor);
00272         SDL_Rect offset;
00273         offset.x = (d->getWidth() -bgWidth) /2;
00274         offset.y = (d-> getHeight() - bgHeight) /2;
00275 
00276         SDL_Rect theClip ;
00277         theClip.x = 0 ;
00278         theClip.y = 0 ;
00279         theClip.w = (int)(IMAGE_WIDTH*resizeFactor) ;
00280         theClip.h = (int)(IMAGE_WIDTH*resizeFactor) ;
00281 
00282         SDL_Rect patternOffset;
00283         patternOffset.x = (bgWidth - (int)(maxPatternCols*IMAGE_WIDTH*resizeFactor))/2;
00284         patternOffset.y = (int)((maxMapRows+1)*IMAGE_WIDTH*resizeFactor);
00285 
00286         //here we listen for keystrokes and react based on the keystroke
00287         d->showCursor(true);
00288         int trialIndex =0 ;
00289         int key = 1 ;
00290         while(true){
00291 
00292                 SDL_Surface* nbg = getAFreshSurface(bgWidth,bgHeight);
00293 
00294                 d->displayText(stringify(trialIndex)+":"+(*evalVector)[trialIndex]+", et:"+"  " + stringify((*elapsedTimeVector)[trialIndex])+" micro sec",true ,1 );
00295 
00296                 for( int r = 0 ; r < (*mapVector)[trialIndex]->getNumOfRows() ;r++ ){
00297                         for( int c = 0 ; c < (*mapVector)[trialIndex]->getNumOfColumns() ;c++ ){
00298                                 apply_surface((int)(c*IMAGE_WIDTH*resizeFactor),(int)(r*IMAGE_WIDTH*resizeFactor),*((*classesMap)[(*mapVector)[trialIndex]->get(r,c) -1]),*nbg,theClip) ;
00299 
00300                         }
00301                 }
00302 
00303                 for( int r = 0 ; r < (*patternVector)[trialIndex]->getNumOfRows() ;r++ ){
00304                         for( int c = 0 ; c < (*patternVector)[trialIndex]->getNumOfColumns() ;c++ ){
00305                                 apply_surface((int)(c*IMAGE_WIDTH*resizeFactor)+patternOffset.x,(int)(r*IMAGE_WIDTH*resizeFactor)+patternOffset.y,*((*classesMap)[(*classVector)[trialIndex] * (*patternVector)[trialIndex]->get(r,c) -1]),*nbg,theClip) ;
00306 
00307                         }
00308                 }
00309 
00310                 d->displaySDLSurfacePatch(nbg , &offset,NULL , -2,false, true);
00311                 do{
00312                         key = d->waitForKey() ;
00313                 }while(key != 49 && key != 50 && key!= 51);
00314 
00315                 if( key == 49 && trialIndex !=0 ){
00316                         --trialIndex ;
00317                 }else{
00318                         if( key == 49 && trialIndex ==0  ){
00319                                 trialIndex = trialSize -1 ;
00320                         }
00321                 }
00322 
00323                 if( key == 50 ){
00324                         trialIndex = (trialIndex+1)%trialSize ;
00325                 }
00326                 dumpSurface(*nbg);
00327                 if( key == 51 ) break ;
00328         }
00329 
00330         evalVector->~vector<string>() ;
00331         elapsedTimeVector-> ~vector<int>();
00332         patternVector -> ~vector<Matrix*>() ;
00333         mapVector -> ~vector<Matrix*>() ;
00334         classesMap ->~map<int,SDL_Surface*>() ;
00335         classVector -> ~vector<int>() ;
00336         manager.stop();
00337         return(0) ;
00338 }
00339 #endif // INVT_HAVE_LIBSDL_IMAGE
00340 
Generated on Sun May 8 08:40:39 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3