Main Page   Data Structures   File List   Data Fields   Globals  

tclSadie.c

Go to the documentation of this file.
00001 /*
00002 ##########################################
00003 # tclSadie.c.in -
00004 #   Template for creating tclSadie.c, the main tclSadie program
00005 #   file.  tclSadie.c calls tcl and tk libraries and sets up
00006 #   commands for calling SADIE functions through tcl.
00007 #
00008 # RCS: $Id: tclSadie.c,v 2.5 1999/09/11 03:36:14 gopalan Exp $ 
00009 #
00010 # Digital Image Analysis Lab
00011 # Dept. of Electrical and Computer Engineering
00012 # University of Arizona
00013 ##########################################
00014 */
00015 
00016 #include <stdio.h>
00017 #include <tcl.h>
00018 #include <tk.h>
00019 #include "sadie.h"
00020 
00021 /* RCS Indentification information */
00022 static char rcsid[] =
00023   "$Id: tclSadie.c,v 2.5 1999/09/11 03:36:14 gopalan Exp $";
00024 
00025 static int FLAG = 0;
00026 static int DIRECTORY = 1;
00027 static int CMDFILE = 2;
00028 
00029 static int libhomeset = 0;
00030 static char libhome[200];
00031 static int nogui = 0;
00032 static char cmdfile[200];
00033 static int cmdfileset = 0;
00034 static char *ProgName;
00035 
00036 /*-------------------------------------------------------------*/
00037 /* Global Sadie variables that must be set from within Tcl/Tk  */
00038 /*-------------------------------------------------------------*/
00039 short nlev;
00040 short csize;
00041 double weight;
00042 double *count;
00043 PIXEL gain;
00044 PIXEL bias;
00045 PIXEL gmin;
00046 PIXEL gmax;
00047 PIXEL thresh;
00048 PIXEL gbrk[2][4];
00049 PIXEL *table;
00050 /* -------------------------------------------------------------------------- */
00051 
00052 char *msg_ptr;
00053 Tcl_Interp *MainTclInterp;
00054 
00055 int Sadie_FileIO_Init (Tcl_Interp * interp);
00056 int Sadie_General_Init (Tcl_Interp * interp);
00057 int Sadie_Plot_Init (Tcl_Interp * interp);
00058 int Sadie_NewFunctions_Init (Tcl_Interp * interp);
00059 int Sadie_Contrast_Init (Tcl_Interp * interp);
00060 int Sadie_Filter_Init (Tcl_Interp * interp);
00061 int Sadie_Geometry_Init (Tcl_Interp * interp);
00062 int Sadie_Multi_Init (Tcl_Interp * interp);
00063 int Sadie_Classify_Init (Tcl_Interp * interp);
00064 int Sadie_Tools_Init (Tcl_Interp * interp);
00065 int Sadie_Image_Init (Tcl_Interp * interp);
00066 
00067 int Sadie_Proto_Init (Tcl_Interp * interp);
00068 
00069 /*-Copyright Information------------------------------------------------------*/
00070 /* Copyright (c) 1999 by the University of Arizona Digital Image Analysis Lab */
00071 /*----------------------------------------------------------------------------*/
00072 /*-General Information--------------------------------------------------------*/
00073 /*                                                                            */
00074 /*   This function displays the command line options for use when invoking    */
00075 /*   tclSadie.                                                                */
00076 /*                                                                            */
00077 /*----------------------------------------------------------------------------*/
00078 /*-Interface Information------------------------------------------------------*/
00079 void
00080 usage (void)
00081 {
00082   fprintf (stderr, "\nUsage: %s [options]\n", ProgName);
00083   fprintf (stderr, "Options:\n");
00084   fprintf (stderr, "\t-d <tclSadie library directory>\n");
00085   fprintf (stderr, "\t-f <command script file>\n");
00086   fprintf (stderr, "\t-nogui\trun tclSadie without a graphical interface\n");
00087   fprintf (stderr, "\t-h\tprint this message\n");
00088   fprintf (stderr, "\n");
00089 }
00090 
00091 /*-Copyright Information------------------------------------------------------*/
00092 /* Copyright (c) 1999 by the University of Arizona Digital Image Analysis Lab */
00093 /*----------------------------------------------------------------------------*/
00094 /*-General Information--------------------------------------------------------*/
00095 /*                                                                            */
00096 /*   This function checks to see if a file exists.                            */
00097 /*                                                                            */
00098 /*----------------------------------------------------------------------------*/
00099 /*-Interface Information------------------------------------------------------*/
00100 int
00101 verifyfile (Tcl_Interp * interp,        /*  I  The Tcl/Tk command interpreter.                    */
00102             char *name          /*  I  The name of the file.                              */
00103   )
00104 {
00105   Tcl_Obj *cmds = NULL;
00106   char tempstr[250];
00107   int result;
00108 
00109   sprintf (tempstr, "file exists %s", name);
00110   cmds = Tcl_NewStringObj (tempstr, -1);
00111   Tcl_EvalObj (interp, cmds);
00112   Tcl_GetIntFromObj (interp, Tcl_GetObjResult (interp), &result);
00113 
00114   return result;
00115 }
00116 
00117 /*-Copyright Information------------------------------------------------------*/
00118 /* Copyright (c) 1999 by the University of Arizona Digital Image Analysis Lab */
00119 /*----------------------------------------------------------------------------*/
00120 /*-General Information--------------------------------------------------------*/
00121 /*                                                                            */
00122 /*   This function checks to see if the environment variable TCLSADIE_HOME    */
00123 /*   contains the location of the .tcl files needed by tclSadie.              */
00124 /*                                                                            */
00125 /*----------------------------------------------------------------------------*/
00126 /*-Interface Information------------------------------------------------------*/
00127 int
00128 setlibdir_env (Tcl_Interp * interp)
00129 {
00130   Tcl_Obj *cmds = NULL;
00131   char *libdir = NULL;
00132   int result;
00133 
00134   /* Verify that the environment variable TCLSADIE_HOME exists */
00135   cmds = Tcl_NewStringObj ("global env; info exists env(TCLSADIE_HOME)", -1);
00136   Tcl_EvalObj (interp, cmds);
00137   Tcl_GetIntFromObj (interp, Tcl_GetObjResult (interp), &result);
00138 
00139   /* Read the environment variable TCLSADIE_HOME */
00140   if (result)
00141     {
00142       cmds = Tcl_NewStringObj ("global env; set env(TCLSADIE_HOME)", -1);
00143       Tcl_EvalObj (interp, cmds);
00144       libdir = Tcl_GetStringFromObj (Tcl_GetObjResult (interp), NULL);
00145       sprintf (libhome, "%s", libdir);
00146     }
00147 
00148   return result;
00149 }
00150 
00151 /*-Copyright Information------------------------------------------------------*/
00152 /* Copyright (c) 1999 by the University of Arizona Digital Image Analysis Lab */
00153 /*----------------------------------------------------------------------------*/
00154 /*-General Information--------------------------------------------------------*/
00155 /*                                                                            */
00156 /*   This function computes the default location of the .tcl files needed by  */
00157 /*   tclSadie, based on the location of the executable.                       */
00158 /*                                                                            */
00159 /*----------------------------------------------------------------------------*/
00160 /*-Interface Information------------------------------------------------------*/
00161 int
00162 setlibdir_default (Tcl_Interp * interp)
00163 {
00164   Tcl_Obj *cmds = NULL;
00165   char *libdirroot = NULL;
00166   int result;
00167 
00168   /* Read the environment variable TCLSADIE_HOME */
00169   cmds = Tcl_NewStringObj ("file dirname [info nameofexecutable]", -1);
00170   Tcl_EvalObj (interp, cmds);
00171   libdirroot = Tcl_GetStringFromObj (Tcl_GetObjResult (interp), NULL);
00172   sprintf (libhome, "%s/../lib/tclSadie", libdirroot);
00173 
00174   return 1;
00175 }
00176 
00177 /*-Copyright Information------------------------------------------------------*/
00178 /* Copyright (c) 1999 by the University of Arizona Digital Image Analysis Lab */
00179 /*----------------------------------------------------------------------------*/
00180 /*-General Information--------------------------------------------------------*/
00181 /*                                                                            */
00182 /*   This function determines the location of the .tcl library files needed   */
00183 /*   by tclSadie.                                                             */
00184 /*                                                                            */
00185 /*----------------------------------------------------------------------------*/
00186 /*-Interface Information------------------------------------------------------*/
00187 void
00188 setlibdir (Tcl_Interp * interp)
00189 {
00190   char testfile[250];
00191   Tcl_Obj *cmds = NULL;
00192 
00193   /*
00194    * First check if the library directory was supplied
00195    * on the command line.
00196    */
00197   if (!libhomeset)
00198     {
00199       /* Check if the environment variable TCLSADIE_HOME exists */
00200       if (!setlibdir_env (interp))
00201         {
00202           /* Finally, as a last resort, try a default location */
00203           setlibdir_default (interp);
00204         }
00205     }
00206 
00207   /* Verify that tclSadie_main.tcl can be found in the library directory */
00208   sprintf (testfile, "%s/tclSadie_main.tcl", libhome);
00209   if (verifyfile (interp, testfile))
00210     {
00211       Tcl_SetVar2 (interp, "SadieVar", "libdir", libhome, TCL_GLOBAL_ONLY);
00212       cmds =
00213         Tcl_NewStringObj
00214         ("global SadieVar auto_path; lappend auto_path $SadieVar(libdir)",
00215          -1);
00216       Tcl_EvalObj (interp, cmds);
00217     }
00218   else
00219     {
00220       fprintf (stderr,
00221                "\nERROR: tclSadie library directory: %s is not valid!\n",
00222                libhome);
00223       fprintf (stderr,
00224                "       \tYou can indicate the location of the library by using the\n");
00225       fprintf (stderr,
00226                "       \tenvironment variable TCLSADIE_HOME or the command line option -d.\n");
00227       usage ();
00228       exit (0);
00229     }
00230 }
00231 
00232 /*-Copyright Information------------------------------------------------------*/
00233 /* Copyright (c) 1999 by the University of Arizona Digital Image Analysis Lab */
00234 /*----------------------------------------------------------------------------*/
00235 /*-General Information--------------------------------------------------------*/
00236 /*                                                                            */
00237 /*   This function initializes the Tcl/Tk command interpreter used by         */
00238 /*   tclSadie.                                                                */
00239 /*                                                                            */
00240 /*----------------------------------------------------------------------------*/
00241 /*-Interface Information------------------------------------------------------*/
00242 int
00243 Tcl_AppInit (Tcl_Interp * interp        /*  I   Pointer to the command interpreter.                */
00244   )
00245 {
00246   int status;
00247   Tcl_Obj *cmds = NULL;
00248 
00249   msg_ptr = (char *) Tcl_Alloc (1024);
00250 
00251   MainTclInterp = interp;
00252 
00253   status = Tcl_Init (interp);
00254 
00255   if (status != TCL_OK)
00256     {
00257       return TCL_ERROR;
00258     }
00259 
00260   /* Initialize Tk values. */
00261   status = Tk_Init (interp);
00262 
00263   if (status != TCL_OK)
00264     {
00265       return TCL_ERROR;
00266     }
00267 
00268   /*
00269    * Call the init procedures for included packages.  Each call should
00270    * look like this:
00271    *
00272    * if (Mod_Init(interp) == TCL_ERROR) {
00273    *     return TCL_ERROR;
00274    * }
00275    *
00276    * where "Mod" is the name of the module.
00277    */
00278 
00279   if (Sadie_FileIO_Init (interp) == TCL_ERROR)
00280     {
00281       return TCL_ERROR;
00282     }
00283   if (Sadie_General_Init (interp) == TCL_ERROR)
00284     {
00285       return TCL_ERROR;
00286     }
00287   if (Sadie_Plot_Init (interp) == TCL_ERROR)
00288     {
00289       return TCL_ERROR;
00290     }
00291   if (Sadie_NewFunctions_Init (interp) == TCL_ERROR)
00292     {
00293       return TCL_ERROR;
00294     }
00295   if (Sadie_Contrast_Init (interp) == TCL_ERROR)
00296     {
00297       return TCL_ERROR;
00298     }
00299   if (Sadie_Filter_Init (interp) == TCL_ERROR)
00300     {
00301       return TCL_ERROR;
00302     }
00303   if (Sadie_Geometry_Init (interp) == TCL_ERROR)
00304     {
00305       return TCL_ERROR;
00306     }
00307   if (Sadie_Multi_Init (interp) == TCL_ERROR)
00308     {
00309       return TCL_ERROR;
00310     }
00311   if (Sadie_Classify_Init (interp) == TCL_ERROR)
00312     {
00313       return TCL_ERROR;
00314     }
00315   if (Sadie_Tools_Init (interp) == TCL_ERROR)
00316     {
00317       return TCL_ERROR;
00318     }
00319   if (Sadie_Image_Init (interp) == TCL_ERROR)
00320     {
00321       return TCL_ERROR;
00322     }
00323 
00324   /* Find the location of the library files */
00325   setlibdir (interp);
00326   printf ("Initializing tclSadie with TCLSADIE_HOME=%s\n", libhome);
00327 
00328   /* Initialize the sadie program */
00329   if (Tcl_Eval (interp, "tclSadie_main_init") == TCL_ERROR)
00330     {
00331       return TCL_ERROR;
00332     }
00333   Tk_MainLoop ();
00334 
00335   return TCL_OK;
00336 }
00337 
00338 /*-Copyright Information------------------------------------------------------*/
00339 /* Copyright (c) 1999 by the University of Arizona Digital Image Analysis Lab */
00340 /*----------------------------------------------------------------------------*/
00341 /*-General Information--------------------------------------------------------*/
00342 /*                                                                            */
00343 /*   This function parses command line arguements and spawns the Tcl/Tk       */
00344 /*   command interpreter used by tclSadie.                                    */
00345 /*                                                                            */
00346 /*----------------------------------------------------------------------------*/
00347 /*-Interface Information------------------------------------------------------*/
00348 int
00349 main (int argc, char **argv)
00350 {
00351   int i;
00352   int state = FLAG;
00353 
00354   /* Parse Command Line */
00355 
00356   ProgName = argv[0];
00357 
00358   for (i = 1; i < argc; i++)
00359     {
00360       char *arg = argv[i];
00361 
00362       if (state == FLAG && *arg == '-')
00363         {
00364           switch (arg[1])
00365             {
00366             case 'n':
00367               /* Do not use a gui interface */
00368               nogui = 1;
00369               break;
00370             case 'd':
00371               /* The next arg should be a directory */
00372               state = DIRECTORY;
00373               break;
00374             case 'f':
00375               /* The next arg should be a file */
00376               state = CMDFILE;
00377               break;
00378             default:
00379               usage ();
00380               exit (0);
00381               break;
00382             }
00383         }
00384       else if (state == DIRECTORY && *arg != '-')
00385         {
00386           sprintf (libhome, "%s", arg);
00387           libhomeset = 1;
00388           state = FLAG;
00389         }
00390       else if (state == CMDFILE && *arg != '-')
00391         {
00392           sprintf (cmdfile, "%s", arg);
00393           cmdfileset = 1;
00394           state = FLAG;
00395         }
00396       else
00397         {
00398           usage ();
00399           exit (0);
00400         }
00401     }
00402 
00403   if (nogui && !cmdfileset)
00404     {
00405       fprintf (stderr,
00406                "\nERROR: Attempting to run tclSadie with no gui without a command file!\n");
00407       usage ();
00408       exit (0);
00409     }
00410   else if (state != FLAG)
00411     {
00412       fprintf (stderr, "\nERROR: Expecting parameter after: %s\n",
00413                argv[i - 1]);
00414       usage ();
00415       exit (0);
00416     }
00417 
00418   /* Start TK interpreter without passing in command line options */
00419   Tk_Main (1, argv, Tcl_AppInit);
00420 
00421   return 0;
00422 }

Generated on Sun May 18 15:36:19 2003 for tclSadie by doxygen1.3