whitebox-Raster.C

Go to the documentation of this file.
00001 /*!@file TestSuite/whitebox-Raster.C Test Raster class */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // 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@klab.caltech.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/TestSuite/whitebox-Raster.C $
00035 // $Id: whitebox-Raster.C 8630 2007-07-25 20:33:41Z rjpeters $
00036 //
00037 
00038 #include "Image/ColorOps.H"
00039 #include "Image/IO.H"
00040 #include "Image/Image.H"
00041 #include "Image/MathOps.H"
00042 #include "Image/Pixels.H"
00043 #include "Image/Transforms.H" // for makeBinary()
00044 #include "Raster/GenericFrame.H"
00045 #include "Raster/PfmParser.H"
00046 #include "Raster/PfmWriter.H"
00047 #include "Raster/Raster.H"
00048 #include "TestSuite/TestSuite.H"
00049 #include "Util/log.H"
00050 #include "Util/sformat.H"
00051 #include "rutz/rand.h"
00052 
00053 #include <algorithm> // for std::generate()
00054 #include <deque> // for pfm tags
00055 #include <limits> // for std::numeric_limits<>
00056 #include <string>
00057 #include <unistd.h> // for getpid(), unlink()
00058 
00059 namespace
00060 {
00061 
00062   class TempFile
00063   {
00064     static int theCounter;
00065     std::string itsName;
00066   public:
00067     TempFile(const char* ext = "")
00068       :
00069       itsName(sformat("test-Raster-%d-tmp-%d.%s",
00070                       int(getpid()), theCounter++, ext))
00071     {}
00072 
00073     ~TempFile()
00074     {
00075       unlink(itsName.c_str());
00076     }
00077 
00078     const char* name() const { return itsName.c_str(); }
00079   };
00080 
00081   int TempFile::theCounter = 1;
00082 
00083   template <class T>
00084   struct Randomizer
00085   {
00086     T top;
00087 
00088     Randomizer(T val = std::numeric_limits<T>::max()) : top(val) {}
00089 
00090     T operator()() const
00091     {
00092       return T( randomDouble() * double(top) );;
00093     }
00094   };
00095 
00096   template <class T>
00097   struct Randomizer<PixRGB<T> >
00098   {
00099     Randomizer<T> randomT;
00100 
00101     PixRGB<T> operator()() const
00102     {
00103       return PixRGB<T>(randomT(), randomT(), randomT());
00104     }
00105   };
00106 
00107   template <>
00108   struct Randomizer<float>
00109   {
00110     float operator()() const
00111     {
00112       return float(255.0f*randomDouble());
00113     }
00114   };
00115 
00116   template <class T>
00117   Image<T> randImage(int w, int h)
00118   {
00119     Image<T> result(w, h, NO_INIT);
00120 
00121     std::generate(result.beginw(), result.endw(), Randomizer<T>());
00122 
00123     return result;
00124   }
00125 
00126   template <class T>
00127   Image<T> randImage()
00128   {
00129     Randomizer<int> r(500);
00130 
00131     return randImage<T>(r()+10, r()+10);
00132   }
00133 }
00134 
00135 ///////////////////////////////////////////////////////////////////////
00136 //
00137 // Test functions
00138 //
00139 ///////////////////////////////////////////////////////////////////////
00140 
00141 static void Raster_xx_img_gray_write_bw_read_gray_xx_1(TestSuite& suite)
00142 {
00143   /* img gray */ Image<byte> i1 = randImage<byte>();
00144 
00145   /* write bw */ TempFile tf("pbm"); Raster::WriteGray(i1, tf.name());
00146 
00147   /* read gray */ Image<byte> i2 = Raster::ReadGray(tf.name());
00148 
00149   REQUIRE_EQ(i2, makeBinary(i1, byte(127), byte(0), byte(255)));
00150 }
00151 
00152 static void Raster_xx_img_gray_write_gray_read_gray_xx_1(TestSuite& suite)
00153 {
00154   /* img gray */ Image<byte> i1 = randImage<byte>();
00155 
00156   /* write gray */ TempFile tf("pgm"); Raster::WriteGray(i1, tf.name());
00157 
00158   /* read gray */ Image<byte> i2 = Raster::ReadGray(tf.name());
00159 
00160   REQUIRE_EQ(i2, i1);
00161 }
00162 
00163 static void Raster_xx_img_gray_write_gray_read_rgb_xx_1(TestSuite& suite)
00164 {
00165   /* img gray */ Image<byte> i1 = randImage<byte>();
00166 
00167   /* write gray */ TempFile tf("pgm"); Raster::WriteGray(i1, tf.name());
00168 
00169   /* read rgb */ Image<PixRGB<byte> > i2 = Raster::ReadRGB(tf.name());
00170 
00171   Image<byte> r, g, b; getComponents(i2, r, g, b);
00172   REQUIRE_EQ(r, i1);
00173   REQUIRE_EQ(g, i1);
00174   REQUIRE_EQ(b, i1);
00175 }
00176 
00177 static void Raster_xx_img_gray_write_rgb_read_gray_xx_1(TestSuite& suite)
00178 {
00179   /* img gray */ Image<byte> i1 = randImage<byte>();
00180 
00181   /* write rgb */ TempFile tf("ppm"); Raster::WriteRGB(i1, tf.name());
00182 
00183   /* read gray */ Image<byte> i2 = Raster::ReadGray(tf.name());
00184 
00185   REQUIRE_EQ(i2, i1);
00186 }
00187 
00188 static void Raster_xx_img_gray_write_rgb_read_rgb_xx_1(TestSuite& suite)
00189 {
00190   /* img gray */ Image<byte> i1 = randImage<byte>();
00191 
00192   /* write rgb */ TempFile tf("ppm"); Raster::WriteRGB(i1, tf.name());
00193 
00194   /* read rgb */ Image<PixRGB<byte> > i2 = Raster::ReadRGB(tf.name());
00195 
00196   Image<byte> r, g, b; getComponents(i2, r, g, b);
00197   REQUIRE_EQ(r, i1);
00198   REQUIRE_EQ(g, i1);
00199   REQUIRE_EQ(b, i1);
00200 }
00201 
00202 static void Raster_xx_img_rgb_write_gray_read_gray_xx_1(TestSuite& suite)
00203 {
00204   /* img rgb */ Image<PixRGB<byte> > i1 = randImage<PixRGB<byte> >();
00205 
00206   /* write gray */ TempFile tf("pgm"); Raster::WriteGray(luminance(i1), tf.name());
00207 
00208   /* read gray */ Image<byte> i2 = Raster::ReadGray(tf.name());
00209 
00210   REQUIRE_EQ(i2, luminance(i1));
00211 }
00212 
00213 static void Raster_xx_img_rgb_write_gray_read_rgb_xx_1(TestSuite& suite)
00214 {
00215   /* img rgb */ Image<PixRGB<byte> > i1 = randImage<PixRGB<byte> >();
00216 
00217   /* write gray */ TempFile tf("pgm"); Raster::WriteGray(luminance(i1), tf.name());
00218 
00219   /* read rgb */ Image<PixRGB<byte> > i2 = Raster::ReadRGB(tf.name());
00220 
00221   REQUIRE_EQ(luminance(i2), luminance(i1));
00222 }
00223 
00224 static void Raster_xx_img_rgb_write_rgb_read_gray_xx_1(TestSuite& suite)
00225 {
00226   /* img rgb */ Image<PixRGB<byte> > i1 = randImage<PixRGB<byte> >();
00227 
00228   /* write rgb */ TempFile tf("ppm"); Raster::WriteRGB(i1, tf.name());
00229 
00230   /* read gray */ Image<byte> i2 = Raster::ReadGray(tf.name());
00231 
00232   REQUIRE_EQ(i2, luminance(i1));
00233 }
00234 
00235 static void Raster_xx_img_rgb_write_rgb_read_rgb_xx_1(TestSuite& suite)
00236 {
00237   /* img rgb */ Image<PixRGB<byte> > i1 = randImage<PixRGB<byte> >();
00238 
00239   /* write rgb */ TempFile tf("ppm"); Raster::WriteRGB(i1, tf.name());
00240 
00241   /* read rgb */ Image<PixRGB<byte> > i2 = Raster::ReadRGB(tf.name());
00242 
00243   REQUIRE_EQ(i2, i1);
00244 }
00245 
00246 static void Raster_xx_img_float_write_float_read_float_xx_1(TestSuite& suite)
00247 {
00248   /* img float */ Image<float> i1 = randImage<float>();
00249 
00250   /* write float */ TempFile tf("pfm"); Raster::WriteFloat(i1, 0, tf.name());
00251 
00252   /* read float */ Image<float> i2 = Raster::ReadFloat(tf.name());
00253 
00254   REQUIRE_EQ(i2, i1);
00255 }
00256 
00257 static void Raster_xx_pfm_tags_xx_1(TestSuite& suite)
00258 {
00259   const Image<float> i1 = randImage<float>();
00260 
00261   std::deque<std::string> tagNames;
00262   std::deque<std::string> tagValues;
00263 
00264   tagNames.push_back("test tag name #1");
00265   tagValues.push_back("test tag value #1");
00266 
00267   tagNames.push_back("test tag name #2");
00268   tagValues.push_back("test tag value #2");
00269 
00270   TempFile tf("pfm");
00271 
00272   PfmWriter::writeFloat(i1, tf.name(), tagNames, tagValues);
00273 
00274   PfmParser parser(tf.name());
00275 
00276   REQUIRE_EQ(parser.getTagCount(), uint(2));
00277 
00278   std::string name, value;
00279 
00280   REQUIRE_EQ(parser.getTag(0, name, value), true);
00281   REQUIRE_EQ(name, tagNames[0]);
00282   REQUIRE_EQ(value, tagValues[0]);
00283 
00284   REQUIRE_EQ(parser.getTag(1, name, value), true);
00285   REQUIRE_EQ(name, tagNames[1]);
00286   REQUIRE_EQ(value, tagValues[1]);
00287 
00288   REQUIRE_EQ(parser.getComments(), std::string());
00289 
00290   const Image<float> i2 = parser.getFrame().asFloat();
00291 
00292   REQUIRE_EQ(i1, i2);
00293 }
00294 
00295 
00296 ///////////////////////////////////////////////////////////////////////
00297 //
00298 // main
00299 //
00300 ///////////////////////////////////////////////////////////////////////
00301 
00302 int main(int argc, const char** argv)
00303 {
00304   initRandomNumbers();
00305 
00306   TestSuite suite;
00307 
00308   suite.ADD_TEST(Raster_xx_img_gray_write_bw_read_gray_xx_1);
00309   suite.ADD_TEST(Raster_xx_img_gray_write_gray_read_gray_xx_1);
00310   suite.ADD_TEST(Raster_xx_img_gray_write_gray_read_rgb_xx_1);
00311   suite.ADD_TEST(Raster_xx_img_gray_write_rgb_read_gray_xx_1);
00312   suite.ADD_TEST(Raster_xx_img_gray_write_rgb_read_rgb_xx_1);
00313   suite.ADD_TEST(Raster_xx_img_rgb_write_gray_read_gray_xx_1);
00314   suite.ADD_TEST(Raster_xx_img_rgb_write_gray_read_rgb_xx_1);
00315   suite.ADD_TEST(Raster_xx_img_rgb_write_rgb_read_gray_xx_1);
00316   suite.ADD_TEST(Raster_xx_img_rgb_write_rgb_read_rgb_xx_1);
00317   suite.ADD_TEST(Raster_xx_img_float_write_float_read_float_xx_1);
00318   suite.ADD_TEST(Raster_xx_pfm_tags_xx_1);
00319 
00320   suite.parseAndRun(argc, argv);
00321 
00322   return 0;
00323 }
00324 
00325 // ######################################################################
00326 /* So things look consistent in everyone's emacs... */
00327 /* Local Variables: */
00328 /* indent-tabs-mode: nil */
00329 /* End: */
Generated on Sun May 8 08:42:23 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3