00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 #include "afni.h"
00025 
00026 #ifndef ALLOW_PLUGINS
00027 #  error "Plugins not properly set up -- see machdep.h"
00028 #endif
00029 
00030 #define MEGA  1048576  
00031 
00032 char * EDIT_main( PLUGIN_interface * ) ;
00033 
00034 
00035 static char helpstring[] = 
00036   "Purpose: AFNI plugin to edit data and return new dataset.\n"
00037   "Inputs: \n"
00038   " Dataset       Input and Output datasets\n"
00039   "   Input       Input dataset that must already be in memory \n"
00040   "   Prefix        Prefix for output file name \n"
00041   "   Session       Write output into specified directory (default=./) \n"
00042   " Clip          Clip intensities in range (lower,upper) to zero \n"
00043   "   Unscaled      Do not apply any automatic scaling factor \n"
00044   " Threshold     Use threshold sub-brick to censor the intensities \n"
00045   " Blur          Gaussian blur using specified function width \n"
00046   " Zero Vol UL   Zero out entries inside the 3D volume defined by: \n"
00047   " Zero Vol LL     xLL <= x <=xUL,  yLL <= y <=yUL,  zLL <= z <= zUL \n"
00048   " Cluster       Form clusters and clip off data not in clusters \n"
00049   "   Type          Options for setting voxel intensities within a cluster \n"
00050   "   Radius        Max. distance for 2 voxels to be connected in a cluster \n"
00051   "   MinVol        Min. volume for a cluster to survive \n"
00052   " Erode/Dilate  Sever narrow connecting paths between clusters \n"
00053   "   % Voxels      Min. % of active 'neighbors' for a voxel to survive \n"
00054   "   Dilate        Restore voxels near main body of cluster \n"
00055   " Filter        Filter voxel intensities \n"
00056   "   Type          Defines filter action \n"
00057   "   Radius        Voxel intensity is effected by voxels within this radius\n"
00058   " Multiply      Multiply intensities by the given factor\n"
00059   " Datum         Coerce output data to be stored as the given type \n"
00060   " Keep Thr      Copy the threshold sub-brick into the output dataset \n"
00061   " Thr Blur      Apply Gaussian blur function to threshold data sub-brick \n"
00062   " Thr Filter    Apply specified filter to threshold data sub-brick \n"
00063   " Thr Datum     Coerce threshold data sub-brick to be stored as given type\n"
00064   "Author -- BD Ward"
00065 ;
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 DEFINE_PLUGIN_PROTOTYPE
00075 
00076 PLUGIN_interface * PLUGIN_init( int ncall )
00077 {
00078   
00079   char * boolean_types[2] = {"False", "True"};
00080   char * blur_types[3] = {"Sigma", "RMS", "FWHM"};
00081   char * cluster_types[7] = {"Keep", "Mean", "Max", "AMax", "SMax", "Size",
00082                              "Order"};
00083   char * filter_types[6] = {"Mean", "NZMean", "Max", "AMax", "SMax", "Aver" };
00084   char * brick_types[2] = {"Intensity", "Threshold"};
00085   char * datum_types[3] = {"Byte", "Short", "Float"};
00086 
00087   PLUGIN_interface * plint ;
00088 
00089 
00090   if( ncall > 0 ) return NULL ;  
00091   
00092   
00093   plint = PLUTO_new_interface( "3D Edit" , "Dataset Editing" , helpstring ,
00094                                PLUGIN_CALL_VIA_MENU , EDIT_main  ) ;
00095 
00096   PLUTO_add_hint( plint , "Edit Dataset Contents" ) ;
00097 
00098   PLUTO_set_sequence( plint , "A:newdset:edit" ) ;
00099 
00100   
00101   PLUTO_add_option (plint, "Dataset", "Dataset", TRUE);
00102   PLUTO_add_hint( plint , "Choose input and output" ) ;
00103 
00104   PLUTO_add_dataset (plint, "Input",
00105                      ANAT_ALL_MASK, FUNC_ALL_MASK,
00106                      DIMEN_3D_MASK | BRICK_ALLREAL_MASK);
00107   PLUTO_add_hint( plint , "Choose input dataset" ) ;
00108 
00109   PLUTO_add_string (plint, "Prefix", 0, NULL, 19);
00110   PLUTO_add_hint( plint , "Name output dataset" ) ;
00111 
00112   PLUTO_add_string (plint, "Session", 0, NULL, 19);
00113   PLUTO_add_hint( plint , "Name output directory" ) ;
00114 
00115   
00116   PLUTO_add_option (plint, "Options", "Options", FALSE);
00117   PLUTO_add_hint( plint , "Preprocessing steps" ) ;
00118 
00119   PLUTO_add_string (plint, "Thr->Int",  2, boolean_types, 0);
00120   PLUTO_add_hint( plint , "Copy threshold over intensity brick?" ) ;
00121 
00122   PLUTO_add_string (plint, "No Neg",    2, boolean_types, 0);
00123   PLUTO_add_hint( plint , "Zero out negative values?" ) ;
00124 
00125   PLUTO_add_string (plint, "Abs Value", 2, boolean_types, 0);
00126   PLUTO_add_hint( plint , "Take absolute value?" ) ;
00127   
00128   
00129   PLUTO_add_option (plint, "Clip", "Clip", FALSE);
00130   PLUTO_add_hint( plint , "Zero out values in some range" ) ;
00131 
00132   PLUTO_add_number (plint, "Lower", -99999, 99999, 0, 0, TRUE);
00133   PLUTO_add_hint( plint , "Values above this => zero" ) ;
00134 
00135   PLUTO_add_number (plint, "Upper", -99999, 99999, 0, 0, TRUE);
00136   PLUTO_add_hint( plint , "Values below this => zero" ) ;
00137 
00138   PLUTO_add_string (plint, "Unscaled?", 2, boolean_types, 0);
00139   PLUTO_add_hint( plint , "Don't apply scaling factors?" ) ;
00140 
00141   
00142   PLUTO_add_option (plint, "Threshold", "Threshold" , FALSE);
00143   PLUTO_add_hint( plint , "Zero out if threshold brick too small" ) ;
00144 
00145   PLUTO_add_number (plint, "Cutoff"   , 0, 10000, 2, 50, TRUE);
00146   PLUTO_add_hint( plint , "Threshold values < this => 0" ) ;
00147 
00148   
00149   PLUTO_add_option (plint, "Blur", "Blur", FALSE);
00150   PLUTO_add_hint( plint , "Gaussian convolution" ) ;
00151 
00152   PLUTO_add_string (plint, "Format", 3, blur_types, 0);
00153   PLUTO_add_hint( plint , "How blur width is specified" ) ;
00154 
00155   PLUTO_add_number (plint, "Width(mm)", 0, 500, 1, 20, TRUE);
00156   PLUTO_add_hint( plint , "Range of blurring function" ) ;
00157 
00158   
00159   PLUTO_add_option (plint, "Zero Vol UL", "Zero Vol UL", FALSE);
00160   PLUTO_add_number (plint, "x Upper", -999, 999, 0, 0, TRUE);
00161   PLUTO_add_number (plint, "y Upper", -999, 999, 0, 0, TRUE);
00162   PLUTO_add_number (plint, "z Upper", -999, 999, 0, 0, TRUE);   
00163 
00164   
00165   PLUTO_add_option (plint, "Zero Vol LL", "Zero Vol LL", FALSE);
00166   PLUTO_add_number (plint, "x Lower", -999, 999, 0, 0, TRUE);
00167   PLUTO_add_number (plint, "y Lower", -999, 999, 0, 0, TRUE);
00168   PLUTO_add_number (plint, "z Lower", -999, 999, 0, 0, TRUE);
00169 
00170   
00171   PLUTO_add_option (plint, "Cluster", "Cluster", FALSE);
00172   PLUTO_add_hint( plint , "Find and reject small clusters" ) ;
00173 
00174   PLUTO_add_string (plint, "Type", 7, cluster_types, 0);
00175   PLUTO_add_hint( plint , "How to process data inside clusters" ) ;
00176 
00177   PLUTO_add_number (plint, "Radius(mm)", 0, 100, 1, 20, TRUE);
00178   PLUTO_add_hint( plint , "Max distance between 'neighbors'" ) ;
00179 
00180   PLUTO_add_number (plint, "MinVol(ul)", 0, 1000, -1, 100, TRUE);
00181   PLUTO_add_hint( plint , "Min size for cluster to survive" ) ;
00182 
00183    
00184   PLUTO_add_option (plint, "Erode/Dilate", "Erode/Dilate", FALSE);
00185   PLUTO_add_hint (plint , "Sever narrow connecting paths between clusters");
00186 
00187   PLUTO_add_number (plint, "% Voxels", 0, 100, 0, 50, TRUE);
00188   PLUTO_add_hint (plint,  
00189                   "Min % of active 'neighbors' for a voxel to survive");
00190 
00191   PLUTO_add_string (plint, "Dilate?",  2, boolean_types, 0);
00192   PLUTO_add_hint (plint , "Restore voxels near main body of cluster");
00193 
00194   
00195   PLUTO_add_option (plint, "Filter", "Filter", FALSE);
00196   PLUTO_add_string (plint, "Type", 6, filter_types, 0);
00197   PLUTO_add_number (plint, "Radius(mm)", 0, 100, 1, 20, TRUE);
00198 
00199   
00200   PLUTO_add_option (plint, "Multiply", "Multiply", FALSE);
00201   PLUTO_add_number (plint, "Factor", -99999, 99999, 0, 1, TRUE);
00202 
00203   
00204   PLUTO_add_option (plint, "Datum", "Datum", FALSE);
00205   PLUTO_add_string (plint, "Type", 3, datum_types, 1);
00206 
00207   
00208   PLUTO_add_option (plint, "Keep Thr", "Keep Thr", FALSE);
00209   PLUTO_add_string (plint, "Keep?",  2, boolean_types, 0);
00210   
00211   
00212   PLUTO_add_option (plint, "Thr Blur", "Thr Blur", FALSE);
00213   PLUTO_add_string (plint, "Format", 3, blur_types, 0);
00214   PLUTO_add_number (plint, "Width(mm)", 0, 100, 1, 20, TRUE);
00215 
00216   
00217   PLUTO_add_option (plint, "Thr Filter", "Thr Filter", FALSE);
00218   PLUTO_add_string (plint, "Type", 6, filter_types, 0);
00219   PLUTO_add_number (plint, "Radius(mm)", 0, 100, 1, 20, TRUE);
00220 
00221   
00222   PLUTO_add_option (plint, "Thr Datum", "Thr Datum", FALSE);
00223   PLUTO_add_string (plint, "Type", 3, datum_types, 1);
00224 
00225   return plint ;
00226 }
00227 
00228 
00229 
00230 
00231 
00232 
00233 
00234 char * EDIT_opts
00235 ( 
00236   PLUGIN_interface * plint,       
00237   THD_3dim_dataset ** dset,       
00238   EDIT_options * edopt,           
00239   char ** new_prefix,             
00240   char ** new_session,            
00241   int * datum,                    
00242   int * keepthr,                  
00243   int * thrdatum                  
00244 )
00245 
00246 {
00247   char * tag;                     
00248   char * str;                     
00249   float rmm;                      
00250   float vmul;                     
00251   float thresh;                   
00252   MCW_idcode * idc ;              
00253   int ival;                       
00254   float bot, top;                 
00255   float blur;                     
00256   float fval;                     
00257   float dx, dy, dz, dxyz;         
00258   float x1, x2, y1, y2, z1, z2;   
00259   float pv;                         
00260   
00261 
00262   
00263   if( plint == NULL )
00264     return 
00265       "*********************\n"
00266       "EDIT_opts: NULL input\n"
00267       "*********************";
00268   
00269 
00270   
00271   tag = PLUTO_get_optiontag(plint) ;
00272   if( (tag==NULL) || (strcmp(tag,"Dataset") != 0) )
00273     return 
00274       "*********************************\n"
00275       "EDIT_opts: Bad dataset option tag\n"
00276       "*********************************";
00277   
00278   idc  = PLUTO_get_idcode(plint) ;
00279   (*dset) = PLUTO_find_dset(idc) ;
00280   if( (*dset) == NULL )
00281     return 
00282       "****************************\n"
00283       "EDIT_opts: Bad input dataset\n"
00284       "****************************";
00285   
00286   if (DSET_NUM_TIMES((*dset)) > 1)
00287     return
00288       "*************************************************\n"
00289       "EDIT_opts: Unable to edit time-dependent datasets\n"
00290       "*************************************************";
00291   
00292   
00293   dx = fabs((*dset)->daxes->xxdel);
00294   dy = fabs((*dset)->daxes->yydel);
00295   dz = fabs((*dset)->daxes->zzdel);
00296   dxyz  = dx*dy*dz ;
00297   
00298   
00299   str = PLUTO_get_string(plint);
00300   if (str != NULL) 
00301     {
00302       if( ! PLUTO_prefix_ok(str) )
00303         return 
00304           "*********************\n"
00305           "EDIT_opts: bad prefix\n"
00306           "*********************";
00307       else
00308         *new_prefix = str;
00309     } 
00310   
00311   str = PLUTO_get_string(plint);
00312   if (str != NULL) 
00313     {
00314       *new_session = str;
00315     } 
00316   
00317 
00318   
00319   do 
00320     {
00321       tag = PLUTO_get_optiontag(plint) ; 
00322       if( tag == NULL ) break ;
00323       
00324       
00325       
00326       if (strcmp (tag, "Options") == 0)
00327         {
00328           str = PLUTO_get_string(plint);
00329           if (strcmp (str, "True") == 0)
00330             {
00331               if (DSET_THRESH_INDEX(*dset) < 0)
00332                 return 
00333                   "*********************************************\n"
00334                   "EDIT_opts: Dataset has no threshold sub-brick\n"
00335                   "*********************************************";
00336               else
00337                 edopt->thtoin = 1;
00338             }
00339           else
00340             edopt->thtoin = 0;
00341           
00342           str = PLUTO_get_string(plint);
00343           if (strcmp (str, "True") == 0)
00344             edopt->noneg = 1;
00345           else
00346             edopt->noneg = 0;
00347           
00348           str = PLUTO_get_string(plint);
00349           if (strcmp (str, "True") == 0)
00350             edopt->abss = 1;
00351           else
00352             edopt->abss = 0;
00353           
00354           continue;
00355         }
00356       
00357       
00358       
00359       if (strcmp(tag,"Clip") == 0)
00360         {
00361           bot = PLUTO_get_number(plint);
00362           top = PLUTO_get_number(plint);
00363           str = PLUTO_get_string(plint);
00364           
00365           if (bot >= top)
00366             return 
00367               "**********************************************************\n"
00368               "EDIT_opts: First clip value must be less than second value\n"
00369               "**********************************************************";
00370           
00371           edopt->clip_bot = bot;
00372           edopt->clip_top = top;
00373 
00374           if (strcmp (str, "True") == 0)
00375             edopt->clip_unscaled = 1;
00376           else
00377             edopt->clip_unscaled = 0;
00378           
00379           continue;
00380         }
00381 
00382 
00383       
00384       if (strcmp(tag,"Threshold") == 0)
00385         {
00386           thresh = PLUTO_get_number(plint) ;
00387           
00388           if (thresh < 0.0)
00389             return 
00390               "******************************\n"
00391               "EDIT_opts: Bad Threshold input\n"
00392               "******************************";
00393           
00394           if( thresh > 0.0 && DSET_THRESH_INDEX(*dset) < 0 )
00395             return 
00396               "*********************************************\n"
00397               "EDIT_opts: Dataset has no threshold sub-brick\n"
00398               "*********************************************";
00399           
00400           edopt->thresh = thresh;
00401           
00402           continue;
00403         } 
00404 
00405 
00406       
00407       if (strcmp(tag,"Blur") == 0)
00408         {
00409           str = PLUTO_get_string(plint);
00410           blur = PLUTO_get_number(plint);
00411           
00412           if (blur <= 0.0 )
00413             return 
00414               "*****************************\n"
00415               "EDIT_opts: Illegal Blur input\n"
00416               "*****************************";
00417           
00418           if (strcmp(str,"Sigma") == 0)
00419             edopt->blur  = blur;
00420           else
00421             if (strcmp(str,"RMS") == 0)
00422               edopt->blur = RMS_TO_SIGMA(blur);
00423             else
00424               if (strcmp(str,"FWHM") == 0)
00425                 edopt->blur = FWHM_TO_SIGMA(blur);
00426               else
00427                 return 
00428                   "******************************\n"
00429                   "EDIT_opts: Illegal Blur option\n"
00430                   "******************************";
00431           
00432           continue;
00433         } 
00434       
00435       
00436       
00437       if (strcmp(tag, "Zero Vol UL") == 0)
00438         {
00439           x2 = PLUTO_get_number(plint);
00440           y2 = PLUTO_get_number(plint);
00441           z2 = PLUTO_get_number(plint);
00442           
00443           tag = PLUTO_get_optiontag(plint);
00444           if (strcmp(tag, "Zero Vol LL") == 0)
00445             {
00446               x1 = PLUTO_get_number(plint);
00447               y1 = PLUTO_get_number(plint);
00448               z1 = PLUTO_get_number(plint);
00449             }
00450           else
00451             return 
00452               "***************************\n"
00453               "EDIT_opts: Need Zero Vol LL\n"
00454               "***************************";
00455 
00456           edopt->zv_x1 = x1;  edopt->zv_x2 = x2;
00457           edopt->zv_y1 = y1;  edopt->zv_y2 = y2;
00458           edopt->zv_z1 = z1;  edopt->zv_z2 = z2;
00459           edopt->do_zvol = 1;
00460           continue;
00461         }
00462 
00463 
00464       if (strcmp(tag, "Zero Vol LL") == 0)
00465         {
00466           return 
00467             "***************************\n"
00468             "EDIT_opts: Need Zero Vol UL\n"
00469             "***************************";
00470         }
00471       
00472       
00473       
00474       if (strcmp(tag,"Cluster") == 0)
00475         {
00476           str = PLUTO_get_string(plint);
00477           rmm  = PLUTO_get_number(plint) ;
00478           vmul = PLUTO_get_number(plint) ;
00479           
00480           if ( (rmm < dx) && (rmm < dy) && (rmm < dz) )
00481             return 
00482               "***********************************\n"
00483               "EDIT_opts: Cluster rmm is too small\n"
00484               "***********************************";
00485 
00486           if (vmul <= dxyz)
00487             return 
00488               "************************************\n"
00489               "EDIT_opts: Cluster vmul is too small\n"
00490               "************************************";
00491         
00492           edopt->clust_rmm  = rmm;
00493           edopt->clust_vmul = vmul;
00494           
00495           if (strcmp(str,"Keep") == 0)
00496             edopt->edit_clust = ECFLAG_SAME;
00497           else
00498             if (strcmp(str,"Mean") == 0)
00499               edopt->edit_clust = ECFLAG_MEAN;
00500             else
00501               if (strcmp(str,"Max") == 0)
00502                 edopt->edit_clust = ECFLAG_MAX;
00503               else
00504                 if (strcmp(str,"AMax") == 0)
00505                   edopt->edit_clust = ECFLAG_AMAX;
00506                 else
00507                   if (strcmp(str,"SMax") == 0)
00508                     edopt->edit_clust = ECFLAG_SMAX;
00509                   else
00510                     if (strcmp(str,"Size") == 0)
00511                       edopt->edit_clust = ECFLAG_SIZE;
00512                     else
00513                       if (strcmp(str,"Order") == 0)
00514                         edopt->edit_clust = ECFLAG_ORDER;
00515                       else
00516                         return 
00517                           "*********************************\n"
00518                           "EDIT_opts: Illegal Cluster option\n"
00519                           "*********************************";
00520           
00521           continue;
00522         }
00523       
00524       
00525       
00526       if (strcmp(tag,"Erode/Dilate") == 0)
00527         {
00528           pv  = PLUTO_get_number(plint);
00529           if ((pv > 0.0) && (edopt->clust_rmm <= 0.0))
00530             return 
00531               "******************************************************\n"
00532               "EDIT_opts: Erode/Dilate requires use of Cluster option\n"
00533               "******************************************************";
00534           else
00535             edopt->erode_pv  = pv / 100.0;
00536           
00537           str = PLUTO_get_string(plint);
00538           if (strcmp (str, "True") == 0)
00539             {
00540               if (pv <= 0.0)
00541                 return 
00542                   "**********************************************\n"
00543                   "EDIT_opts: Dilate requires use of Erode option\n"
00544                   "**********************************************";
00545               else
00546                 edopt->dilate = 1;
00547             }
00548           else
00549             edopt->dilate = 0;
00550           
00551           continue;
00552         }
00553       
00554       
00555       
00556       if (strcmp(tag,"Filter") == 0)
00557         {
00558           str = PLUTO_get_string(plint);
00559           rmm  = PLUTO_get_number(plint) ;
00560           
00561           if ( (rmm < dx) && (rmm < dy) && (rmm < dz) )
00562             return 
00563               "**********************************\n"
00564               "EDIT_opts: Filter rmm is too small\n"
00565               "**********************************";
00566 
00567           edopt->filter_rmm  = rmm;
00568           
00569           if (strcmp(str,"Mean") == 0)
00570             edopt->filter_opt = FCFLAG_MEAN;
00571           else
00572             if (strcmp(str,"NZMean") == 0)
00573               edopt->filter_opt = FCFLAG_NZMEAN;
00574             else
00575               if (strcmp(str,"Max") == 0)
00576                 edopt->filter_opt = FCFLAG_MAX;
00577               else
00578                 if (strcmp(str,"AMax") == 0)
00579                   edopt->filter_opt = FCFLAG_AMAX;
00580                 else
00581                   if (strcmp(str,"SMax") == 0)
00582                     edopt->filter_opt = FCFLAG_SMAX;
00583                   else
00584                     if (strcmp(str,"Aver") == 0)       
00585                       edopt->filter_opt = FCFLAG_AVER;
00586                     else
00587                       return 
00588                         "********************************\n"
00589                         "EDIT_opts: Illegal Filter option\n"
00590                         "********************************";
00591           
00592           continue;
00593         }
00594       
00595 
00596       
00597       if (strcmp(tag,"Multiply") == 0)
00598         {
00599           fval  = PLUTO_get_number(plint);
00600           
00601           if (fval == 0.0)
00602             return 
00603               "*****************************\n"
00604               "EDIT_opts: Bad Multiply input\n"
00605               "*****************************";
00606           
00607           edopt->mult = fval;
00608           
00609           continue;
00610         }
00611       
00612       
00613       
00614       if (strcmp(tag, "Datum") == 0)
00615         {
00616           str = PLUTO_get_string(plint);
00617           if (strcmp(str,"Byte") == 0)
00618             *datum = MRI_byte;
00619           else
00620             if (strcmp(str,"Short") == 0)
00621               *datum = MRI_short;
00622             else
00623               if (strcmp(str,"Float") == 0)
00624                 *datum = MRI_float;
00625               else 
00626                 {
00627                   return 
00628                     "*****************************\n"
00629                     "EDIT_opts: Illegal Datum type\n"
00630                     "*****************************";
00631                 }
00632           
00633           continue;
00634         }
00635       
00636       
00637       
00638       if (strcmp(tag,"Keep Thr") == 0)
00639         {
00640           str = PLUTO_get_string(plint);
00641           if (strcmp (str, "True") == 0)
00642             {
00643               if (DSET_THRESH_INDEX(*dset) < 0)
00644                 return 
00645                   "*********************************************\n"
00646                   "EDIT_opts: Dataset has no threshold sub-brick\n"
00647                   "*********************************************";
00648               else
00649                 *keepthr = 1;
00650             }
00651           else
00652             *keepthr = 0;
00653           
00654           continue;
00655         }
00656       
00657       
00658       
00659       if (strcmp(tag,"Thr Blur") == 0)
00660         {
00661           if (DSET_THRESH_INDEX(*dset) < 0)
00662             return 
00663               "*********************************************\n"
00664               "EDIT_opts: Dataset has no threshold sub-brick\n"
00665               "*********************************************";
00666 
00667           str = PLUTO_get_string(plint);
00668           blur = PLUTO_get_number(plint) ;
00669           
00670           if (blur <= 0.0 )
00671             return 
00672               "***************************************\n"
00673               "EDIT_opts: Illegal Threshold Blur input\n"
00674               "***************************************";
00675           
00676           if (strcmp(str,"Sigma") == 0)
00677             edopt->thrblur  = blur;
00678           else
00679             if (strcmp(str,"RMS") == 0)
00680               edopt->thrblur = RMS_TO_SIGMA(blur);
00681             else
00682               if (strcmp(str,"FWHM") == 0)
00683                 edopt->thrblur = FWHM_TO_SIGMA(blur);
00684               else
00685                 return 
00686                   "******************************\n"
00687                   "EDIT_opts: Illegal Blur option\n"
00688                   "******************************";
00689           
00690           *keepthr = 1;
00691           
00692           continue;
00693         } 
00694       
00695       
00696       
00697       if (strcmp(tag,"Thr Filter") == 0)
00698         {
00699           if (DSET_THRESH_INDEX(*dset) < 0)
00700             return 
00701               "*********************************************\n"
00702               "EDIT_opts: Dataset has no threshold sub-brick\n"
00703               "*********************************************";
00704 
00705           str = PLUTO_get_string(plint);
00706           rmm  = PLUTO_get_number(plint) ;
00707           
00708           if ( (rmm < dx) && (rmm < dy) && (rmm < dz) )
00709             return 
00710               "**************************************\n"
00711               "EDIT_opts: Thr Filter rmm is too small\n"
00712               "**************************************";
00713 
00714           edopt->thrfilter_rmm  = rmm;
00715           
00716           if (strcmp(str,"Mean") == 0)
00717             edopt->thrfilter_opt = FCFLAG_MEAN;
00718           else
00719             if (strcmp(str,"NZMean") == 0)
00720               edopt->thrfilter_opt = FCFLAG_NZMEAN;
00721             else
00722               if (strcmp(str,"Max") == 0)
00723                 edopt->thrfilter_opt = FCFLAG_MAX;
00724               else
00725                 if (strcmp(str,"AMax") == 0)
00726                   edopt->thrfilter_opt = FCFLAG_AMAX;
00727                 else
00728                   if (strcmp(str,"SMax") == 0)
00729                     edopt->thrfilter_opt = FCFLAG_SMAX;
00730                   else
00731                     if (strcmp(str,"Aver") == 0)       
00732                       edopt->thrfilter_opt = FCFLAG_AVER;
00733                     else
00734                       return 
00735                         "************************************\n"
00736                         "EDIT_opts: Illegal Thr Filter option\n"
00737                         "************************************";
00738 
00739           *keepthr = 1;
00740           
00741           continue;
00742         }
00743       
00744 
00745       
00746       if (strcmp(tag, "Thr Datum") == 0)
00747         {
00748           if (DSET_THRESH_INDEX(*dset) < 0)
00749             return 
00750               "*********************************************\n"
00751               "EDIT_opts: Dataset has no threshold sub-brick\n"
00752               "*********************************************";
00753 
00754           str = PLUTO_get_string(plint);
00755           if (strcmp(str,"Byte") == 0)
00756             *thrdatum = MRI_byte;
00757           else
00758             if (strcmp(str,"Short") == 0)
00759               *thrdatum = MRI_short;
00760             else
00761               if (strcmp(str,"Float") == 0)
00762                 *thrdatum = MRI_float;
00763               else 
00764                 {
00765                   return 
00766                     "***************************************\n"
00767                     "EDIT_opts: Illegal Threshold Datum type\n"
00768                     "***************************************";
00769                 }
00770           
00771           *keepthr = 1;
00772           
00773           continue;
00774         }
00775       
00776       
00777       
00778       else 
00779         {
00780           return 
00781             "***********************************\n"
00782             "EDIT_opts: Illegal optiontag found!\n"
00783             "***********************************";
00784         }
00785       
00786     } while(1) ;
00787   
00788   
00789   
00790   
00791   return NULL;
00792 }
00793 
00794 
00795 
00796 
00797 
00798 
00799 
00800 char * EDIT_main( PLUGIN_interface * plint )
00801 {
00802   EDIT_options PE_edopt;
00803   int PE_keepthr = 0;
00804   int PE_datum = ILLEGAL_TYPE;
00805   int PE_thdatum = ILLEGAL_TYPE;
00806   int PE_be_quiet = 0;
00807   char * PE_output_session = NULL;
00808   char * PE_output_prefix = NULL;
00809   
00810 
00811   int nx,ny,nz , nxyz , ii ,  ival;
00812   THD_3dim_dataset * old_dset = NULL, * dset=NULL , * new_dset=NULL ;
00813   int     datum ;
00814   float fimfac , fimfacinv , first_fimfac , thrfac ;
00815   int   output_datum , output_thdatum ;
00816   int   input_datum  , input_thdatum , first_datum ;
00817   
00818   float thr_stataux[MAX_STAT_AUX] ;
00819   char * str;
00820 
00821   
00822   
00823   
00824   INIT_EDOPT( &PE_edopt ) ;
00825 
00826 
00827   
00828   str = EDIT_opts (plint, &old_dset, &PE_edopt,  
00829                    &PE_output_prefix, &PE_output_session, &PE_datum,
00830                    &PE_keepthr, &PE_thdatum);
00831   if (str != NULL)  return (str);
00832 
00833   
00834   dset = PLUTO_copy_dset (old_dset, NULL);
00835   DSET_unload (old_dset);
00836 
00837   
00838   nx = dset->daxes->nxx ;
00839   ny = dset->daxes->nyy ;
00840   nz = dset->daxes->nzz ; nxyz = nx*ny*nz ;
00841 
00842 
00843   ival        = DSET_PRINCIPAL_VALUE(dset) ;
00844   input_datum = DSET_BRICK_TYPE(dset,ival) ;
00845   if (PE_datum >= 0) output_datum = PE_datum ;
00846   else               output_datum = input_datum ;
00847   
00848   new_dset = EDIT_empty_copy( dset ) ;
00849   
00850   EDIT_dset_items( new_dset ,
00851                    ADN_prefix , PE_output_prefix ,
00852                    ADN_label1 , PE_output_prefix ,
00853                    ADN_directory_name , PE_output_session ,
00854                    ADN_none ) ;
00855   strcat( new_dset->self_name , "(PE)" ) ;
00856 
00857   { char * his = PLUTO_commandstring(plint) ;
00858     tross_Copy_History( dset , new_dset ) ;
00859     tross_Append_History( new_dset, his ) ; free(his) ;
00860   }
00861   
00862   if( ! PE_keepthr && new_dset->dblk->nvals > 1 )
00863     EDIT_dset_items( new_dset ,
00864                      ADN_nvals , 1 ,
00865                      ADN_func_type , FUNC_FIM_TYPE ,
00866                      ADN_none ) ;
00867   
00868   if ( PE_keepthr && ISFUNC(new_dset) && FUNC_HAVE_THR(new_dset->func_type) )
00869     {
00870       ii            = FUNC_ival_thr[dset->func_type] ;
00871       input_thdatum = DSET_BRICK_TYPE(dset,ii) ;
00872       if (PE_thdatum >= 0) output_thdatum = PE_thdatum ;
00873       else                 output_thdatum = input_thdatum ;
00874     } 
00875   else 
00876     {
00877       output_thdatum = input_thdatum = ILLEGAL_TYPE ;
00878     }
00879   
00880   if ( THD_is_file(new_dset->dblk->diskptr->header_name) )
00881     {
00882       fprintf(stderr,
00883               "*** Output file %s already exists -- cannot continue!\n",
00884               new_dset->dblk->diskptr->header_name ) ;
00885       return("*** Output file already exists -- cannot continue!");
00886       
00887     }
00888   
00889   if (! PE_be_quiet)
00890     {
00891       printf("-- editing input dataset in memory (%.1f MB)",
00892                   ((double)dset->dblk->total_bytes) / MEGA ) ;
00893       fflush(stdout) ;
00894     } 
00895 
00896 
00897   EDIT_one_dataset( dset , &PE_edopt ) ;  
00898 
00899   
00900   if (! PE_be_quiet)  { printf(".\n") ; fflush(stdout) ; }
00901   
00902 
00903 
00904   ival = DSET_PRINCIPAL_VALUE(dset) ;
00905   ii   = DSET_PRINCIPAL_VALUE(new_dset) ;
00906   
00907   if( input_datum == output_datum )
00908     {
00909       
00910 
00911 
00912  
00913       if (! PE_be_quiet) 
00914         printf ("connecting edited input to be output \n");
00915       mri_fix_data_pointer (DSET_ARRAY(dset,ival), DSET_BRICK(new_dset,ii));
00916       DSET_BRICK_FACTOR(new_dset,ii) = DSET_BRICK_FACTOR(dset,ival);
00917     }
00918   else 
00919     {
00920  
00921       void * dfim , * efim ;
00922       float etop ;
00923       
00924       if(! PE_be_quiet)
00925         {
00926           printf("-- coercing output datum to be %s\n",
00927                  MRI_TYPE_name[output_datum]);
00928         } 
00929       
00930       efim = DSET_ARRAY(dset,ival) ;
00931       dfim = (void *) XtMalloc( mri_datum_size(output_datum) * nxyz ) ;
00932       
00933       fimfac = EDIT_coerce_autoscale( nxyz , input_datum  , efim ,
00934                                       output_datum , dfim  ) ;
00935       
00936       DSET_BRICK_FACTOR(new_dset,ii) = (fimfac != 0.0 && fimfac != 1.0)
00937         ? 1.0/fimfac : 0.0 ;
00938 
00939       EDIT_substitute_brick( new_dset , ii , output_datum , dfim ) ;
00940       mri_free( DSET_BRICK(dset,ival) ) ;
00941     }
00942 
00943 
00944   
00945   if( output_thdatum >= 0 )
00946     {      
00947       ival = FUNC_ival_thr[    dset->func_type] ;
00948       ii   = FUNC_ival_thr[new_dset->func_type] ;
00949       
00950       if( input_thdatum == output_thdatum )
00951         {
00952           if (! PE_be_quiet)
00953             printf ("connecting input and output thresholds \n") ;
00954           mri_fix_data_pointer (DSET_ARRAY(dset,ival),DSET_BRICK(new_dset,ii));
00955           DSET_BRICK_FACTOR(new_dset,ii) = DSET_BRICK_FACTOR(dset,ival) ; 
00956         } 
00957       else 
00958         {
00959           void * dfim , * efim ;
00960           
00961           if( ! PE_be_quiet )
00962             {
00963               printf("-- coercing threshold datum to be %s\n",
00964                      MRI_TYPE_name[output_thdatum]);
00965             } 
00966           
00967           efim = DSET_ARRAY(dset,ival) ;
00968           dfim = (void *) XtMalloc( mri_datum_size(output_thdatum) * nxyz ) ;
00969           
00970           switch( output_thdatum )
00971             {
00972             default: fprintf(stderr,"** illegal output_thdatum = %d\n",
00973                              output_thdatum);
00974               return("** illegal output_thdatum");
00975               
00976             
00977             case MRI_float:
00978               fimfacinv = 0.0 ;
00979               fimfac    = DSET_BRICK_FACTOR(dset,ival) ;
00980               if( fimfac == 0.0 )
00981                 {
00982                   fimfac = (input_thdatum == MRI_short)
00983                     ? 1.0/FUNC_scale_short[dset->func_type]
00984                     : (input_thdatum == MRI_byte)
00985                     ? 1.0/FUNC_scale_byte[dset->func_type] : 0.0 ;
00986                 }
00987               break ;
00988 
00989             case MRI_short:
00990               if( input_datum == MRI_float )
00991                 {
00992                   fimfac    = FUNC_scale_short[new_dset->func_type] ;
00993                   fimfacinv = 1.0 / fimfac ;
00994                 } 
00995               else 
00996                 if( input_datum == MRI_byte )
00997                   {
00998                     fimfac    = ((float)FUNC_scale_short[new_dset->func_type])
00999                       / FUNC_scale_byte[new_dset->func_type] ;
01000                     fimfacinv = 1.0 / FUNC_scale_short[new_dset->func_type] ;
01001                   } 
01002                 else 
01003                   {
01004                     fprintf(stderr,
01005                             "** illegal input_thdatum = %d\n",input_thdatum);
01006                     return("** illegal input_thdatum");
01007                     
01008                   }
01009               break ;
01010               
01011             case MRI_byte:
01012               if( input_datum == MRI_float )
01013                 {
01014                   fimfac    = FUNC_scale_byte[new_dset->func_type] ;
01015                   fimfacinv = 1.0 / fimfac ;
01016                 }
01017               else 
01018                 if( input_datum == MRI_short )
01019                   {
01020                     fimfac    = ((float)FUNC_scale_byte[new_dset->func_type])
01021                       / FUNC_scale_short[new_dset->func_type] ;
01022                     fimfacinv = 1.0 / FUNC_scale_byte[new_dset->func_type] ;
01023                   } 
01024                 else 
01025                   {
01026                     fprintf(stderr,"** illegal input_thdatum = %d\n",
01027                             input_thdatum);
01028                     return("** illegal input_thdatum");
01029               
01030                   }
01031               break;
01032             }
01033           
01034           EDIT_coerce_scale_type( nxyz , fimfac ,
01035                                   DSET_BRICK_TYPE(dset,ival),efim ,
01036                                   output_thdatum,dfim );
01037           
01038           DSET_BRICK_FACTOR(new_dset,ii) = fimfacinv;
01039           EDIT_substitute_brick( new_dset , ii , output_thdatum , dfim );
01040           mri_free( DSET_BRICK(dset,ival) );
01041         }
01042     }
01043   
01044   if (! PE_be_quiet)
01045     printf("-- Writing edited dataset:%s\n" , DSET_BRIKNAME(new_dset) ) ;
01046   
01047   ival = PLUTO_add_dset( plint , new_dset , DSET_ACTION_MAKE_CURRENT ) ;
01048 
01049   if (ival)
01050     {
01051       THD_delete_3dim_dataset( new_dset , False ) ;
01052       return 
01053         "*********************************************\n"
01054         "EDIT_main: failure to add new dataset to AFNI\n"
01055         "*********************************************" ;
01056     }
01057   else
01058     return (NULL) ;
01059   
01060 }
01061 
01062 
01063 
01064 
01065