TigsInputFrame.C

Go to the documentation of this file.
00001 /*!@file TIGS/TigsInputFrame.C Class that lets us do lazy computation of luminance/rg/by from an input frame */
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/TIGS/TigsInputFrame.C $
00035 // $Id: TigsInputFrame.C 8297 2007-04-25 00:26:17Z rjpeters $
00036 //
00037 
00038 #ifndef TIGS_TIGSINPUTFRAME_C_DEFINED
00039 #define TIGS_TIGSINPUTFRAME_C_DEFINED
00040 
00041 #include "TIGS/TigsInputFrame.H"
00042 
00043 #include "Image/ColorOps.H"
00044 #include "Util/StringConversions.H"
00045 #include "rutz/trace.h"
00046 
00047 #include <sstream>
00048 
00049 // specialization of rescale() for PixRGB<byte>; here we unpack the
00050 // PixRGB operations so that they can be better optimized by the
00051 // compiler for at least a 2x speedup -- unfortunately (with gcc
00052 // anyway) the overloaded PixRGB operators (i.e. PixRGB+PixRGB,
00053 // PixRGB*float, etc.) make for slow code, so here we unpack things
00054 // and do the interpolation one element at a time using builtin,
00055 // scalar arithmetic only
00056 Image<PixRGB<float> > rescaleAndPromote(const Image<PixRGB<byte> >& src,
00057                                         const int new_w, const int new_h)
00058 {
00059 GVX_TRACE(__PRETTY_FUNCTION__);
00060 
00061   ASSERT(src.initialized()); ASSERT(new_w > 0 && new_h > 0);
00062 
00063   const int orig_w = src.getWidth();
00064   const int orig_h = src.getHeight();
00065 
00066   // check if same size already
00067   if (new_w == orig_w && new_h == orig_h) return src;
00068 
00069   const float sw = float(orig_w) / float(new_w);
00070   const float sh = float(orig_h) / float(new_h);
00071 
00072   Image<PixRGB<float> > result(new_w, new_h, NO_INIT);
00073   Image<PixRGB<float> >::iterator dptr = result.beginw();
00074   Image<PixRGB<byte> >::const_iterator const sptr = src.begin();
00075 
00076   for (int j = 0; j < new_h; ++j)
00077     {
00078       const float y = std::max(0.0f, (j+0.5f) * sh - 0.5f);
00079 
00080       const int y0 = int(y);
00081       const int y1 = std::min(y0 + 1, orig_h - 1);
00082 
00083       const float fy = y - float(y0);
00084 
00085       const int wy0 = orig_w * y0;
00086       const int wy1 = orig_w * y1;
00087 
00088       for (int i = 0; i < new_w; ++i)
00089         {
00090           const float x = std::max(0.0f, (i+0.5f) * sw - 0.5f);
00091 
00092           const int x0 = int(x);
00093           const int x1 = std::min(x0 + 1, orig_w - 1);
00094 
00095           const float fx = x - float(x0);
00096 
00097 #define RGB_BILINEAR_INTERP(EL)                                 \
00098   do {                                                          \
00099     const float                                                 \
00100       d00( sptr[x0 + wy0].p[EL] ), d10( sptr[x1 + wy0].p[EL] ), \
00101       d01( sptr[x0 + wy1].p[EL] ), d11( sptr[x1 + wy1].p[EL] ); \
00102                                                                 \
00103     const float                                                 \
00104       dx0( d00 + (d10 - d00) * fx ),                            \
00105       dx1( d01 + (d11 - d01) * fx );                            \
00106                                                                 \
00107     dptr->p[EL] = float( int( dx0 + (dx1 - dx0) * fy ) );       \
00108   } while(0)
00109 
00110           // yes, I'm doing that funny float(byte()) conversion on
00111           // purpose, in order to maintain backward compatibility with
00112           // when I used to do
00113           // Image<PixRGB<float>>(rescale(byteimage))
00114 
00115           RGB_BILINEAR_INTERP(0);
00116           RGB_BILINEAR_INTERP(1);
00117           RGB_BILINEAR_INTERP(2);
00118 
00119 #undef RGB_BILINEAR_INTERP
00120 
00121           ++dptr;
00122         }
00123     }
00124   return result;
00125 }
00126 
00127 rutz::shared_ptr<TigsInputFrame>
00128 TigsInputFrame::fromGhostString(const std::string& s)
00129 {
00130   std::istringstream iss(s);
00131   int64 nanosecs;
00132   std::string dimsstr;
00133   std::string hashstr;
00134   iss >> nanosecs >> dimsstr >> hashstr;
00135 
00136   const Dims dims = fromStr<Dims>(dimsstr);
00137   const Digest<16> hash = Digest<16>::fromString(hashstr);
00138 
00139   return rutz::shared_ptr<TigsInputFrame>
00140     (new TigsInputFrame(SimTime::NSECS(nanosecs), dims, hash));
00141 }
00142 
00143 std::string TigsInputFrame::toGhostString() const
00144 {
00145   std::ostringstream oss;
00146   oss << itsTime.nsecs()
00147       << ' ' << convertToString(itsOrigbounds.dims())
00148       << ' ' << this->getHash().asString();
00149   return oss.str();
00150 }
00151 
00152 void TigsInputFrame::initialize() const
00153 {
00154   if (itsLum.initialized()
00155       && itsRG.initialized()
00156       && itsBY.initialized()
00157       && itsRGB.initialized())
00158     return;
00159 
00160   GVX_TRACE(__PRETTY_FUNCTION__);
00161 
00162   const Image<PixRGB<float> > fframe =
00163     rescaleAndPromote(itsOrigframe, 512, 512);
00164 
00165   itsRGB = Image<PixRGB<byte> >(fframe);
00166 
00167   itsLum = luminance(fframe);
00168 
00169   getRGBYsimple(fframe, itsRG, itsBY, float(5.0f));
00170 
00171   {GVX_TRACE("re-range");
00172   itsRG += 1.0f;
00173   itsBY += 1.0f;
00174   itsRG *= 127.5f;
00175   itsBY *= 127.5f;
00176   }
00177 }
00178 
00179 // ######################################################################
00180 /* So things look consistent in everyone's emacs... */
00181 /* Local Variables: */
00182 /* mode: c++ */
00183 /* indent-tabs-mode: nil */
00184 /* End: */
00185 
00186 #endif // TIGS_TIGSINPUTFRAME_C_DEFINED
Generated on Sun May 8 08:42:23 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3