ImageDisplayStream.C

Go to the documentation of this file.
00001 /*!@file GUI/ImageDisplayStream.C */
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:
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/GUI/ImageDisplayStream.C $
00035 // $Id: ImageDisplayStream.C 14376 2011-01-11 02:44:34Z pez $
00036 //
00037 
00038 #ifndef GUI_IMAGEDISPLAYSTREAM_C_DEFINED
00039 #define GUI_IMAGEDISPLAYSTREAM_C_DEFINED
00040 
00041 #include <map>
00042 #include <vector>
00043 
00044 #include "Component/GlobalOpts.H" // for OPT_TestMode
00045 #include "Component/ModelOptionDef.H"
00046 #include "Component/OptionManager.H"
00047 #include "GUI/XWinManaged.H"
00048 #include "Image/Image.H"
00049 #include "Image/Layout.H"
00050 #include "Image/Pixels.H"
00051 #include "Raster/GenericFrame.H"
00052 #include "Transport/TransportOpts.H"
00053 #include "Util/Assert.H"
00054 #include "rutz/shared_ptr.h"
00055 #include "GUI/ImageDisplayStream.H"
00056 
00057 // Used by: ImageDisplayStream
00058 static const ModelOptionDef OPT_MaxImageDisplay =
00059   { MODOPT_ARG(int), "MaxImageDisplay", &MOC_OUTPUT, OPTEXP_CORE,
00060     "max number of images to display onscreen for any given timestep",
00061     "max-image-display", '\0', "<integer>", "20" };
00062 
00063 // Used by: ImageDisplayStream
00064 static const ModelOptionDef OPT_TraceXEvents =
00065   { MODOPT_FLAG, "TraceXEvents", &MOC_OUTPUT, OPTEXP_CORE,
00066     "print some info about each X11 event that is received",
00067     "trace-x-events", '\0', "", "false" };
00068 
00069 typedef std::map<std::string, rutz::shared_ptr<XWinManaged> > map_type;
00070 
00071 // ######################################################################
00072 struct ImageDisplayStream::WindowMap
00073 {
00074   WindowMap()
00075     :
00076     windows(),
00077     pressedCloseButton(false)
00078   {}
00079 
00080   XWinManaged* getWindowForFrame(const Dims& dims,
00081                                  const std::string& shortname,
00082                                  const size_t maxdisplay,
00083                                  const bool tracexevents)
00084   {
00085     if (!this->windows[shortname].is_valid())
00086       {
00087         // check if one more window would be too many
00088         if (this->windows.size() + 1 > maxdisplay)
00089           {
00090             LERROR("**** TOO MANY WINDOWS! NOT DISPLAYING IMAGE...");
00091             return 0;
00092           }
00093 
00094         rutz::shared_ptr<XWinManaged> win
00095           (new XWinManaged(dims, -1, -1, shortname.c_str()));
00096 
00097         win->setTraceEvents(tracexevents);
00098 
00099         this->windows[shortname] = win;
00100       }
00101 
00102     rutz::shared_ptr<XWinManaged> win = this->windows[shortname];
00103 
00104     ASSERT(win.is_valid());
00105 
00106     if (win->pressedCloseButton())
00107       this->pressedCloseButton = true;
00108 
00109     win->setVisible(true);
00110 
00111     return win.get();
00112   }
00113 
00114   map_type windows;
00115   bool pressedCloseButton;
00116 };
00117 
00118 // ######################################################################
00119 ImageDisplayStream::ImageDisplayStream(OptionManager& mgr)
00120   :
00121   FrameOstream(mgr, "Image Display Stream", "ImageDisplayStream"),
00122   itsTestMode(&OPT_TestMode, this),
00123   itsMaxDisplay(&OPT_MaxImageDisplay, this),
00124   itsTraceXEvents(&OPT_TraceXEvents, this),
00125   wmap(0)
00126 {}
00127 
00128 // ######################################################################
00129 ImageDisplayStream::~ImageDisplayStream()
00130 {
00131   delete wmap;
00132 }
00133 
00134 // ######################################################################
00135 void ImageDisplayStream::writeFrame(const GenericFrame& frame,
00136                                     const std::string& shortname,
00137                                     const FrameInfo& auxinfo)
00138 {
00139   if (itsTestMode.getVal() == true)
00140     // don't put up any windows in test mode
00141     return;
00142 
00143   if (wmap == 0)
00144     {
00145       wmap = new WindowMap;
00146     }
00147 
00148   ASSERT(wmap != 0);
00149 
00150   XWinManaged* const win =
00151     wmap->getWindowForFrame(frame.getDims(), shortname,
00152                             size_t(itsMaxDisplay.getVal()),
00153                             itsTraceXEvents.getVal());
00154 
00155   if (win == 0)
00156     return;
00157 
00158   switch (frame.nativeType())
00159     {
00160     case GenericFrame::NONE:                                                          break;
00161     case GenericFrame::RGB_U8:   win->drawRgbLayout(frame.asRgbU8Layout(),0,0,true);  break;
00162     case GenericFrame::RGBD:     win->drawRgbLayout(frame.asRgbU8Layout(),0,0,true);  break;
00163     case GenericFrame::RGB_F32:  win->drawImage(frame.asRgbU8(),0,0,true);            break;
00164     case GenericFrame::GRAY_U8:  win->drawGrayLayout(frame.asGrayU8Layout(),0,0,true);break;
00165     case GenericFrame::GRAY_F32: win->drawImage(frame.asGrayU8(),0,0,true);           break;
00166     case GenericFrame::VIDEO:    win->drawImage(frame.asRgbU8(),0,0,true);            break;
00167     case GenericFrame::RGB_U16:        break;
00168     case GenericFrame::GRAY_U16:       break;
00169     }
00170 }
00171 
00172 // ######################################################################
00173 bool ImageDisplayStream::isVoid() const
00174 {
00175   return wmap && wmap->pressedCloseButton;
00176 }
00177 
00178 // ######################################################################
00179 void ImageDisplayStream::closeStream(const std::string& shortname)
00180 {
00181   if (wmap)
00182     {
00183       map_type::iterator itr = wmap->windows.find(shortname);
00184 
00185       if (itr != wmap->windows.end())
00186         (*itr).second->setVisible(false);
00187     }
00188 }
00189 
00190 // ######################################################################
00191 rutz::shared_ptr<XWinManaged>
00192 ImageDisplayStream::getWindow(const std::string& shortname)
00193 {
00194   if (wmap)
00195     {
00196       map_type::iterator itr = wmap->windows.find(shortname);
00197 
00198       if (itr != wmap->windows.end())
00199         return (*itr).second;
00200     }
00201 
00202   return rutz::shared_ptr<XWinManaged>();
00203 }
00204 
00205 // ######################################################################
00206 std::vector<rutz::shared_ptr<XWinManaged> > ImageDisplayStream::getWindows()
00207 {
00208   std::vector<rutz::shared_ptr<XWinManaged> > windows;
00209   if (wmap)
00210   {
00211     map_type::iterator itr;
00212     for(itr = wmap->windows.begin(); itr != wmap->windows.end(); itr++)
00213     {
00214       windows.push_back((*itr).second);
00215     }
00216   }
00217 
00218   return windows;
00219 
00220 }
00221 
00222 // ######################################################################
00223 /* So things look consistent in everyone's emacs... */
00224 /* Local Variables: */
00225 /* indent-tabs-mode: nil */
00226 /* End: */
00227 
00228 #endif // GUI_IMAGEDISPLAYSTREAM_C_DEFINED
Generated on Sun May 8 08:04:48 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3