Main Page   Data Structures   File List   Data Fields   Globals  

Sadie_Plot.c

Go to the documentation of this file.
00001 /*
00002 ##########################################
00003 # Sadie_Plot.c -
00004 #   Set of routines for creating plots of SADIE data in the
00005 #   session log through tcl/tk.
00006 #
00007 # RCS: $Id: Sadie_Plot.c,v 2.2 1999/03/20 00:34:26 conner Exp $ 
00008 # 
00009 # Digital Image Analysis Lab
00010 # Dept. of Electrical and Computer Engineering
00011 # University of Arizona
00012 ##########################################
00013 */
00014 
00015 #include <tcl.h>
00016 #include <tk.h>
00017 #include <stdio.h>
00018 #include <string.h>
00019 #include "sadie.h"
00020 
00021 /* RCS Indentification information */
00022 static char rcsid[] =
00023   "$Id: Sadie_Plot.c,v 2.2 1999/03/20 00:34:26 conner Exp $";
00024 
00025 #define FALSE 0
00026 #define TRUE  1
00027 
00028 extern Tcl_Interp *MainTclInterp;
00029 
00030 int **x_array_buffer;           /* The data where info is stored      */
00031 int point;                      /* Counter for points used            */
00032 int count_point;                /* Flag decide to count or fill array */
00033 int plotcount = 0;
00034 
00035 int
00036 Sadie_Plot_ProfileCmd (ClientData client_data, Tcl_Interp * interp, int argc,
00037                        char *argv[])
00038 {
00039 
00040   int i, j;
00041   int startx, endx;
00042   int starty, endy;
00043   int option;
00044   int inimgaddr;
00045   IMAGE *inimg;
00046   char msg[SLEN];
00047   PIXEL **arrays = NULL;
00048   char minlbl[20], maxlbl[20];
00049 
00050   if (argc <= 6)
00051     {
00052       return TCL_ERROR;
00053     }
00054 
00055   sscanf (argv[1], "%x", &inimgaddr);
00056   inimg = (IMAGE *) inimgaddr;
00057 
00058   if (Tcl_GetInt (interp, argv[2], &startx) != TCL_OK)
00059     {
00060       return TCL_ERROR;
00061     }
00062 
00063   if (Tcl_GetInt (interp, argv[3], &starty) != TCL_OK)
00064     {
00065       return TCL_ERROR;
00066     }
00067 
00068   if (Tcl_GetInt (interp, argv[4], &endx) != TCL_OK)
00069     {
00070       return TCL_ERROR;
00071     }
00072 
00073   if (Tcl_GetInt (interp, argv[5], &endy) != TCL_OK)
00074     {
00075       return TCL_ERROR;
00076     }
00077 
00078   if (Tcl_GetInt (interp, argv[6], &option) != TCL_OK)
00079     {
00080       return TCL_ERROR;
00081     }
00082 
00083   /* check input */
00084   if (option != UNIT && option != NORM && option != LOG)
00085     {
00086       MESSAGE ('E', " Option must be one of UNIT, NORM, or LOG.");
00087       return TCL_ERROR;
00088     }
00089 
00090   /* Count points */
00091   count_point = TRUE;
00092   point = 0;
00093   brshnm (startx, starty, endx, endy);
00094 
00095   /* Allocate memory */
00096   x_array_buffer = (int **) malloc (point * sizeof (int *));
00097   if (x_array_buffer == (int **) NULL)
00098     {
00099       sprintf (msg, "No memory for x_array_buffer");
00100       MESSAGE ('E', msg);
00101       goto the_end;
00102     }
00103 
00104   for (i = 0; i < point; i++)
00105     {
00106       x_array_buffer[i] = (int *) calloc (2, sizeof (int));
00107       if (x_array_buffer[i] == (int) NULL)
00108         {
00109           sprintf (msg, "No memory for element %d", x_array_buffer[i]);
00110           MESSAGE ('E', msg);
00111           goto the_end;
00112         }
00113     }
00114 
00115   /* Fill array */
00116   count_point = FALSE;
00117   point = 0;
00118   brshnm (startx, starty, endx, endy);
00119 
00120   /* Allocate memory for plotting */
00121   arrays = (PIXEL **) malloc (inimg->nbnd * sizeof (PIXEL *));
00122   if (!arrays)
00123     {
00124       MESSAGE ('E', " Error allocating memory for plotting!");
00125       goto the_end;
00126     }
00127   for (i = 0; i < inimg->nbnd; i++)
00128     {
00129       arrays[i] = NULL;
00130       arrays[i] = (PIXEL *) malloc (point * sizeof (PIXEL));
00131       if (!arrays[i])
00132         {
00133           MESSAGE ('E', " Error allocating memory for plotting!");
00134           goto the_end;
00135         }
00136     }
00137 
00138   /* Fill the plot array */
00139   for (i = 0; i < inimg->nbnd; i++)
00140     {
00141       for (j = 0; j < point; j++)
00142         {
00143           arrays[i][j] =
00144             inimg->data[i][x_array_buffer[j][1]][x_array_buffer[j][0]];
00145         }
00146     }
00147 
00148   sprintf (minlbl, "(%d,%d)", startx, starty);
00149   sprintf (maxlbl, "(%d,%d)", endx, endy);
00150   PLOT (arrays, inimg->nbnd, point, minlbl, maxlbl, option);
00151 
00152   if (NAMES)
00153     {
00154       MESSAGE ('I', " ");
00155       MESSAGE ('I', " ...............");
00156     }
00157 
00158 the_end:
00159 
00160   /* Free Array */
00161   for (i = 0; i < point; i++)
00162     {
00163       free (x_array_buffer[i]);
00164       x_array_buffer[i] = NULL;
00165     }
00166 
00167   free (x_array_buffer);
00168   x_array_buffer = NULL;
00169 
00170   if (arrays)
00171     {
00172       for (i = 0; i < inimg->nbnd; i++)
00173         {
00174           if (arrays[i])
00175             free (arrays[i]);
00176           arrays[i] = NULL;
00177         }
00178       free (arrays);
00179       arrays = NULL;
00180     }
00181 
00182   return TCL_OK;
00183 
00184 }
00185 
00186 int
00187 Sadie_Plot_StatisticsCmd (ClientData client_data, Tcl_Interp * interp,
00188                           int argc, char *argv[])
00189 {
00190   int imgaddr;
00191   IMAGE *in;
00192   int incr, nbins, matrix;
00193   double *mean = 0, *dev = 0, *cov = 0, *cor = 0;
00194   PIXEL *gmin = 0, *gmax = 0, *grng = 0;
00195   long *zero = 0;
00196   int rangeoption;
00197   PIXEL rangemin, rangemax;
00198   double tempval;
00199 
00200   if (argc <= 7)
00201     {
00202       return TCL_ERROR;
00203     }
00204 
00205   sscanf (argv[1], "%x", &imgaddr);
00206   in = (IMAGE *) imgaddr;
00207 
00208   if (Tcl_GetInt (interp, argv[2], &incr) != TCL_OK)
00209     {
00210       return TCL_ERROR;
00211     }
00212 
00213   if (Tcl_GetInt (interp, argv[3], &nbins) != TCL_OK)
00214     {
00215       return TCL_ERROR;
00216     }
00217 
00218   if (Tcl_GetInt (interp, argv[4], &matrix) != TCL_OK)
00219     {
00220       return TCL_ERROR;
00221     }
00222 
00223   if (Tcl_GetInt (interp, argv[5], &rangeoption) != TCL_OK)
00224     {
00225       return TCL_ERROR;
00226     }
00227 
00228   if (Tcl_GetDouble (interp, argv[6], &tempval) != TCL_OK)
00229     {
00230       return TCL_ERROR;
00231     }
00232   rangemin = (PIXEL) tempval;
00233 
00234   if (Tcl_GetDouble (interp, argv[7], &tempval) != TCL_OK)
00235     {
00236       return TCL_ERROR;
00237     }
00238   rangemax = (PIXEL) tempval;
00239 
00240   if (!CHECKIMG (in))
00241     {
00242       Tcl_AppendResult (interp,
00243                         "couldn't identify SADIE image from input string.",
00244                         NULL);
00245       return TCL_ERROR;
00246     }
00247   if (rangeoption != IGNORE && rangeoption != INCLUDE
00248       && rangeoption != EXCLUDE)
00249     {
00250       MESSAGE ('E', " Option must be one of IGNORE, INCLUDE, or EXCLUDE.");
00251       return TCL_ERROR;
00252     }
00253 
00254   RANGE (in);
00255 
00256   /* allocate space */
00257   if (!(mean = (double *) malloc (in->nbnd * sizeof (double))))
00258     {
00259       MESSAGE ('E', " Memory request failed.");
00260       goto the_end;
00261     }
00262   if (!(dev = (double *) malloc (in->nbnd * sizeof (double))))
00263     {
00264       MESSAGE ('E', " Memory request failed.");
00265       goto the_end;
00266     }
00267   if (!(gmin = (PIXEL *) malloc (in->nbnd * sizeof (PIXEL))))
00268     {
00269       MESSAGE ('E', " Memory request failed.");
00270       goto the_end;
00271     }
00272   if (!(gmax = (PIXEL *) malloc (in->nbnd * sizeof (PIXEL))))
00273     {
00274       MESSAGE ('E', " Memory request failed.");
00275       goto the_end;
00276     }
00277   if (!(grng = (PIXEL *) malloc (in->nbnd * sizeof (PIXEL))))
00278     {
00279       MESSAGE ('E', " Memory request failed.");
00280       goto the_end;
00281     }
00282   if (!(zero = (long *) malloc (in->nbnd * sizeof (long))))
00283     {
00284       MESSAGE ('E', " Memory request failed.");
00285       goto the_end;
00286     }
00287   if (!(cov = (double *) malloc (in->nbnd * in->nbnd * sizeof (double))))
00288     {
00289       MESSAGE ('E', " Memory request failed.");
00290       goto the_end;
00291     }
00292   if (!(cor = (double *) malloc (in->nbnd * in->nbnd * sizeof (double))))
00293     {
00294       MESSAGE ('E', " Memory request failed.");
00295       goto the_end;
00296     }
00297 
00298   STATS (in, incr, nbins, rangeoption, rangemin, rangemax, mean, dev, gmin,
00299          gmax, grng, zero);
00300 
00301   if (matrix)
00302     COVAR (in, incr, cov, cor);
00303 
00304 the_end:
00305   if (cov)
00306     free (cov);
00307   if (cor)
00308     free (cor);
00309   if (mean)
00310     free (mean);
00311   if (dev)
00312     free (dev);
00313   if (gmin)
00314     free (gmin);
00315   if (gmax)
00316     free (gmax);
00317   if (grng)
00318     free (grng);
00319   if (zero)
00320     free (zero);
00321 
00322   return TCL_OK;
00323 }
00324 
00325 int
00326 Sadie_Plot_CreateROICmd (ClientData client_data, Tcl_Interp * interp,
00327                          int argc, char *argv[])
00328 {
00329   int imgaddr;
00330   IMAGE *in;
00331   char *name;
00332   int incr, nlev, matrix;
00333   int point1x, point2x, point1y, point2y;
00334   int xbgn, ybgn, xend, yend;
00335 
00336   if (argc <= 9)
00337     {
00338       return TCL_ERROR;
00339     }
00340 
00341   sscanf (argv[1], "%x", &imgaddr);
00342   in = (IMAGE *) imgaddr;
00343 
00344   if (Tcl_GetInt (interp, argv[2], &incr) != TCL_OK)
00345     {
00346       return TCL_ERROR;
00347     }
00348 
00349   if (Tcl_GetInt (interp, argv[3], &nlev) != TCL_OK)
00350     {
00351       return TCL_ERROR;
00352     }
00353 
00354   if (Tcl_GetInt (interp, argv[4], &matrix) != TCL_OK)
00355     {
00356       return TCL_ERROR;
00357     }
00358 
00359   if (Tcl_GetInt (interp, argv[5], &point1x) != TCL_OK)
00360     {
00361       return TCL_ERROR;
00362     }
00363   if (Tcl_GetInt (interp, argv[6], &point1y) != TCL_OK)
00364     {
00365       return TCL_ERROR;
00366     }
00367   if (Tcl_GetInt (interp, argv[7], &point2x) != TCL_OK)
00368     {
00369       return TCL_ERROR;
00370     }
00371   if (Tcl_GetInt (interp, argv[8], &point2y) != TCL_OK)
00372     {
00373       return TCL_ERROR;
00374     }
00375 
00376   name = argv[9];
00377 
00378   if (!CHECKIMG (in))
00379     {
00380       Tcl_AppendResult (interp,
00381                         "couldn't identify SADIE image from input string.",
00382                         NULL);
00383       return TCL_ERROR;
00384     }
00385 
00386   RANGE (in);
00387 
00388   xbgn = min (point1x, point2x);
00389   xend = max (point1x, point2x);
00390   ybgn = min (point1y, point2y);
00391   yend = max (point1y, point2y);
00392 
00393   CREATE_ROI (in, name, xbgn, ybgn, xend, yend, matrix, incr, nlev);
00394 
00395 the_end:
00396 
00397   return TCL_OK;
00398 }
00399 
00400 int
00401 Sadie_Plot_PlotArrayCmd (PIXEL * array, int arraysize)
00402 {
00403   int i, j;
00404   PIXEL gmin, gmax;
00405   int minval, maxval;
00406   char minlbl[20], maxlbl[20];
00407 
00408   /* Find min and max plot values */
00409   gmax = gmin = array[0];
00410   for (i = 0; i < arraysize; i++)
00411     {
00412       gmin = min (gmin, array[i]);
00413       gmax = max (gmax, array[i]);
00414     }
00415 
00416   minval = (int) rnd (gmin);
00417   maxval = (int) rnd (gmax);
00418 
00419   sprintf (minlbl, "%d", minval);
00420   sprintf (maxlbl, "%d", maxval);
00421 
00422   PLOT (&array, 1, arraysize, (char *) minlbl, (char *) maxlbl, NORM);
00423 }
00424 
00425 /*************************************************************************
00426  *
00427  * This procedure initializes the the Sadie_Plot procedures when
00428  * this package is loaded.
00429  *
00430  *************************************************************************/
00431 int
00432 Sadie_Plot_Init (Tcl_Interp * interp)
00433 {
00434   Tcl_CreateCommand (interp, "Sadie_Plot_Profile", Sadie_Plot_ProfileCmd,
00435                      (ClientData) NULL, NULL);
00436   Tcl_CreateCommand (interp, "Sadie_Plot_Statistics",
00437                      Sadie_Plot_StatisticsCmd, (ClientData) NULL, NULL);
00438   Tcl_CreateCommand (interp, "Sadie_Plot_CreateROI", Sadie_Plot_CreateROICmd,
00439                      (ClientData) NULL, NULL);
00440 
00441   return TCL_OK;
00442 }

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