psycho-still-monkey.C

00001 /*!@file AppPsycho/psycho-still.C Psychophysics display of still images */
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 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppPsycho/psycho-still-monkey.C $
00035 // $Id: psycho-still-monkey.C 13678 2010-07-19 18:16:16Z beobot $
00036 //
00037 #include "Component/ModelManager.H"
00038 #include "Component/ModelOptionDef.H"
00039 #include "Image/Image.H"
00040 #include "Image/Range.H"
00041 #include "Psycho/PsychoDisplay.H"
00042 #include "Psycho/EyeTrackerConfigurator.H"
00043 #include "Psycho/EyeTracker.H"
00044 #include "Psycho/PsychoOpts.H"
00045 #include "Component/EventLog.H"
00046 #include "Component/ComponentOpts.H"
00047 #include "Raster/Raster.H"
00048 #include "Util/SimTime.H"
00049 #include "Util/MathFunctions.H"
00050 #include "Util/Types.H"
00051 #include "rutz/rand.h"
00052 
00053 // ######################################################################
00054 extern "C" int main(const int argc, char** argv)
00055 {
00056   MYLOGVERB = LOG_INFO;  // suppress debug messages
00057 
00058   // Instantiate a ModelManager:
00059   ModelManager manager("Psycho Still");
00060   OModelParam<bool> keepfix(&OPT_KeepFix, &manager);
00061   OModelParam<float> fixsize(&OPT_FixSize, &manager);
00062   OModelParam<float> ppd(&OPT_Ppd, &manager);
00063   OModelParam<SimTime> itsDur(&OPT_DisplayTime, &manager);
00064   OModelParam<Range<int> > itsWait(&OPT_TrialWait, &manager);
00065   OModelParam<bool> testrun(&OPT_Testrun, &manager);
00066 
00067   // Instantiate our various ModelComponents:
00068   nub::soft_ref<PsychoDisplay> d(new PsychoDisplay(manager));
00069   manager.addSubComponent(d);
00070 
00071   nub::soft_ref<EyeTrackerConfigurator>
00072     etc(new EyeTrackerConfigurator(manager));
00073   manager.addSubComponent(etc);
00074 
00075   nub::soft_ref<EventLog> el(new EventLog(manager));
00076   manager.addSubComponent(el);
00077 
00078   manager.setOptionValString(&OPT_EventLogFileName, "psychodata.psy");
00079   manager.setOptionValString(&OPT_EyeTrackerType, "DML");
00080 
00081   // Parse command-line:
00082   if (manager.parseCommandLine(argc, argv,
00083                                "<img1.ppm> ... <imgN.ppm>", 1, -1)==false)
00084     return(1);
00085 
00086   // hook our various babies up and do post-command-line configs:
00087   nub::soft_ref<EyeTracker> et = etc->getET();
00088   d->setEyeTracker(et);
00089   d->setEventLog(el);
00090   et->setEventLog(el);
00091 
00092   //fixation point
00093   int fixrad = int(ppd.getVal() * fixsize.getVal());
00094   if ((fixrad % 2) != 0)
00095     --fixrad;
00096   d->setFixationSize(fixrad*2);
00097 
00098 
00099   SDL_Rect rect;
00100   const int siz2 = (fixrad - 1) / 2; // half cross size
00101   const int w = d->getDims().w(), h = d->getDims().h();
00102   const int i = w / 2 - 1;
00103   const int j = h / 2 - 1;
00104   rect.x = i - siz2; rect.y = j - siz2; rect.w = fixrad; rect.h = fixrad;
00105 
00106   // let's get all our ModelComponent instances started:
00107   manager.start();
00108   d->clearScreen();
00109 
00110   // setup array of movie indices:
00111   uint nbimgs = manager.numExtraArgs(); int index[nbimgs];
00112   for (uint i = 0; i < nbimgs; i ++)
00113     index[i] = i;
00114 
00115   LINFO("Randomizing images...");
00116   randShuffle(index, nbimgs);
00117 
00118   try {
00119     // main loop:
00120     for (uint i = 0; i < nbimgs; i ++)
00121       {
00122         // load up the frame and show a fixation cross on a blank screen:
00123         d->clearScreen();
00124         LINFO("Loading '%s'...", manager.getExtraArg(index[i]).c_str());
00125         Image< PixRGB<byte> > image =
00126           Raster::ReadRGB(manager.getExtraArg(index[i]));
00127 
00128         SDL_Surface *surf = d->makeBlittableSurface(image, true);
00129 
00130         //create a fixation on top of the image
00131         if (keepfix.getVal())
00132           SDL_FillRect(surf, &rect, d->getUint32color(PixRGB<byte>(255, 0, 0)));
00133 
00134         LINFO("'%s' ready.", manager.getExtraArg(index[i]).c_str());
00135 
00136         // eat up any extra fixation codes and keys before we start
00137         // the next fixation round:
00138         while(et->isFixating()) ;
00139         while(d->checkForKey() != -1) ;
00140 
00141         // display fixation to indicate that we are ready:
00142         d->displayRedDotFixation();
00143 
00144         // give us a chance to abort
00145         d->checkForKey();
00146 
00147         // ready to go whenever the monkey is ready (pulse on parallel port):
00148         if (!testrun.getVal())
00149           while(true)
00150             {
00151               if (et->isFixating()) break;
00152               if (d->checkForKey() != -1) break; // allow force start by key
00153             }
00154 
00155         d->waitNextRequestedVsync(false, true);
00156         d->pushEvent(std::string("===== Showing image: ") +
00157                      manager.getExtraArg(index[i]) + " =====");
00158 
00159         // start the eye tracker:
00160         et->track(true);
00161 
00162         // show the image:
00163         d->displaySurface(surf, -2);
00164 
00165         // wait for key:
00166         usleep(itsDur.getVal().usecs());
00167 
00168         // free the image:
00169         SDL_FreeSurface(surf);
00170 
00171         // make sure display if off before we stop the tracker:
00172         d->clearScreen();
00173 
00174         // stop the eye tracker:
00175         usleep(50000);
00176         et->track(false);
00177 
00178         //wait for requested time till next trial
00179         usleep(rutz::rand_range<int>(itsWait.getVal().min(), 
00180                                      itsWait.getVal().max()) * 1000);
00181       }
00182 
00183     d->clearScreen();
00184     d->displayText("Experiment complete. Thank you!");
00185     d->waitForKey();
00186   }
00187   catch (...)
00188     {
00189       REPORT_CURRENT_EXCEPTION;
00190     };
00191 
00192   // stop all our ModelComponents
00193   manager.stop();
00194 
00195   // all done!
00196   return 0;
00197 }
00198 
00199 // ######################################################################
00200 /* So things look consistent in everyone's emacs... */
00201 /* Local Variables: */
00202 /* indent-tabs-mode: nil */
00203 /* End: */
Generated on Sun May 8 08:04:14 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3