PlaintextWriter.C

Go to the documentation of this file.
00001 /*!@file Raster/PlaintextWriter.C Plaintext image-file writer class; resulting files usable with matlab's 'load' */
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/Raster/PlaintextWriter.C $
00035 // $Id: PlaintextWriter.C 8312 2007-04-27 22:05:07Z rjpeters $
00036 //
00037 
00038 #ifndef RASTER_PLAINTEXTWRITER_C_DEFINED
00039 #define RASTER_PLAINTEXTWRITER_C_DEFINED
00040 
00041 #include "Raster/PlaintextWriter.H"
00042 
00043 #include "Image/Image.H"
00044 #include "Image/Pixels.H"
00045 #include "Raster/GenericFrame.H"
00046 #include "Util/log.H"
00047 
00048 #include <cstdio>
00049 
00050 // ######################################################################
00051 PlaintextWriter::PlaintextWriter()
00052 {}
00053 
00054 // ######################################################################
00055 PlaintextWriter::~PlaintextWriter()
00056 {}
00057 
00058 // ######################################################################
00059 std::string PlaintextWriter::writeFrame(const GenericFrame& image,
00060                                         const std::string& fname)
00061 {
00062   switch (image.nativeType())
00063     {
00064     case GenericFrame::NONE:    // fall-through
00065     case GenericFrame::VIDEO:   // fall-through
00066     case GenericFrame::RGB_U8:
00067       writePlaintextRgbU8(image.asRgbU8(), fname);
00068       break;
00069 
00070     case GenericFrame::RGB_F32:
00071       writePlaintextRgbF32(image.asRgbF32(), fname);
00072       break;
00073 
00074     case GenericFrame::GRAY_U8:
00075       writePlaintextGrayU8(image.asGrayU8(), fname);
00076       break;
00077 
00078     case GenericFrame::GRAY_F32:
00079       writePlaintextGrayF32(image.asGrayF32(), fname);
00080       break;
00081 
00082     default:
00083       LFATAL("unknown GenericFrame::NativeType value %d",
00084              int(image.nativeType()));
00085     }
00086 
00087   return fname;
00088 }
00089 
00090 // ######################################################################
00091 void PlaintextWriter::writePlaintextRgbU8(const Image<PixRGB<byte> >& img,
00092                                           const std::string& fname)
00093 {
00094   FILE* f = fopen(fname.c_str(), "w");
00095   if (f == 0)
00096     LFATAL("couldn't open %s for writing", fname.c_str());
00097 
00098   const int w = img.getWidth();
00099   const int h = img.getHeight();
00100   Image<PixRGB<byte> >::const_iterator itr = img.begin();
00101   for (int y = 0; y < h; ++y)
00102     {
00103       const char* sep = "";
00104       for (int x = 0; x < w; ++x)
00105         {
00106           fprintf(f, "%s%3d %3d %3d", sep, (*itr).p[0], (*itr).p[1], (*itr).p[2]);
00107           sep = "  ";
00108           ++itr;
00109         }
00110       fprintf(f, "\n");
00111     }
00112 
00113   fclose(f);
00114 }
00115 
00116 // ######################################################################
00117 void PlaintextWriter::writePlaintextRgbF32(const Image<PixRGB<float> >& img,
00118                                            const std::string& fname)
00119 {
00120   FILE* f = fopen(fname.c_str(), "w");
00121   if (f == 0)
00122     LFATAL("couldn't open %s for writing", fname.c_str());
00123 
00124   const int w = img.getWidth();
00125   const int h = img.getHeight();
00126   Image<PixRGB<float> >::const_iterator itr = img.begin();
00127   for (int y = 0; y < h; ++y)
00128     {
00129       const char* sep = "";
00130       for (int x = 0; x < w; ++x)
00131         {
00132           fprintf(f, "%s%.9e %.9e %.9e", sep, (*itr).p[0], (*itr).p[1], (*itr).p[2]);
00133           sep = "  ";
00134           ++itr;
00135         }
00136       fprintf(f, "\n");
00137     }
00138 
00139   fclose(f);
00140 }
00141 
00142 // ######################################################################
00143 void PlaintextWriter::writePlaintextGrayU8(const Image<byte>& img,
00144                                            const std::string& fname)
00145 {
00146   FILE* f = fopen(fname.c_str(), "w");
00147   if (f == 0)
00148     LFATAL("couldn't open %s for writing", fname.c_str());
00149 
00150   const int w = img.getWidth();
00151   const int h = img.getHeight();
00152   Image<byte>::const_iterator itr = img.begin();
00153   for (int y = 0; y < h; ++y)
00154     {
00155       const char* sep = "";
00156       for (int x = 0; x < w; ++x)
00157         {
00158           fprintf(f, "%s%3d", sep, *itr);
00159           sep = " ";
00160           ++itr;
00161         }
00162       fprintf(f, "\n");
00163     }
00164 
00165   fclose(f);
00166 }
00167 
00168 // ######################################################################
00169 void PlaintextWriter::writePlaintextGrayF32(const Image<float>& img,
00170                                             const std::string& fname)
00171 {
00172   FILE* f = fopen(fname.c_str(), "w");
00173   if (f == 0)
00174     LFATAL("couldn't open %s for writing", fname.c_str());
00175 
00176   const int w = img.getWidth();
00177   const int h = img.getHeight();
00178   Image<float>::const_iterator itr = img.begin();
00179   for (int y = 0; y < h; ++y)
00180     {
00181       const char* sep = "";
00182       for (int x = 0; x < w; ++x)
00183         {
00184           fprintf(f, "%s%.9e", sep, *itr);
00185           sep = " ";
00186           ++itr;
00187         }
00188       fprintf(f, "\n");
00189     }
00190 
00191   fclose(f);
00192 }
00193 
00194 // ######################################################################
00195 /* So things look consistent in everyone's emacs... */
00196 /* Local Variables: */
00197 /* mode: c++ */
00198 /* indent-tabs-mode: nil */
00199 /* End: */
00200 
00201 #endif // RASTER_PLAINTEXTWRITER_C_DEFINED
Generated on Sun May 8 08:41:16 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3