envision-smoketest.c

Go to the documentation of this file.
00001 /*!@file Envision/envision-smoketest.c Run env_visual_cortex with random inputs to weed out bugs */
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/Envision/envision-smoketest.c $
00035 // $Id: envision-smoketest.c 14376 2011-01-11 02:44:34Z pez $
00036 //
00037 
00038 #ifndef ENVISION_ENVISION_SMOKETEST_C_DEFINED
00039 #define ENVISION_ENVISION_SMOKETEST_C_DEFINED
00040 
00041 #include "Envision/env_alloc.h"
00042 #include "Envision/env_c_math_ops.h"
00043 #include "Envision/env_image.h"
00044 #include "Envision/env_image_ops.h"
00045 #include "Envision/env_log.h"
00046 #include "Envision/env_params.h"
00047 #include "Envision/env_stdio_interface.h"
00048 #include "Envision/env_visual_cortex.h"
00049 
00050 #include <stdio.h>
00051 #include <stdlib.h> // for atoi(), malloc(), free()
00052 #include <sys/types.h> // for pid_t
00053 #include <sys/wait.h>
00054 #include <unistd.h> // for fork()
00055 
00056 // ######################################################################
00057 // Thunk to convert from env_size_t to size_t
00058 static void* malloc_thunk(env_size_t n)
00059 {
00060         return malloc(n);
00061 }
00062 
00063 // ######################################################################
00064 static void run_evc(char **fnames, size_t nnames)
00065 {
00066         struct env_params envp;
00067         env_params_set_defaults(&envp);
00068 
00069         envp.maxnorm_type = ENV_VCXNORM_MAXNORM;
00070         envp.scale_bits = 16;
00071 
00072         env_assert_set_handler(&env_stdio_assert_handler);
00073         env_allocation_init(&malloc_thunk, &free);
00074 
00075         struct env_visual_cortex ivc;
00076         env_visual_cortex_init(&ivc, &envp);
00077 
00078         for (int n = 0; n < nnames; ++n)
00079         {
00080                 struct env_dims indims;
00081                 struct env_rgb_pixel* input = 0;
00082 
00083                 input = env_stdio_parse_rgb(fnames[n], &indims);
00084 
00085                 struct env_image ivcout = env_img_initializer;
00086                 struct env_image intens = env_img_initializer;
00087                 struct env_image color = env_img_initializer;
00088                 struct env_image ori = env_img_initializer;
00089 #ifdef ENV_WITH_DYNAMIC_CHANNELS
00090                 struct env_image flicker = env_img_initializer;
00091                 struct env_image motion = env_img_initializer;
00092 #endif
00093 
00094                 env_visual_cortex_input(&ivc, &envp,
00095                                         "visualcortex",
00096                                         input, 0, indims,
00097                                         0,
00098                                         0,
00099                                         &ivcout,
00100                                         &intens, &color, &ori
00101 #ifdef ENV_WITH_DYNAMIC_CHANNELS
00102                                         , &flicker, &motion
00103 #endif
00104                         );
00105 
00106                 env_deallocate(input);
00107                 input = 0;
00108 
00109                 env_visual_cortex_rescale_ranges(
00110                         &ivcout, &intens, &color, &ori
00111 #ifdef ENV_WITH_DYNAMIC_CHANNELS
00112                         , &flicker, &motion
00113 #endif
00114                         );
00115 
00116                 env_img_make_empty(&ivcout);
00117                 env_img_make_empty(&intens);
00118                 env_img_make_empty(&color);
00119                 env_img_make_empty(&ori);
00120 #ifdef ENV_WITH_DYNAMIC_CHANNELS
00121                 env_img_make_empty(&flicker);
00122                 env_img_make_empty(&motion);
00123 #endif
00124         }
00125 
00126         env_visual_cortex_destroy(&ivc);
00127 
00128         env_allocation_cleanup();
00129 }
00130 
00131 // ######################################################################
00132 int main(int argc, const char** argv)
00133 {
00134         if (argc != 2)
00135         {
00136                 fprintf(stderr,
00137                         "usage: %s niter\n",
00138                         argv[0]);
00139                 return 1;
00140         }
00141 
00142         const int niter = atoi(argv[1]);
00143 
00144         const pid_t self = getpid();
00145 
00146         srand(self);
00147 
00148         for (int i = 0; i < niter; ++i)
00149         {
00150                 char fnames0[3][64];
00151                 char *fnames[3];
00152                 fnames[0] = &fnames0[0][0];
00153                 fnames[1] = &fnames0[1][0];
00154                 fnames[2] = &fnames0[2][0];
00155                 const int w = 1 + rand() % 2000;
00156                 const int h = 1 + rand() % 2000;
00157 
00158                 for (int n = 0; n < 3; ++n)
00159                 {
00160                         snprintf(fnames[n], sizeof(fnames[n]),
00161                                  "smoketest-%d-%d-%06d.pnm",
00162                                  (int) self, i, n);
00163 
00164                         FILE* f = fopen(fnames[n], "w");
00165                         if (f == 0)
00166                         {
00167                                 fprintf(stderr, "couldn't open %s for writing\n",
00168                                         fnames[n]);
00169                                 exit(-1);
00170                         }
00171 
00172                         fprintf(f, "P6\n%d %d\n255\n", w, h);
00173 
00174                         for (int x = 0; x < 3*w*h; ++x)
00175                         {
00176                                 const unsigned char val = rand() % 256;
00177                                 fwrite(&val, 1, 1, f);
00178                         }
00179 
00180                         fclose(f);
00181                 }
00182 
00183                 fprintf(stderr, "running iter %d/%d with dims %dx%d... ",
00184                         i+1, niter, w, h);
00185                 fflush(stderr);
00186 
00187                 const pid_t child = fork();
00188 
00189                 if (child < 0)
00190                 {
00191                         fprintf(stderr, "fork() failed\n");
00192                         exit(-1);
00193                 }
00194                 else if (child > 0)
00195                 {
00196                         // in parent
00197                         int status;
00198                         const pid_t p = waitpid(child, &status, 0);
00199                         if (p != child)
00200                         {
00201                                 fprintf(stderr, "waitpid() failed\n");
00202                                 exit(-1);
00203                         }
00204 
00205                         if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
00206                         {
00207                                 // ok, normal exit with status == 0
00208                                 for (int n = 0; n < 3; ++n)
00209                                 {
00210                                         if (unlink(fnames[n]) != 0)
00211                                         {
00212                                                 fprintf(stderr,
00213                                                         "unlink() failed\n");
00214                                                 exit(-1);
00215                                         }
00216                                 }
00217                                 fprintf(stderr, "iter %d/%d ok\n",
00218                                         i+1, niter);
00219                         }
00220                         else
00221                         {
00222                                 fprintf(stderr, "iter %d/%d FAILED!\n",
00223                                         i+1, niter);
00224                         }
00225 
00226                         continue;
00227                 }
00228 
00229                 // else... in child
00230 
00231                 run_evc(fnames, 3);
00232 
00233                 exit(0);
00234         }
00235 
00236         return 0;
00237 }
00238 
00239 // ######################################################################
00240 /* So things look consistent in everyone's emacs... */
00241 /* Local Variables: */
00242 /* indent-tabs-mode: nil */
00243 /* c-file-style: "linux" */
00244 /* End: */
00245 
00246 #endif // ENVISION_ENVISION_SMOKETEST_C_DEFINED
Generated on Sun May 8 08:04:45 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3