text2image.C

Go to the documentation of this file.
00001 /*!@file AppPsycho/text2image.C convert sentences from a text file to
00002    images  */
00003 
00004 // //////////////////////////////////////////////////////////////////// //
00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00006 // University of Southern California (USC) and the iLab at USC.         //
00007 // See http://iLab.usc.edu for information about this project.          //
00008 // //////////////////////////////////////////////////////////////////// //
00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00011 // in Visual Environments, and Applications'' by Christof Koch and      //
00012 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00013 // pending; application number 09/912,225 filed July 23, 2001; see      //
00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00015 // //////////////////////////////////////////////////////////////////// //
00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00017 //                                                                      //
00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00019 // redistribute it and/or modify it under the terms of the GNU General  //
00020 // Public License as published by the Free Software Foundation; either  //
00021 // version 2 of the License, or (at your option) any later version.     //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00026 // PURPOSE.  See the GNU General Public License for more details.       //
00027 //                                                                      //
00028 // You should have received a copy of the GNU General Public License    //
00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00031 // Boston, MA 02111-1307 USA.                                           //
00032 // //////////////////////////////////////////////////////////////////// //
00033 //
00034 // Primary maintainer for this file: David J. Berg <dberg@usc.edu>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppPsycho/text2image.C $
00036 
00037 #include "Component/ModelManager.H"
00038 #include "Component/ComponentOpts.H"
00039 #include "Image/Image.H"
00040 #include "Image/DrawOps.H"
00041 #include "Image/Pixels.H"
00042 #include "Image/SimpleFont.H"
00043 #include "Util/Types.H"
00044 #include "Util/StringConversions.H"
00045 #include "Util/StringUtil.H"
00046 #include "Media/FrameSeries.H"
00047 #include "Transport/FrameInfo.H"
00048 
00049 #include <fstream>
00050 
00051 #define HDEG 54.9
00052 // ######################################################################
00053 extern "C" int main(const int argc, char** argv)
00054 {
00055   MYLOGVERB = LOG_INFO;  // suppress debug messages
00056 
00057   // Instantiate a ModelManager:
00058   ModelManager manager("Create Images");
00059 
00060   nub::soft_ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00061   manager.addSubComponent(ofs);
00062 
00063   // Parse command-line:
00064   if (manager.parseCommandLine(argc, argv,
00065                                "<textfile> visual-angle-of-single-character",
00066                                1, 2)==false)
00067     return(1);
00068 
00069   double fontsize = fromStr<double>(manager.getExtraArg(1));
00070 
00071   // let's get all our ModelComponent instances started:
00072   manager.start();
00073 
00074   // create an image frame for each sentence in our text file and store
00075   // it in a vector before we start the experiment, then we can just
00076   // present each frame like in psycho still
00077   //
00078   //First read the text file and all the sentences
00079   //load our file
00080   std::ifstream *itsFile;
00081   itsFile = new std::ifstream(manager.getExtraArg(0).c_str());
00082 
00083   //error if no file
00084   if (itsFile->is_open() == false)
00085     LFATAL("Cannot open '%s' for reading",manager.getExtraArg(0).c_str());
00086 
00087   //some storage variables
00088   std::string line;
00089   std::vector<std::vector<std::string> > lines;
00090   std::vector<uint> itsType;
00091   uint scount = 0;
00092 
00093   //loop through lines of file
00094   while (!itsFile->eof())
00095     {
00096       getline(*itsFile, line);
00097 
00098       std::vector<std::string> temp;
00099       //store the sentence and type (question or statement)
00100       if (line[0] == '#')//question
00101         {
00102           line.erase(0,1);
00103           temp.push_back(line);
00104           lines.push_back(temp);
00105           itsType.push_back(1);
00106           scount++;
00107         }
00108       else if (line[0] =='!')//sentence
00109         {
00110           line.erase(0,1);
00111           temp.push_back(line);
00112           lines.push_back(temp);
00113           itsType.push_back(0);
00114           scount++;
00115         }
00116       else
00117         {
00118           if (line.size() > 1)
00119             {
00120               scount--;
00121               lines[scount].push_back(line);
00122               scount++;
00123             }
00124         }
00125     }
00126   itsFile->close();
00127 
00128   //now we have stored all of our sentences, lets create our images
00129   int w = 1920;
00130   int h = 1080;
00131   uint fontwidth = uint(fontsize * w / HDEG);
00132   SimpleFont fnt = SimpleFont::fixedMaxWidth(fontwidth); //font
00133   std::vector<Image<PixRGB<byte> > > itsImage; //store sentences
00134 
00135   for (uint i = 0; i < lines.size(); i++)
00136     {
00137       int space = 0;
00138       int hanchor = int(h/2) - int(fnt.h()/2);
00139       Image<PixRGB<byte> > timage(w,h,ZEROS);
00140       PixRGB<byte> gr(128,128,128);
00141       timage += gr;
00142 
00143       for (uint j = 0; j < lines[i].size(); j++)
00144         {
00145           if (j < 1)
00146             space = int( double(w - fnt.w() * lines[i][j].size()) / 2.0 );
00147           if (j > 0)
00148             hanchor = hanchor + fnt.h();
00149           Point2D<int> tanchor(space, hanchor);
00150           writeText(timage,tanchor,lines[i][j].c_str(),
00151                     PixRGB<byte>(0,0,0),
00152                     gr,
00153                     fnt);
00154         }
00155 
00156       itsImage.push_back(timage);
00157     }
00158 
00159 
00160   //ok, now right out the image
00161   for (uint i = 0; i < itsImage.size(); i ++)
00162     {
00163 
00164       //check for void OFS
00165       if (ofs->becameVoid())
00166         {
00167           LINFO("quitting because output stream was closed or became void");
00168           return 0;
00169         }
00170 
00171       //update ofs
00172       const FrameState os = ofs->updateNext();
00173 
00174       //write out image
00175       ofs->writeRGB(itsImage[i], "output",
00176                     FrameInfo("Text imbeded image",SRC_POS));
00177 
00178       //last frame?
00179       if (os == FRAME_FINAL)
00180         break;
00181 
00182     }
00183 
00184 
00185   // stop all our ModelComponents
00186   manager.stop();
00187 
00188   // all done!
00189   return 0;
00190 }
00191 
00192 // ######################################################################
00193 /* So things look consistent in everyone's emacs... */
00194 /* Local Variables: */
00195 /* indent-tabs-mode: nil */
00196 /* End: */
Generated on Sun May 8 08:40:11 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3