SDLdisplayStream.C

Go to the documentation of this file.
00001 /*!@file GUI/SDLdisplayStream.C A FrameOstream class that sends images to SDL windows */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the 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: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/GUI/SDLdisplayStream.C $
00035 // $Id: SDLdisplayStream.C 14290 2010-12-01 21:44:03Z itti $
00036 //
00037 
00038 #ifndef GUI_SDLDISPLAYSTREAM_C_DEFINED
00039 #define GUI_SDLDISPLAYSTREAM_C_DEFINED
00040 
00041 #ifdef HAVE_SDL_SDL_H
00042 
00043 #include "GUI/SDLdisplayStream.H"
00044 
00045 #include "Component/GlobalOpts.H" // for OPT_TestMode
00046 #include "GUI/SDLdisplay.H"
00047 #include "Image/Image.H"
00048 #include "Image/Pixels.H"
00049 #include "Raster/GenericFrame.H"
00050 #include "Util/Assert.H"
00051 
00052 #include <unistd.h> // for sleep()
00053 
00054 struct SDLdisplayStream::WindowMap
00055 {
00056   WindowMap()
00057     :
00058     win(),
00059     fakesdl(),
00060     pressedEscape(false)
00061   {}
00062 
00063   nub::soft_ref<SDLdisplay> win;
00064   nub::soft_ref<SDLdisplay> fakesdl; // to export command-line options
00065   bool pressedEscape;
00066 };
00067 
00068 SDLdisplayStream::SDLdisplayStream(OptionManager& mgr)
00069   :
00070   FrameOstream(mgr, "Image Display Stream", "SDLdisplayStream"),
00071   itsTestMode(&OPT_TestMode, this),
00072   wmap(0)
00073 {
00074   wmap = new WindowMap;
00075   wmap->fakesdl.reset(new SDLdisplay(mgr));
00076   this->addSubComponent(wmap->fakesdl);
00077 }
00078 
00079 SDLdisplayStream::~SDLdisplayStream()
00080 {
00081   delete wmap;
00082 }
00083 
00084 void SDLdisplayStream::writeFrame(const GenericFrame& frame,
00085                                     const std::string& shortname,
00086                                     const FrameInfo& auxinfo)
00087 {
00088   if (itsTestMode.getVal() == true)
00089     // don't put up any windows in test mode
00090     return;
00091 
00092   ASSERT(wmap != 0);
00093 
00094   if (wmap->pressedEscape == true)
00095     // don't put up any more windows if the user has already pressed
00096     // ESC once
00097     return;
00098 
00099   if (!wmap->win.is_valid())
00100     {
00101       wmap->win.reset
00102         (new SDLdisplay(this->getManager(), shortname, shortname));
00103 
00104       wmap->win->exportOptions(MC_RECURSE);
00105       wmap->win->setModelParamVal("SDLdisplayDims", frame.getDims());
00106       wmap->win->start();
00107       this->addSubComponent(wmap->win);
00108 
00109       if (wmap->win->getModelParamVal<bool>("SDLdisplayFullscreen"))
00110         // give the fullscreen window a chance to pop up:
00111         sleep(1);
00112     }
00113 
00114   ASSERT(wmap->win.is_valid());
00115 
00116   nub::ref<SDLdisplay> win(wmap->win);
00117 
00118   if (win->getDims() != frame.getDims())
00119     LFATAL("all frames sent to SDLdisplayStream must be the same size "
00120            "(got %dx%d, expected %dx%d)",
00121            frame.getDims().w(), frame.getDims().h(),
00122            win->getDims().w(), win->getDims().h());
00123 
00124   // give checkForKey() a chance to LFATAL() if ESC has been pressed
00125   win->checkForKey();
00126 
00127   switch (frame.nativeType())
00128     {
00129     case GenericFrame::NONE:
00130       break;
00131 
00132     case GenericFrame::RGB_U8:   /* fall through */
00133     case GenericFrame::RGBD:     /* fall through */
00134     case GenericFrame::RGB_F32:  /* fall through */
00135     case GenericFrame::GRAY_U8:  /* fall through */
00136     case GenericFrame::GRAY_F32:
00137       win->displayImage(frame.asRgb(), false, PixRGB<byte>(0,0,0),
00138                         -1, true);
00139       break;
00140 
00141     case GenericFrame::VIDEO:
00142       if (!win->supportsVideoOverlay(frame.asVideo().getMode()))
00143         {
00144           win->displayImage(frame.asRgb(), false, PixRGB<byte>(0,0,0),
00145                             -1, true);
00146         }
00147       else
00148         {
00149           if (!win->hasYUVoverlay())
00150             win->createVideoOverlay(frame.asVideo().getMode());
00151           win->displayVideoOverlay(frame.asVideo(), -1,
00152                                    SDLdisplay::NEXT_VSYNC);
00153         }
00154       break;
00155 
00156     case GenericFrame::RGB_U16:
00157       break;
00158     case GenericFrame::GRAY_U16:
00159       break;
00160     }
00161 }
00162 
00163 // ######################################################################
00164 bool SDLdisplayStream::isVoid() const
00165 {
00166   return wmap && wmap->pressedEscape;
00167 }
00168 
00169 // ######################################################################
00170 void SDLdisplayStream::start1()
00171 {
00172   // forget about our fake sdl display (which we have only used to
00173   // export sdl-related command-line options) before it tries to pop
00174   // up a window:
00175   this->removeSubComponent(*wmap->fakesdl);
00176   wmap->fakesdl.reset(0);
00177 }
00178 
00179 // ######################################################################
00180 void SDLdisplayStream::closeStream(const std::string& shortname)
00181 {
00182   if (this->wmap->win.is_valid())
00183     {
00184       this->wmap->win->stop();
00185       this->wmap->win.reset(0);
00186     }
00187 }
00188 
00189 #endif // HAVE_SDL_SDL_H
00190 
00191 // ######################################################################
00192 /* So things look consistent in everyone's emacs... */
00193 /* Local Variables: */
00194 /* mode: c++ */
00195 /* indent-tabs-mode: nil */
00196 /* End: */
00197 
00198 #endif // GUI_SDLDISPLAYSTREAM_C_DEFINED
Generated on Sun May 8 08:40:41 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3