00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
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
00083
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
00117 SDL_Surface* loadedImage = NULL;
00118
00119
00120 SDL_Surface* optimizedImage = NULL;
00121
00122
00123 loadedImage = IMG_Load( filename.c_str() );
00124
00125
00126 if( loadedImage != NULL )
00127 {
00128
00129 optimizedImage = SDL_DisplayFormat( loadedImage );
00130
00131
00132 SDL_FreeSurface( loadedImage );
00133
00134
00135 if( optimizedImage != NULL )
00136 {
00137
00138 Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
00139
00140
00141 SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
00142 }
00143 }
00144
00145
00146
00147
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
00160 SDL_Rect offset;
00161
00162
00163 offset.x = x;
00164 offset.y = y;
00165
00166
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;
00178
00179 manager.addSubComponent(d);
00180 nub::soft_ref<EventLog> el(new EventLog(manager));
00181 manager.addSubComponent(el);
00182 d->setEventLog(el);
00183
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
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
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