/*---------------------------------------------------------------------- ** ** Main routine for this plugin (will be called from AFNI). ** **---------------------------------------------------------------------- */ char * HEMISUB_main( PLUGIN_interface * plint ) { THD_3dim_dataset * dset, * new_dset; MCW_idcode * idc; hemi_s hs = { 0 }; char * new_prefix; char * ret_string = NULL; char * tag; if ( plint == NULL ) return "------------------------\n" "HEMISUB_main: NULL input\n" "------------------------\n"; PLUTO_next_option( plint ); idc = PLUTO_get_idcode( plint ); dset = PLUTO_find_dset( idc ); if( dset == NULL ) return "-------------------------------\n" "HEMISUB_main: bad input dataset\n" "-------------------------------"; DSET_load( dset ); PLUTO_next_option( plint ); new_prefix = PLUTO_get_string( plint ); if ( ! PLUTO_prefix_ok( new_prefix ) ) return "------------------------\n" "HEMISUB_main: bad prefix\n" "------------------------\n"; if ( ( new_dset = PLUTO_copy_dset( dset, new_prefix ) ) == NULL ) return "------------------------------------------\n" "HEMISUB_main: failed to copy input dataset\n" "------------------------------------------\n"; tag = PLUTO_get_optiontag( plint ); if ( tag && ! strcmp( tag, "Thresh Type" ) ) { tag = PLUTO_get_string( plint ); if ( tag != NULL ) hs.thresh_type = PLUTO_string_index( tag, NUM_T_OPTS, thresh_opts ); } if ( ret_string = process_data( new_dset, &hs ) ) return ret_string; if ( PLUTO_add_dset( plint, new_dset, DSET_ACTION_MAKE_CURRENT ) ) { THD_delete_3dim_dataset( new_dset, False ); return "---------------------------------------\n" "HEMISUB_main: failed to add new dataset\n" "---------------------------------------\n"; } return NULL; }
char * STAVG_main( PLUGIN_interface * plint ) { MCW_idcode * idc ; /* input dataset idcode */ THD_3dim_dataset * old_dset , * new_dset ; /* input and output datasets */ char * new_prefix , * str , * str2; /* strings from user */ int meth; /* chosen computation method */ int new_datum , /* control parameters */ old_datum , ntime ; int te, ne, tinc, kim, nia; int numepochs, minlength, maxlength, lastindex, navgpts; int nvox , perc , new_units, old_units ; int ii, ibot,itop , kk, jj; int no1, user_maxlength, delta; int *pEpochLength, *pTimeIndex; int nx, ny, nz, npix; float *pNumAvg; float old_dtime; MRI_IMAGE * stimim; MRI_IMARR *avgimar; byte ** bptr = NULL ; /* one of these will be the array of */ short ** sptr = NULL ; /* pointers to input dataset sub-bricks */ float ** fptr = NULL ; /* (depending on input datum type) */ float * fxar = NULL ; /* array loaded from input dataset */ float * stimar = NULL ; float ** fout = NULL ; /* will be array of output floats */ float * tar = NULL ; /* will be array of taper coefficients */ float * nstimar; /*--------------------------------------------------------------------*/ /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/ /*--------- go to first input line ---------*/ PLUTO_next_option(plint) ; idc = PLUTO_get_idcode(plint) ; /* get dataset item */ old_dset = PLUTO_find_dset(idc) ; /* get ptr to dataset */ if( old_dset == NULL ) return "*************************\n" "Cannot find Input Dataset\n" "*************************" ; ntime = DSET_NUM_TIMES(old_dset) ; if( ntime < 2 ) return "*****************************\n" "Dataset has only 1 time point\n" "*****************************" ; ii = DSET_NVALS_PER_TIME(old_dset) ; if( ii > 1 ) return "************************************\n" "Dataset has > 1 value per time point\n" "************************************" ; old_datum = DSET_BRICK_TYPE( old_dset , 0 ) ; /* get old dataset datum type */ new_datum = old_datum; old_dtime = DSET_TIMESTEP(old_dset); old_units = DSET_TIMEUNITS(old_dset); nvox = old_dset->daxes->nxx * old_dset->daxes->nyy * old_dset->daxes->nzz; npix = old_dset->daxes->nxx * old_dset->daxes->nyy; nx = old_dset->daxes->nxx; new_prefix = PLUTO_get_string(plint) ; /* get string item (the output prefix) */ if( ! PLUTO_prefix_ok(new_prefix) ) /* check if it is OK */ return "************************\n" "Output Prefix is illegal\n" "************************" ; /*--------- go to next input line ---------*/ PLUTO_next_option(plint); stimim = PLUTO_get_timeseries(plint); if( stimim == NULL ) return "Please specify stimulus timing"; if( stimim->nx < ntime ){ return "**************************************\n" "Not enough pts in stimulus time-series\n" "**************************************"; } stimar = MRI_FLOAT_PTR(stimim); delta = PLUTO_get_number(plint); if( abs(delta) > ntime ){ return "************************\n" "Delta shift is too large\n" "************************"; } /*initialize variables if not user specified */ user_maxlength = ntime; no1 = 0; /*--------- go to next input line ---------*/ PLUTO_next_option(plint); str = PLUTO_get_string(plint) ; /* get string item (the method) */ meth = PLUTO_string_index( str , /* find it in list it is from */ _STAVG_NUM_METHODS , method_strings ) ; /*--------- see if the 4th option line is present --------*/ str = PLUTO_get_optiontag( plint ) ; if( str != NULL ){ user_maxlength = (int) PLUTO_get_number(plint) ; str2 = PLUTO_get_string(plint) ; /* get string item (the method) */ no1 = PLUTO_string_index( str2 , /* find it in list it is from */ 2 , yes_no_strings) ; } /*------------------------------------------------------*/ /*---------- At this point, the inputs are OK ----------*/ PLUTO_popup_meter( plint ) ; /* popup a progress meter */ /*________________[ Main Code ]_________________________*/ fout = avg_epochs( old_dset, stimar, user_maxlength, 1, meth, plint ); if( fout == NULL ) return " \nError in avg_epochs() function!\n " ; if( RMB_DEBUG ) fprintf(stderr, "Done with avg_epochs\n"); maxlength = M_maxlength; /*______________________________________________________*/ new_dset = EDIT_empty_copy( old_dset ) ; /* start with copy of old one */ { char * his = PLUTO_commandstring(plint) ; tross_Copy_History( old_dset , new_dset ) ; tross_Append_History( new_dset , his ) ; free( his ) ; } /*-- edit some of its internal parameters --*/ ii = EDIT_dset_items( new_dset , ADN_prefix , new_prefix , /* filename prefix */ ADN_malloc_type , DATABLOCK_MEM_MALLOC , /* store in memory */ ADN_datum_all , new_datum , /* atomic datum */ ADN_nvals , maxlength , /* # sub-bricks */ ADN_ntt , maxlength , /* # time points */ /* ADN_ttorg , old_dtime , */ /* time origin */ /* ADN_ttdel , old_dtime , */ /* time step */ /* ADN_ttdur , old_dtime , */ /* time duration */ /* ADN_nsl , 0 , */ /* z-axis time slicing */ /* ADN_tunits , old_units , */ /* time units */ ADN_none ) ; if( ii != 0 ){ THD_delete_3dim_dataset( new_dset , False ) ; FREE_WORKSPACE ; return "***********************************\n" "Error while creating output dataset\n" "***********************************" ; } /*------------------------------------------------------------*/ /*------- The output is now in fout[kk][ii], for kk=0..maxlength-1 , ii=0..nvox-1. We must now put this into the output dataset -------*/ switch( new_datum ){ /*** output is floats is the simplest: we just have to attach the fout bricks to the dataset ***/ case MRI_float: for( kk=0 ; kk < maxlength ; kk++ ) EDIT_substitute_brick( new_dset , kk , MRI_float , fout[kk] ) ; break ; /*** output is shorts: we have to create a scaled sub-brick from fout ***/ case MRI_short:{ short * bout ; float fac ; for( kk=0 ; kk < maxlength ; kk++ ){ /* loop over sub-bricks */ /*-- get output sub-brick --*/ bout = (short *) malloc( sizeof(short) * nvox ) ; if( bout == NULL ){ fprintf(stderr,"\nFinal malloc error in plug_stavg!\n\a") ; return("Final malloc error in plug_stavg!"); ; /* exit(1) ;*/ } /*-- find scaling and then scale --*/ /*fac = MCW_vol_amax( nvox,1,1 , MRI_float , fout[kk] ) ;*/ fac = 1.0; EDIT_coerce_scale_type( nvox,fac , MRI_float,fout[kk] , MRI_short,bout ) ; free( fout[kk] ) ; /* don't need this anymore */ /*-- put output brick into dataset, and store scale factor --*/ EDIT_substitute_brick( new_dset , kk , MRI_short , bout ) ; } } break ; /*** output is bytes (byte = unsigned char) we have to create a scaled sub-brick from fout ***/ case MRI_byte:{ byte * bout ; float fac ; for( kk=0 ; kk < maxlength ; kk++ ){ /* loop over sub-bricks */ /*-- get output sub-brick --*/ bout = (byte *) malloc( sizeof(byte) * nvox ) ; if( bout == NULL ){ fprintf(stderr,"\nFinal malloc error in plug_stavg!\n\a") ; return("Final malloc error in plug_stavg!"); ; /* exit(1) ;*/ } /*-- find scaling and then scale --*/ fac = 1.0; EDIT_coerce_scale_type( nvox,fac , MRI_float,fout[kk] , MRI_byte,bout ) ; free( fout[kk] ) ; /* don't need this anymore */ /*-- put output brick into dataset, and store scale factor --*/ EDIT_substitute_brick( new_dset , kk , MRI_byte , bout ) ; } } break ; } /* end of switch on output data type */ /*-------------- Cleanup and go home ----------------*/ PLUTO_set_meter( plint , 100 ) ; /* set progress meter to 100% */ PLUTO_add_dset( plint , new_dset , DSET_ACTION_MAKE_CURRENT ) ; FREE_WORKSPACE ; return NULL ; /* null string returned means all was OK */ }
static char * EXTRACT_main( PLUGIN_interface * plint ) { extract_data uda,*ud; MRI_IMAGE * tsim; MCW_idcode * idc ; /* input dataset idcode */ THD_3dim_dataset * old_dset , * new_dset ; /* input and output datasets */ char *tmpstr , * str , *nprfxstr; int ntime, nvec ,nprfx, Err=0 , itmp; float * vec , fs , T ; char * tag; /* plugin option tag */ /* Allocate as much character space as Bob specifies in afni.h + a bit more */ tmpstr = (char *) calloc (PLUGIN_MAX_STRING_RANGE+10,sizeof(char)); nprfxstr = (char *) calloc (PLUGIN_MAX_STRING_RANGE+10,sizeof(char)); if (tmpstr == NULL || nprfxstr == NULL) return "********************\n" "Could not Allocate\n" "a teeni weeni bit of\n" "Memory ! Go complain\n" "to yer Mamma ! \n" "********************\n"; ud = &uda; /* ud now points to an allocated space */ ud->errcode = 0; /*reset error flag */ /*--------------------------------------------------------------------*/ /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/ /*--------- go to first input line ---------*/ tag = PLUTO_get_optiontag(plint) ; if (tag == NULL) { return "************************\n" "Bad 1st line option \n" "************************" ; } idc = PLUTO_get_idcode(plint) ; /* get dataset item */ old_dset = PLUTO_find_dset(idc) ; /* get ptr to dataset */ if( old_dset == NULL ) return "*************************\n" "Cannot find Input Dataset\n" "*************************" ; ud->dsetname = DSET_FILECODE (old_dset); ud->ignore = PLUTO_get_number(plint) ; /* get number item */ str = PLUTO_get_string(plint) ; ud->dtrnd = (int)PLUTO_string_index( str , NUM_YN_STRINGS , yn_strings ); /*--------- loop over ramining options ---------*/ ud->iloc = -1; ud->xloc = -1; ud->yloc = -1; ud->zloc = -1; do { tag = PLUTO_get_optiontag(plint) ; if (tag == NULL) break; if (strcmp (tag, "Mask") == 0) { ud->strin = PLUTO_get_string(plint) ; ud->ncols = PLUTO_get_number(plint) ; ud->pass = PLUTO_get_number(plint) ; ud->fail = 0; /* Set voxels that don't make it to 0 */ continue; } if (strcmp (tag, "Index") == 0) { ud->iloc = PLUTO_get_number(plint) ; /* get number item */ continue; } if (strcmp (tag, "XYZ") == 0) { ud->xloc = PLUTO_get_number(plint) ; /* get number item */ ud->yloc = PLUTO_get_number(plint) ; /* get number item */ ud->zloc = PLUTO_get_number(plint) ; /* get number item */ continue; } if (strcmp (tag, "Output") == 0) { ud->new_prefix = PLUTO_get_string(plint) ; /* get string item (the output prefix) */ /* check to see if the field is empty */ if (ud->new_prefix == NULL) nprfx = 0; else nprfx = 1; /* check if the size is larger than 0. I did not want to check for this unless it's allocated */ if (nprfx == 1 && (int)strlen (ud->new_prefix) == 0) nprfx = 0; if (nprfx == 0) /* now create the new name and make new_prefix point to it */ { sprintf (nprfxstr,"%s.XTRCT",DSET_PREFIX (old_dset)); ud->new_prefix = nprfxstr; /*printf ("New prefix is set to be : %s\n\a",ud->new_prefix);*/ } if( ! PLUTO_prefix_ok(ud->new_prefix) ) /* check if it is OK */ return "************************\n" "Output Prefix is illegal\n" "************************" ; ud->strout = PLUTO_get_string(plint) ; str = PLUTO_get_string(plint) ; ud->format = (int)PLUTO_string_index( str , NUM_FORMAT_STRINGS , format_strings ); continue; } } while (1); /* ------------------ Check for some errorsor inconsistencies ------------- */ if (ud->iloc == -1 && ud->xloc == -1) { return "**************************\n" "At least iloc or x/y/zloc\n" "must be specified\n" "**************************\n" ; } if (ud->iloc != -1 && ud->xloc != -1) { return "***************************\n" "iloc AND x/y/zloc can not\n" "be simultaneously specified\n" "***************************\n" ; } /* ------------------Done with user parameters ---------------------------- */ /* Now loadup that index list or the xyz list */ if (ud->iloc != -1) { itmp = 0; /* might want to give option of setting it to number of rows if*/ /* the users know it, otherwise, it is automatically determined*/ ud->indvect = extract_index (ud->strin, ud->iloc, ud->ncols, &itmp, &Err); } else /* assuming the only other case is that x y z are specified */ { itmp = 0; ud->xyzvect = extract_xyz (ud->strin , ud->xloc , ud->yloc , ud->zloc , ud->ncols, &itmp, &Err); } ud->nrows = itmp; switch (Err) { case (0): break; case (1): return "****************************************\n" "index location should be > 0 and < ncols\n" "****************************************\n"; case (2): return "********************************************\n" "file size and number of columns do not match\n" "********************************************\n"; case (3): return "**********************\n" "Can't find matrix file\n" "**********************\n"; case (4): return "*****************\n" "ncols must be > 0\n" "*****************\n"; case (5): return "****************************************\n" "x/y/z column numbers can NOT be the same\n" "****************************************\n"; default: return "****************************************\n" "Should not have gotten here .....\n" "****************************************\n"; } if (strcmp (ud->strout,"") == 0) /* no output file is specified */ { sprintf ( tmpstr , "%s" , ud->new_prefix); ud->strout = tmpstr; } if (filexists(ud->strout) == 1) { return "*******************************\n" "Outfile exists, won't overwrite\n" "*******************************\n"; } ud->outwritets = fopen (ud->strout ,"w"); sprintf ( tmpstr , "%s.log" , ud->strout); if (filexists(tmpstr) == 1) { return "*******************************\n" "Outfile.log exists, won't overwrite\n" "*******************************\n"; } ud->outlogfile = fopen (tmpstr ,"w"); if ((ud->outwritets == NULL) || (ud->outlogfile == NULL) ) { ud->errcode = ERROR_FILEWRITE; return "***********************\n" "Could Not Write Outfile\n" "***********************\n"; } ud->nxx = (int)old_dset->daxes->nxx; /* get data set dimensions */ ud->nyy = (int)old_dset->daxes->nyy; ud->nzz = (int)old_dset->daxes->nzz; /* ready to dump the log file */ write_ud (ud); /*------------- ready to compute new dataset -----------*/ new_dset = PLUTO_4D_to_typed_fim( old_dset , /* input dataset */ ud->new_prefix , /* output prefix */ -1, /* negative value indicating data type is like original brick */ ud->ignore , /* ignore count */ ud->dtrnd , /* detrend */ EXTRACT_tsfunc , /* timeseries processor */ (void *)ud /* data for tsfunc */ ) ; PLUTO_add_dset( plint , new_dset , DSET_ACTION_MAKE_CURRENT ) ; fclose (ud->outlogfile); fclose (ud->outwritets); free (tmpstr); free (nprfxstr); return NULL ; /* null string returned means all was OK */ }
char * Fourier_Main( PLUGIN_interface * plint ) { THD_3dim_dataset *input=NULL ; char *tag; /* New call, so reinitialize variables */ low_fc=0; high_fc = 0; autocorr=FALSE; retrend=FALSE; output_prefix=NULL, idc=NULL; /*--------------------------------------------------------------------*/ /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/ if( plint == NULL ) return "*************************\n" "Fourier_Main: NULL input\n" "*************************" ; tag = PLUTO_get_optiontag(plint) ; while( tag != NULL ){ if (strcmp(tag, "Input") == 0 ) { idc = PLUTO_get_idcode(plint) ; input = PLUTO_find_dset(idc) ; if( input == NULL ) return "********************************\n" "Fourier_Main: bad input dataset\n" "********************************" ; } else if (strcmp(tag, "Preprocess") == 0) { ignore = PLUTO_get_number(plint); retrend = strcmp(PLUTO_get_string(plint), "No"); } else if( strcmp(tag,"Output") == 0 ){ output_prefix = PLUTO_get_string(plint) ; if( ! PLUTO_prefix_ok(output_prefix) ) return "*************************\n" "Fourier_Main: bad prefix\n" "*************************" ; } else if( strcmp(tag,"Lowpass") == 0 ){ low_fc = PLUTO_get_number(plint); } else if( strcmp(tag,"Highpass") == 0 ){ high_fc = PLUTO_get_number(plint); } else if( strcmp(tag,"Autocorrelate") == 0 ){ autocorr=TRUE; } tag = PLUTO_get_optiontag(plint) ; } /*------------------------------------------------------*/ /*---------- At this point, the inputs are OK ----------*/ if ((output_prefix == NULL) || (idc == NULL)) /* must be setting up for 1D transform */ return NULL ; else return Fourier_Filter_Driver(plint, input, low_fc, high_fc, ignore, autocorr, retrend, output_prefix); }
char * L1F_main( PLUGIN_interface * plint ) { char *str ; int ii ; float *tsar ; /*--------- go to first input line ---------*/ PLUTO_next_option(plint) ; str = PLUTO_get_string(plint) ; polort = PLUTO_string_index( str , NBASE , baseline_strings ) ; ignore = PLUTO_get_number(plint) ; /*------ loop over remaining options, check their tags, process them -----*/ nrsin = nrts = ntsim = 0 ; do { str = PLUTO_get_optiontag(plint) ; if( str == NULL ) break ; if( strcmp(str,"Sinusoid") == 0 ){ sinper[nrsin] = PLUTO_get_number(plint) ; sinharm[nrsin] = PLUTO_get_number(plint) - 1.0 ; if( sinper[nrsin] <= 0.0 ) return "************************\n" "Illegal Sinusoid Period!\n" "************************" ; nrsin++ ; } else if( strcmp(str,"Timeseries") == 0 ){ tsim[ntsim] = PLUTO_get_timeseries(plint) ; if( tsim[ntsim] == NULL || tsim[ntsim]->nx < 3 || tsim[ntsim]->kind != MRI_float ) return "*************************\n" "Illegal Timeseries Input!\n" "*************************" ; tsar = MRI_FLOAT_PTR(tsim[ntsim]) ; for( ii=ignore ; ii < tsim[ntsim]->nx && tsar[ii] >= WAY_BIG ; ii++ ) ; /* nada */ ignore = ii ; nrts += tsim[ntsim]->ny ; ntsim++ ; } else { return "************************\n" "Illegal optiontag found!\n" "************************" ; } } while(1) ; /*--- nothing left to do until data arrives ---*/ initialize = 1 ; /* force re-initialization */ /*** compute how many ref functions are ordered ***/ { int nref , ks ; char str[64] ; nref = (polort+1) + nrts ; for( ks=0 ; ks < nrsin ; ks++ ) nref += 2*(sinharm[ks]+1) ; sprintf(str," \nNumber of fit parameters = %d\n",nref) ; PLUTO_popup_transient( plint , str ) ; } return NULL ; }
char * MASKAVE_main( PLUGIN_interface * plint ) { MCW_idcode * idc ; THD_3dim_dataset * input_dset , * mask_dset ; int iv , mcount , nvox , ii , sigmait , nvals=0 , doall , ivbot,ivtop ; float mask_bot=666.0 , mask_top=-666.0 ; double sum=0.0 , sigma=0.0 ; float * sumar=NULL , * sigmar=NULL ; char * tag , * str , buf[64] , abuf[32],sbuf[32] ; byte * mmm ; char * cname=NULL ; /* 06 Aug 1998 */ int cdisk=0 ; /* 22 Aug 2000 */ int miv=0 ; /*--------------------------------------------------------------------*/ /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/ if( plint == NULL ) return "*************************\n" "MASKAVE_main: NULL input\n" "*************************" ; /*-- read 1st line --*/ PLUTO_next_option(plint) ; idc = PLUTO_get_idcode(plint) ; input_dset = PLUTO_find_dset(idc) ; if( input_dset == NULL ) return "********************************\n" "MASKAVE_main: bad input dataset\n" "********************************" ; iv = (int) PLUTO_get_number(plint) ; if( iv >= DSET_NVALS(input_dset) ) return "**********************************\n" "MASKAVE_main: bad input sub-brick\n" "**********************************" ; doall = (iv < 0) ; if( doall ){ nvals = DSET_NVALS(input_dset) ; ivbot = 0 ; ivtop = nvals-1 ; } else { ivbot = ivtop = iv ; } DSET_load(input_dset) ; if( DSET_ARRAY(input_dset,ivbot) == NULL ) return "*********************************\n" "MASKAVE_main: can't load dataset\n" "*********************************" ; nvox = DSET_NVOX(input_dset) ; /*-- read 2nd line --*/ PLUTO_next_option(plint) ; idc = PLUTO_get_idcode(plint) ; mask_dset = PLUTO_find_dset(idc) ; if( mask_dset == NULL ) return "*******************************\n" "MASKAVE_main: bad mask dataset\n" "*******************************" ; if( DSET_NVOX(mask_dset) != nvox ) return "*************************************************************\n" "MASKAVE_main: mask input dataset doesn't match source dataset\n" "*************************************************************" ; miv = (int) PLUTO_get_number(plint) ; /* 06 Aug 1998 */ if( miv >= DSET_NVALS(mask_dset) ) return "*****************************************************\n" "MASKAVE_main: mask dataset sub-brick index is too big\n" "*****************************************************" ; DSET_load(mask_dset) ; if( DSET_ARRAY(mask_dset,0) == NULL ) return "**************************************\n" "MASKAVE_main: can't load mask dataset\n" "**************************************" ; /*-- read optional lines --*/ while( (tag=PLUTO_get_optiontag(plint)) != NULL ){ if( strcmp(tag,"Range") == 0 ){ mask_bot = PLUTO_get_number(plint) ; mask_top = PLUTO_get_number(plint) ; continue ; } if( strcmp(tag,"1D Save") == 0 ){ char * yn ; cname = PLUTO_get_string(plint) ; yn = PLUTO_get_string(plint) ; cdisk = (strcmp(yn,yesno_list[0]) == 0) ; continue ; } } /*------------------------------------------------------*/ /*---------- At this point, the inputs are OK ----------*/ /*-- build a byte mask array --*/ mmm = (byte *) malloc( sizeof(byte) * nvox ) ; if( mmm == NULL ) return "*** Can't malloc workspace! ***" ; /* separate code for each input data type */ switch( DSET_BRICK_TYPE(mask_dset,miv) ){ default: free(mmm) ; return "*** Can't use mask dataset -- illegal data type! ***" ; case MRI_short:{ short mbot , mtop ; short * mar = (short *) DSET_ARRAY(mask_dset,miv) ; float mfac = DSET_BRICK_FACTOR(mask_dset,miv) ; if( mfac == 0.0 ) mfac = 1.0 ; if( mask_bot <= mask_top ){ mbot = SHORTIZE(mask_bot/mfac) ; mtop = SHORTIZE(mask_top/mfac) ; } else { mbot = (short) -MRI_TYPE_maxval[MRI_short] ; mtop = (short) MRI_TYPE_maxval[MRI_short] ; } for( mcount=0,ii=0 ; ii < nvox ; ii++ ) if( mar[ii] >= mbot && mar[ii] <= mtop && mar[ii] != 0 ){ mmm[ii] = 1 ; mcount++ ; } else { mmm[ii] = 0 ; } } break ; case MRI_byte:{ byte mbot , mtop ; byte * mar = (byte *) DSET_ARRAY(mask_dset,miv) ; float mfac = DSET_BRICK_FACTOR(mask_dset,miv) ; if( mfac == 0.0 ) mfac = 1.0 ; if( mask_bot <= mask_top ){ mbot = BYTEIZE(mask_bot/mfac) ; mtop = BYTEIZE(mask_top/mfac) ; if( mtop == 0 ){ free(mmm) ; return "*** Illegal mask range for mask dataset of bytes. ***" ; } } else { mbot = 0 ; mtop = (byte) MRI_TYPE_maxval[MRI_short] ; } for( mcount=0,ii=0 ; ii < nvox ; ii++ ) if( mar[ii] >= mbot && mar[ii] <= mtop && mar[ii] != 0 ){ mmm[ii] = 1 ; mcount++ ; } else { mmm[ii] = 0 ; } } break ; case MRI_float:{ float mbot , mtop ; float * mar = (float *) DSET_ARRAY(mask_dset,miv) ; float mfac = DSET_BRICK_FACTOR(mask_dset,miv) ; if( mfac == 0.0 ) mfac = 1.0 ; if( mask_bot <= mask_top ){ mbot = (float) (mask_bot/mfac) ; mtop = (float) (mask_top/mfac) ; } else { mbot = -WAY_BIG ; mtop = WAY_BIG ; } for( mcount=0,ii=0 ; ii < nvox ; ii++ ) if( mar[ii] >= mbot && mar[ii] <= mtop && mar[ii] != 0 ){ mmm[ii] = 1 ; mcount++ ; } else { mmm[ii] = 0 ; } } break ; } if( mcount == 0 ){ free(mmm) ; return "*** No voxels survive the masking operations! ***" ; } sigmait = (mcount > 1) ; /*-- compute statistics --*/ if( doall ){ sumar = (float *) malloc( sizeof(float) * nvals ) ; sigmar = (float *) malloc( sizeof(float) * nvals ) ; } for( iv=ivbot ; iv <= ivtop ; iv++ ){ sum = sigma = 0.0 ; /* 13 Dec 1999 */ switch( DSET_BRICK_TYPE(input_dset,iv) ){ default: free(mmm) ; if( doall ){ free(sumar) ; free(sigmar) ; } return "*** Can't use source dataset -- illegal data type! ***" ; case MRI_short:{ short * bar = (short *) DSET_ARRAY(input_dset,iv) ; float mfac = DSET_BRICK_FACTOR(input_dset,iv) ; if( mfac == 0.0 ) mfac = 1.0 ; for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sum += bar[ii] ; sum = sum / mcount ; if( sigmait ){ for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sigma += SQR(bar[ii]-sum) ; sigma = mfac * sqrt( sigma/(mcount-1) ) ; } sum = mfac * sum ; } break ; case MRI_byte:{ byte * bar = (byte *) DSET_ARRAY(input_dset,iv) ; float mfac = DSET_BRICK_FACTOR(input_dset,iv) ; if( mfac == 0.0 ) mfac = 1.0 ; for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sum += bar[ii] ; sum = sum / mcount ; if( sigmait ){ for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sigma += SQR(bar[ii]-sum) ; sigma = mfac * sqrt( sigma/(mcount-1) ) ; } sum = mfac * sum ; } break ; case MRI_float:{ float * bar = (float *) DSET_ARRAY(input_dset,iv) ; float mfac = DSET_BRICK_FACTOR(input_dset,iv) ; if( mfac == 0.0 ) mfac = 1.0 ; for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sum += bar[ii] ; sum = sum / mcount ; if( sigmait ){ for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sigma += SQR(bar[ii]-sum) ; sigma = mfac * sqrt( sigma/(mcount-1) ) ; } sum = mfac * sum ; } break ; } if( doall ){ sumar[iv] = sum ; sigmar[iv] = sigma ; } } free(mmm) ; /*-- send report --*/ if( doall ){ str = (char *) malloc( 1024 + 64*nvals ) ; sprintf(str," ****** ROI statistics ****** \n" " Source = %s [all sub-bricks] \n" " Mask = %s [%s]" , DSET_FILECODE(input_dset) , DSET_FILECODE(mask_dset) , DSET_BRICK_LABEL(mask_dset,miv) ) ; if( mask_bot <= mask_top ){ sprintf(buf," [range %g .. %g]" , mask_bot , mask_top ) ; strcat(str,buf) ; } strcat(str," \n") ; sprintf(buf," Count = %d voxels\n",mcount) ; strcat(str,buf) ; for( iv=0 ; iv < nvals ; iv++ ){ AV_fval_to_char( sumar[iv] , abuf ) ; AV_fval_to_char( sigmar[iv] , sbuf ) ; sprintf(buf," Average = %9.9s Sigma = %9.9s [%s] \n", abuf,sbuf , DSET_BRICK_LABEL(input_dset,iv) ) ; strcat(str,buf) ; } PLUTO_popup_textwin( plint , str ) ; /* 06 Aug 1998 */ if( cname != NULL && cname[0] != '\0' ){ MRI_IMAGE * qim = mri_new_vol_empty( nvals,1,1 , MRI_float ) ; mri_fix_data_pointer( sumar , qim ) ; PLUTO_register_timeseries( cname , qim ) ; if( cdisk ){ /* 22 Aug 2000 */ if( PLUTO_prefix_ok(cname) ){ char * cn ; if( strstr(cname,".1D") == NULL ){ cn = malloc(strlen(cname)+8) ; strcpy(cn,cname) ; strcat(cn,".1D") ; } else { cn = cname ; } mri_write_1D( cn , qim ) ; if( cn != cname ) free(cn) ; } else { PLUTO_popup_transient(plint," \n" "** Illegal filename **\n" "** in 'To Disk?' !! **\n" ) ; } } mri_fix_data_pointer( NULL , qim ) ; mri_free(qim) ; } free(str) ; free(sumar) ; free(sigmar) ; } else if( mask_bot <= mask_top ){ str = (char *) malloc( 1024 ) ; sprintf( str , " *** ROI Statistics *** \n" " Source = %s [%s] \n" " Mask = %s [%s] [range %g .. %g] \n" " Count = %d voxels \n" " Average = %g \n" " Sigma = %g " , DSET_FILECODE(input_dset) , DSET_BRICK_LABEL(input_dset,ivbot) , DSET_FILECODE(mask_dset) , DSET_BRICK_LABEL(mask_dset,miv) , mask_bot , mask_top , mcount , sum , sigma ) ; PLUTO_popup_message(plint,str) ; free(str) ; } else { str = (char *) malloc( 1024 ) ; sprintf( str , " *** ROI Statistics *** \n" " Source = %s [%s] \n" " Mask = %s [%s] \n" " Count = %d voxels \n" " Average = %g \n" " Sigma = %g " , DSET_FILECODE(input_dset) , DSET_BRICK_LABEL(input_dset,ivbot) , DSET_FILECODE(mask_dset) , DSET_BRICK_LABEL(mask_dset,miv) , mcount , sum , sigma ) ; PLUTO_popup_message(plint,str) ; free(str) ; } return NULL ; }
static char * CLUST_main( PLUGIN_interface * plint ) { char * tag , * new_prefix ; float rmm , vmul , thresh ; MCW_idcode * idc ; THD_3dim_dataset * dset , * new_dset ; int ival , ityp , nbytes , nvals ; EDIT_options edopt ; void * new_brick , * old_brick ; int clust_type; /* input cluster type option */ /* 19 June 1998 */ char * str; /* input string */ float pv; /* pv % voxels within rmm must be active */ int dilate; /* boolean for perform dilation of cluster voxels */ /*--------------------------------------------------------------------*/ /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/ if( plint == NULL ) return "**********************\n" "CLUST_main: NULL input\n" "**********************" ; tag = PLUTO_get_optiontag(plint) ; if( tag==NULL || strcmp(tag,"Dataset") != 0 ) return "**********************************\n" "CLUST_main: bad Dataset option tag\n" "**********************************" ; idc = PLUTO_get_idcode(plint) ; dset = PLUTO_find_dset(idc) ; if( dset == NULL ) return "*****************************\n" "CLUST_main: bad input dataset\n" "*****************************" ; tag = PLUTO_get_optiontag(plint) ; if( tag==NULL || strcmp(tag,"Params") != 0 ) return "*********************************\n" "CLUST_main: bad Params option tag\n" "*********************************" ; str = PLUTO_get_string(plint); /* 19 June 1998 */ if (strcmp(str,"Keep") == 0) clust_type = ECFLAG_SAME; else if (strcmp(str,"Mean") == 0) clust_type = ECFLAG_MEAN; else if (strcmp(str,"Max") == 0) clust_type = ECFLAG_MAX; else if (strcmp(str,"AMax") == 0) clust_type = ECFLAG_AMAX; else if (strcmp(str,"SMax") == 0) clust_type = ECFLAG_SMAX; else if (strcmp(str,"Size") == 0) clust_type = ECFLAG_SIZE; else if (strcmp(str,"Order") == 0) clust_type = ECFLAG_ORDER; else if (strcmp(str,"Depth") == 0) clust_type = ECFLAG_DEPTH; else return "**********************************\n" "CLUST_main: Illegal Cluster option\n" "**********************************"; rmm = PLUTO_get_number(plint) ; vmul = PLUTO_get_number(plint) ; if( rmm <= 0 || vmul <= 0 ) return "****************************\n" "CLUST_main: bad Params input\n" "****************************" ; /* 19 June 1998 */ tag = PLUTO_peek_optiontag(plint) ; if( tag != NULL && strcmp(tag,"Erode/Dilate") == 0 ) { PLUTO_next_option(plint) ; pv = PLUTO_get_number(plint); if ((pv > 0.0) && (rmm <= 0.0)) return "*******************************************************\n" "CLUST_main: Erode/Dilate requires use of Cluster option\n" "*******************************************************"; else pv = pv / 100.0; str = PLUTO_get_string(plint); if (strcmp (str, "True") == 0) { if (pv <= 0.0) return "***********************************************\n" "CLUST_main: Dilate requires use of Erode option\n" "***********************************************"; else dilate = 1; } else dilate = 0; } else { pv = 0.0; dilate = 0; } tag = PLUTO_peek_optiontag(plint) ; if( tag != NULL && strcmp(tag,"Threshold") == 0 ){ PLUTO_next_option(plint) ; thresh = PLUTO_get_number(plint) ; if( thresh < 0.0 ) return "*******************************\n" "CLUST_main: bad Threshold input\n" "*******************************" ; if( thresh > 0.0 && DSET_THRESH_INDEX(dset) < 0 ) return "**********************************************\n" "CLUST_main: Dataset has no threshold sub-brick\n" "**********************************************" ; } else { thresh = 0.0 ; } tag = PLUTO_peek_optiontag(plint) ; if( tag != NULL && strcmp(tag,"Output") == 0 ){ PLUTO_next_option(plint) ; new_prefix = PLUTO_get_string(plint) ; if( ! PLUTO_prefix_ok(new_prefix) ) return "**********************\n" "CLUST_main: bad prefix\n" "**********************" ; } else { new_prefix = NULL ; } /*------------------------------------------------------*/ /*---------- At this point, the inputs are OK ----------*/ /*-- maybe, make a new dataset --*/ if( new_prefix == NULL ){ /* no prefix => edit input in place */ new_dset = dset ; DSET_load( dset ) ; /* load into memory */ } else { /* OK, make a copy first */ new_dset = PLUTO_copy_dset( dset , new_prefix ) ; if( new_dset == NULL ) return "****************************************\n" "CLUST_main: failed to copy input dataset\n" "****************************************" ; DSET_unload( dset ) ; /* unload from memory */ } /*-- set up for dataset editing --*/ INIT_EDOPT( &edopt ) ; edopt.edit_clust = clust_type; /* 19 June 1998 */ edopt.clust_rmm = rmm ; edopt.clust_vmul = vmul ; edopt.erode_pv = pv; edopt.dilate = dilate; if( thresh > 0.0 ) edopt.thresh = thresh ; /*-- edit the new dataset --*/ EDIT_one_dataset( new_dset , &edopt ) ; /*-- if we made a completely new dataset, give it to AFNI --*/ if( new_dset != dset ){ ival = PLUTO_add_dset( plint , new_dset , DSET_ACTION_MAKE_CURRENT ) ; if( ival ){ THD_delete_3dim_dataset( new_dset , False ) ; return "**********************************************\n" "CLUST_main: failure to add new dataset to AFNI\n" "**********************************************" ; } } else { DSET_overwrite( new_dset ) ; /* otherwise, re-write to disk */ PLUTO_force_redisplay() ; /* and force a redisplay of images */ } /*-- done successfully!!! --*/ return NULL ; }
static char * BFIT_main( PLUGIN_interface * plint ) { MCW_idcode * idc ; THD_3dim_dataset * input_dset , * mask_dset = NULL ; BFIT_data * bfd ; BFIT_result * bfr ; int nvals,ival , nran,nvox , nbin , miv=0 , sqr,sqt ; float abot,atop,bbot,btop,pcut , eps,eps1 , hlast ; float *bval , *cval ; double aa,bb,xc ; double chq,ccc,cdf ; int ihqbot,ihqtop ; int mcount,mgood , ii , jj , ibot,itop ; float mask_bot=666.0 , mask_top=-666.0 , hbot,htop,dbin ; char buf[THD_MAX_NAME+128] , tbuf[THD_MAX_NAME+128] , * tag ; int * hbin , * jbin,*kbin=NULL , *jist[2] ; MRI_IMAGE * flim ; double aext=-1.0,bext=-1.0 ; /*--------------------------------------------------------------------*/ /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/ if( plint == NULL ) return "************************\n" "BFIT_main: NULL input\n" "************************" ; /*-- read 1st line --*/ PLUTO_next_option(plint) ; idc = PLUTO_get_idcode(plint) ; input_dset = PLUTO_find_dset(idc) ; if( input_dset == NULL ) return "****************************\n" "BFIT_main: bad input dataset\n" "****************************" ; nvox = DSET_NVOX(input_dset) ; nvals = DSET_NVALS(input_dset) ; ival = (int) PLUTO_get_number(plint) ; if( ival < 0 || ival >= nvals ) return "**************************\n" "BFIT_main: bad Brick index\n" "**************************" ; DSET_load(input_dset) ; if( DSET_ARRAY(input_dset,0) == NULL ) return "*****************************\n" "BFIT_main: can't load dataset\n" "*****************************" ; tag = PLUTO_get_string(plint) ; sqr = PLUTO_string_index(tag,NYESNO,YESNO_strings) ; /*-- read 2nd line --*/ PLUTO_next_option(plint) ; abot = PLUTO_get_number(plint) ; atop = PLUTO_get_number(plint) ; if( atop <= abot ) return "*** atop <= abot! ***" ; PLUTO_next_option(plint) ; bbot = PLUTO_get_number(plint) ; btop = PLUTO_get_number(plint) ; if( atop <= abot ) return "*** btop <= bbot! ***" ; hlast = PLUTO_get_number(plint) ; PLUTO_next_option(plint) ; nran = (int) PLUTO_get_number(plint) ; pcut = PLUTO_get_number(plint) ; tag = PLUTO_get_string(plint) ; sqt = PLUTO_string_index(tag,NYESNO,YESNO_strings) ; /*-- read optional lines --*/ while( (tag=PLUTO_get_optiontag(plint)) != NULL ){ /*-- Mask itself --*/ if( strcmp(tag,"Mask") == 0 ){ idc = PLUTO_get_idcode(plint) ; mask_dset = PLUTO_find_dset(idc) ; if( mask_dset == NULL ){ return "******************************\n" "BFIT_main: bad mask dataset\n" "******************************" ; } if( DSET_NVOX(mask_dset) != nvox ){ return "************************************************************\n" "BFIT_main: mask input dataset doesn't match source dataset\n" "************************************************************" ; } miv = (int) PLUTO_get_number(plint) ; if( miv >= DSET_NVALS(mask_dset) || miv < 0 ){ return "****************************************************\n" "BFIT_main: mask dataset sub-brick index is illegal\n" "****************************************************" ; } DSET_load(mask_dset) ; if( DSET_ARRAY(mask_dset,miv) == NULL ){ return "*************************************\n" "BFIT_main: can't load mask dataset\n" "*************************************" ; } continue ; } /*-- Mask range of values --*/ if( strcmp(tag,"Range") == 0 ){ if( mask_dset == NULL ){ return "******************************************\n" "BFIT_main: Can't use Range without Mask\n" "******************************************" ; } mask_bot = PLUTO_get_number(plint) ; mask_top = PLUTO_get_number(plint) ; continue ; } /*-- Extra plot --*/ if( strcmp(tag,"Extra") == 0 ){ aext = PLUTO_get_number(plint) ; bext = PLUTO_get_number(plint) ; continue ; } } /*------------------------------------------------------*/ /*---------- At this point, the inputs are OK ----------*/ bfd = BFIT_prepare_dataset( input_dset , ival , sqr , mask_dset , miv , mask_bot , mask_top ) ; if( bfd == NULL ) return "*** BFIT_prepare_dataset fails ***" ; bfr = BFIT_compute( bfd , pcut , abot,atop , bbot,btop , nran,200 ) ; if( bfr == NULL ){ BFIT_free_data( bfd ) ; return "*** BFIT_compute fails! ***" ; } itop = bfr->itop ; mgood = bfr->mgood ; ibot = bfd->ibot ; bval = bfd->bval ; cval = bfd->cval ; mcount = bfd->mcount ; xc = bfr->xcut ; aa = bfr->a ; bb = bfr->b ; eps = bfr->eps ; eps1 = 1.0 - eps ; if( eps1 > 1.0 ) eps1 = 1.0 ; eps1 = (mcount-ibot) * eps1 ; /*-- compute and plot histogram --*/ /* original data was already squared (e.g., R**2 values) */ if( !sqr ){ hbot = 0.0 ; htop = 1.0 ; nbin = 200 ; if( bval[mcount-1] < 1.0 ) htop = bval[mcount-1] ; dbin = (htop-hbot)/nbin ; hbin = (int *) calloc((nbin+1),sizeof(int)) ; /* actual histogram */ jbin = (int *) calloc((nbin+1),sizeof(int)) ; /* theoretical fit */ for( ii=0 ; ii < nbin ; ii++ ){ /* beta fit */ jbin[ii] = (int)( eps1 * ( beta_t2p(hbot+ii*dbin,aa,bb) -beta_t2p(hbot+ii*dbin+dbin,aa,bb) ) ) ; } jist[0] = jbin ; flim = mri_new_vol_empty( mcount-ibot,1,1 , MRI_float ) ; mri_fix_data_pointer( bval+ibot , flim ) ; mri_histogram( flim , hbot,htop , TRUE , nbin,hbin ) ; /* "extra" histogram (nominal values?) */ if( aext > 0.0 ){ kbin = (int *) calloc((nbin+1),sizeof(int)) ; jist[1] = kbin ; for( ii=0 ; ii < nbin ; ii++ ){ /* beta fit */ kbin[ii] = (int)( eps1 * ( beta_t2p(hbot+ii*dbin,aext,bext) -beta_t2p(hbot+ii*dbin+dbin,aext,bext) ) ) ; } } } else { /* original data was not squared (e.g., correlations) */ double hb,ht ; htop = 1.0 ; nbin = 200 ; if( bval[mcount-1] < 1.0 ) htop = sqrt(bval[mcount-1]) ; hbot = -htop ; dbin = (htop-hbot)/nbin ; hbin = (int *) calloc((nbin+1),sizeof(int)) ; /* actual histogram */ jbin = (int *) calloc((nbin+1),sizeof(int)) ; /* theoretical fit */ for( ii=0 ; ii < nbin ; ii++ ){ /* beta fit */ hb = hbot+ii*dbin ; ht = hb+dbin ; hb = hb*hb ; ht = ht*ht ; if( hb > ht ){ double qq=hb ; hb=ht ; ht=qq ; } jbin[ii] = (int)( 0.5*eps1 * ( beta_t2p(hb,aa,bb) -beta_t2p(ht,aa,bb) ) ) ; } jist[0] = jbin ; flim = mri_new_vol_empty( mcount-ibot,1,1 , MRI_float ) ; mri_fix_data_pointer( cval+ibot , flim ) ; mri_histogram( flim , hbot,htop , TRUE , nbin,hbin ) ; /* nominal fit */ if( aext > 0.0 ){ kbin = (int *) calloc((nbin+1),sizeof(int)) ; jist[1] = kbin ; for( ii=0 ; ii < nbin ; ii++ ){ /* beta fit */ hb = hbot+ii*dbin ; ht = hb+dbin ; hb = hb*hb ; ht = ht*ht ; if( hb > ht ){ double qq=hb ; hb=ht ; ht=qq ; } kbin[ii] = (int)( 0.5*eps1 * ( beta_t2p(hb,aext,bext) -beta_t2p(ht,aext,bext) ) ) ; } } } sprintf(buf,"%s[%d] a=%.2f b=%.2f \\epsilon=%.2f %%=%.0f", DSET_FILECODE(input_dset),ival,aa,bb,eps,pcut ) ; ccc = bfr->q_chisq ; /* blow up histogram details by sqrt-ing, if ordered */ if( sqt ){ for( ii=0 ; ii < nbin ; ii++ ){ hbin[ii] = (int) sqrt( (double)(100*hbin[ii]+0.5) ) ; jbin[ii] = (int) sqrt( (double)(100*jbin[ii]+0.5) ) ; if( kbin!=NULL ) kbin[ii] = (int) sqrt( (double)(100*kbin[ii]+0.5) ) ; } } /* and plot */ sprintf(tbuf,"\\beta fit: cutoff=%.2f nvox=%d q(\\chi^2)=%8.2e", (sqr)?sqrt(xc):xc , mgood , ccc ) ; if( sqt ){ ii = strlen(tbuf) ; sprintf( tbuf+ii , " \\surd ogram" ) ; } if( hlast > 0.0 ){ hbin[nbin-1] = jbin[nbin-1] = hlast ; if( kbin != NULL ) kbin[nbin-1] = hlast ; } PLUTO_histoplot( nbin,hbot,htop,hbin , tbuf,NULL,buf , (kbin==NULL)?1:2 , jist ) ; /* cleanup */ mri_clear_data_pointer(flim) ; mri_free(flim) ; free(hbin) ; free(jbin) ; if( kbin != NULL ) free(kbin); BFIT_free_data(bfd) ; BFIT_free_result(bfr) ; return NULL ; }
static char * ENV_main( PLUGIN_interface *plint ) { char *tag ; int ii,kk , ndone=0 , skip ; ENTRY("ENV_main") ; /*--------- loop over input lines ---------*/ while(1){ tag = PLUTO_get_optiontag(plint) ; skip = 0 ; if( tag == NULL ) break ; /* find which variable */ for( ii=0 ; ii < NUM_env_var ; ii++ ) if( strcmp(tag,env_var[ii].vname) == 0 ) break ; if( ii == NUM_env_var ) RETURN( "** ENV_main: table corruption! **" ); /* should not happen */ switch( env_var[ii].vtype ){ /* set vvalue for each type */ default: RETURN( "** ENV_main: table corruption! **" ); /* should not happen */ /* write a numeric value into the environment */ case ENV_NUMBER_FIXED: case ENV_NUMBER_EDITABLE:{ float val = PLUTO_get_number(plint) ; sprintf(env_var[ii].vvalue,"%s=%s" , env_var[ii].vname , AV_uformat_fval(val) ) ; } break ; /* write a string value into the environment */ case ENV_STRING:{ char *str = PLUTO_get_string(plint) ; int jj ; if( str == NULL || *str == '\0' ){ skip = 1 ; } else { if( env_var[ii].vcount > 0 ){ jj = PLUTO_string_index( str , env_var[ii].vcount , env_var[ii].vlist ) ; if( jj >= 0 ) sprintf(env_var[ii].vvalue,"%s=%s" , env_var[ii].vname , str ) ; else sprintf(env_var[ii].vvalue,"%s=" , env_var[ii].vname ) ; } else { sprintf(env_var[ii].vvalue,"%s=%s" , env_var[ii].vname , str ) ; } } } break ; } /* end of switch over environment variable types */ /* actually set environment variable */ if( !skip ){ putenv(env_var[ii].vvalue) ; /* call callback, if there is one */ #if 0 if( env_var[ii].vfunc != NULL ) env_var[ii].vfunc( env_var[ii].vname ) ; #else if( env_var[ii].vfunc != NULL ) AFNI_CALL_VOID_1ARG( env_var[ii].vfunc , char *, env_var[ii].vname ) ; #endif } /* turn this option off (for the user's convenience) */ for( kk=0 ; kk < plint->option_count ; kk++ ) /* find widget */ if( strcmp(tag,plint->option[kk]->tag) == 0 ) break ; if( kk < plint->option_count ) /* turn off */ XmToggleButtonSetState( plint->wid->opwid[kk]->toggle, False,True ) ; ndone++ ; /* count of how many we've done */ } /* end of while(1) loop over option lines */
char * IMREG_main( PLUGIN_interface * plint ) { MCW_idcode * idc ; /* input dataset idcode */ THD_3dim_dataset * old_dset , * new_dset ; /* input and output datasets */ char * new_prefix , * str ; /* strings from user */ int base , ntime , datum , nx,ny,nz , ii,kk , npix ; float dx,dy,dz ; MRI_IMARR * ims_in , * ims_out ; MRI_IMAGE * im , * imbase ; byte ** bptr = NULL , ** bout = NULL ; short ** sptr = NULL , ** sout = NULL ; float ** fptr = NULL , ** fout = NULL ; float * dxar = NULL , * dyar = NULL , * phiar = NULL ; /*--------------------------------------------------------------------*/ /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/ /*--------- go to first input line ---------*/ PLUTO_next_option(plint) ; idc = PLUTO_get_idcode(plint) ; /* get dataset item */ old_dset = PLUTO_find_dset(idc) ; /* get ptr to dataset */ if( old_dset == NULL ) return "*************************\n" "Cannot find Input Dataset\n" "*************************" ; ntime = DSET_NUM_TIMES(old_dset) ; if( ntime < 2 ) return "*****************************\n" "Dataset has only 1 time point\n" "*****************************" ; ii = DSET_NVALS_PER_TIME(old_dset) ; if( ii > 1 ) return "************************************\n" "Dataset has > 1 value per time point\n" "************************************" ; nx = old_dset->daxes->nxx ; dx = old_dset->daxes->xxdel ; ny = old_dset->daxes->nyy ; dy = old_dset->daxes->yydel ; npix = nx*ny ; nz = old_dset->daxes->nzz ; dz = old_dset->daxes->zzdel ; if( nx != ny || fabs(dx) != fabs(dy) ) { #ifdef IMREG_DEBUG fprintf(stderr,"\nIMREG: nx=%d ny=%d nz=%d dx=%f dy=%f dz=%f\n", nx,ny,nz,dx,dy,dz ) ; #endif return "***********************************\n" "Dataset does not have square slices\n" "***********************************" ; } new_prefix = PLUTO_get_string(plint) ; /* get string item (the output prefix) */ if( ! PLUTO_prefix_ok(new_prefix) ) /* check if it is OK */ return "************************\n" "Output Prefix is illegal\n" "************************" ; /*--------- go to next input line ---------*/ PLUTO_next_option(plint) ; base = PLUTO_get_number(plint) ; if( base >= ntime ) return "********************\n" "Base value too large\n" "********************" ; /*--------- see if the 3rd option line is present --------*/ str = PLUTO_get_optiontag( plint ) ; if( str != NULL ) { float fsig , fdxy , fdph ; fsig = PLUTO_get_number(plint) * 0.42466090 ; fdxy = PLUTO_get_number(plint) ; fdph = PLUTO_get_number(plint) ; mri_align_params( 0 , 0.0,0.0,0.0 , fsig,fdxy,fdph ) ; /* fprintf(stderr,"Set fine params = %f %f %f\n",fsig,fdxy,fdph) ; */ } /*------------- ready to compute new dataset -----------*/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: loading dataset\n") ; #endif DSET_load( old_dset ) ; /*** 1) Copy the dataset in toto ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: Copying dataset\n") ; #endif new_dset = PLUTO_copy_dset( old_dset , new_prefix ) ; if( new_dset == NULL ) return "****************************\n" "Failed to copy input dataset\n" "****************************" ; /*** 2) Make an array of empty images ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: making empty images\n") ; #endif datum = DSET_BRICK_TYPE(new_dset,0) ; INIT_IMARR(ims_in) ; for( ii=0 ; ii < ntime ; ii++ ) { im = mri_new_vol_empty( nx , ny , 1 , datum ) ; ADDTO_IMARR(ims_in,im) ; } imbase = mri_new_vol_empty( nx , ny , 1 , datum ) ; dxar = (float *) malloc( sizeof(float) * ntime ) ; dyar = (float *) malloc( sizeof(float) * ntime ) ; phiar = (float *) malloc( sizeof(float) * ntime ) ; /*** 3) Get pointers to sub-bricks in old and new datasets ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: getting input brick pointers\n") ; #endif switch( datum ) { /* pointer type depends on input datum type */ case MRI_byte: bptr = (byte **) malloc( sizeof(byte *) * ntime ) ; bout = (byte **) malloc( sizeof(byte *) * ntime ) ; for( ii=0 ; ii < ntime ; ii++ ) { bptr[ii] = (byte *) DSET_ARRAY(old_dset,ii) ; bout[ii] = (byte *) DSET_ARRAY(new_dset,ii) ; } break ; case MRI_short: sptr = (short **) malloc( sizeof(short *) * ntime ) ; sout = (short **) malloc( sizeof(short *) * ntime ) ; for( ii=0 ; ii < ntime ; ii++ ) { sptr[ii] = (short *) DSET_ARRAY(old_dset,ii) ; sout[ii] = (short *) DSET_ARRAY(new_dset,ii) ; } #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: sptr[0] = %p sout[0] = %p\n",sptr[0],sout[0]) ; #endif break ; case MRI_float: fptr = (float **) malloc( sizeof(float *) * ntime ) ; fout = (float **) malloc( sizeof(float *) * ntime ) ; for( ii=0 ; ii < ntime ; ii++ ) { fptr[ii] = (float *) DSET_ARRAY(old_dset,ii) ; fout[ii] = (float *) DSET_ARRAY(new_dset,ii) ; } break ; } /*** 4) Loop over slices ***/ PLUTO_popup_meter(plint) ; for( kk=0 ; kk < nz ; kk++ ) { /*** 4a) Setup ims_in images to point to input slices ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: slice %d -- setup input images\n",kk) ; #endif for( ii=0 ; ii < ntime ; ii++ ) { im = IMARR_SUBIMAGE(ims_in,ii) ; switch( datum ) { case MRI_byte: mri_fix_data_pointer( bptr[ii] + kk*npix, im ) ; break ; case MRI_short: mri_fix_data_pointer( sptr[ii] + kk*npix, im ) ; break ; case MRI_float: mri_fix_data_pointer( fptr[ii] + kk*npix, im ) ; break ; } } /*** 4b) Setup im to point to base image ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: slice %d -- setup base image\n",kk) ; #endif switch( datum ) { case MRI_byte: mri_fix_data_pointer( bptr[base] + kk*npix, imbase ) ; break ; case MRI_short: mri_fix_data_pointer( sptr[base] + kk*npix, imbase ) ; break ; case MRI_float: mri_fix_data_pointer( fptr[base] + kk*npix, imbase ) ; break ; } /*** 4c) Register this slice at all times ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: slice %d -- register\n",kk) ; #endif ims_out = mri_align_dfspace( imbase , NULL , ims_in , ALIGN_REGISTER_CODE , dxar,dyar,phiar ) ; if( ims_out == NULL ) fprintf(stderr,"IMREG: mri_align_dfspace return NULL\n") ; /*** 4d) Put the output back in on top of the input; note that the output is always in MRI_float format ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: slice %d -- put output back into dataset\n",kk) ; #endif for( ii=0 ; ii < ntime ; ii++ ) { switch( datum ) { case MRI_byte: im = mri_to_mri( MRI_byte , IMARR_SUBIMAGE(ims_out,ii) ) ; memcpy( bout[ii] + kk*npix , MRI_BYTE_PTR(im) , sizeof(byte)*npix ) ; mri_free(im) ; break ; case MRI_short: #ifdef IMREG_DEBUG if( ii==0 )fprintf(stderr,"IMREG: conversion to short at ii=%d\n",ii) ; #endif im = mri_to_mri( MRI_short , IMARR_SUBIMAGE(ims_out,ii) ) ; #ifdef IMREG_DEBUG if( ii==0 )fprintf(stderr,"IMREG: copying to %p from %p\n",sout[ii] + kk*npix,MRI_SHORT_PTR(im)) ; #endif memcpy( sout[ii] + kk*npix , MRI_SHORT_PTR(im) , sizeof(short)*npix ) ; #ifdef IMREG_DEBUG if( ii==0 )fprintf(stderr,"IMREG: freeing\n") ; #endif mri_free(im) ; break ; case MRI_float: im = IMARR_SUBIMAGE(ims_out,ii) ; memcpy( fout[ii] + kk*npix , MRI_FLOAT_PTR(im) , sizeof(float)*npix ) ; break ; } } PLUTO_set_meter(plint, (100*(kk+1))/nz ) ; /*** 4e) Destroy the output images ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: destroying aligned output\n") ; #endif DESTROY_IMARR( ims_out ) ; } /*** 5) Destroy the empty images and other workspaces ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: destroy workspaces\n") ; #endif mri_clear_data_pointer(imbase) ; mri_free(imbase) ; for( ii=0 ; ii < ntime ; ii++ ) { im = IMARR_SUBIMAGE(ims_in,ii) ; mri_clear_data_pointer(im) ; } DESTROY_IMARR(ims_in) ; FREE_WORKSPACE ; /*------------- let AFNI know about the new dataset ------------*/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: send result to AFNI\n") ; #endif PLUTO_add_dset( plint , new_dset , DSET_ACTION_MAKE_CURRENT ) ; return NULL ; /* null string returned means all was OK */ }