00001 
00002 
00003 
00004 
00005 
00006    
00007 #include "afni.h"
00008 
00009 #ifndef ALLOW_PLUGINS
00010 #  error "Plugins not properly set up -- see machdep.h"
00011 #endif
00012 
00013 
00014 
00015 
00016 
00017 static char * COPY_main( PLUGIN_interface * ) ;
00018 
00019 static char helpstring[] =
00020    " Purpose: Creating a copy of a dataset.\n"
00021    " Inputs:\n"
00022    " Dataset = A dataset in the current session that exists in memory\n"
00023    "               (not warp-on-demand).\n"
00024    " Prefix  = Filename prefix to be used for the output dataset.\n"
00025    " Fill    = How to fill voxel data in new dataset:\n"
00026    "             Data [All] = copy all sub-bricks from input\n"
00027    "             Zero [All] = fill all sub-bricks with zero\n"
00028    "             Zero [One] = make new dataset have only 1 sub-brick,\n"
00029    "                          and fill with zero -- this is useful for\n"
00030    "                          creating mask datasets using the\n"
00031    "                          'Draw Dataset' plugin.\n"
00032    " Type    = Lets you change the 'type' of the output dataset, for\n"
00033    "             example from anat to func.\n"
00034    " Datum   = Lets you set the data type of the new brick.  This will\n"
00035    "             only work when using \"Zero [All]\" or \"Zero [One]\"\n"
00036    "             Fill modes.\n"
00037    "Author -- RWCox"
00038 ;
00039 
00040 #define NFILL 3
00041 static char * fill_options[NFILL] = { "Data [All]" , "Zero [All]" , "Zero [One]" } ;
00042 
00043 #define NDTYPE 4
00044 static char * dtype_options[NDTYPE] = {
00045   "byte" , "short" , "float" , "complex" } ;
00046 static int    dtype_kinds[NDTYPE] = {
00047   MRI_byte , MRI_short , MRI_float , MRI_complex } ;
00048 
00049 
00050 
00051 
00052 
00053 
00054 DEFINE_PLUGIN_PROTOTYPE
00055 
00056 PLUGIN_interface * PLUGIN_init( int ncall )
00057 {
00058    PLUGIN_interface * plint ;
00059 
00060    if( ncall > 0 ) return NULL ;  
00061 
00062    
00063 
00064    plint = PLUTO_new_interface( "Dataset Copy" , "Make a Copy of a Dataset" , helpstring ,
00065                                  PLUGIN_CALL_VIA_MENU , COPY_main  ) ;
00066 
00067    PLUTO_add_hint( plint , "Make a Copy of a Dataset" ) ;
00068 
00069    PLUTO_set_sequence( plint , "A:newdset:copy" ) ;
00070 
00071    PLUTO_set_runlabels( plint , "Copy+Keep" , "Copy+Close" ) ;  
00072 
00073    
00074 
00075    PLUTO_add_option( plint , "Input" , "Input" , TRUE ) ;
00076    PLUTO_add_dataset(plint , "Dataset" ,
00077                                     ANAT_ALL_MASK , FUNC_ALL_MASK ,
00078                                     DIMEN_ALL_MASK | BRICK_ALLTYPE_MASK ) ;
00079 
00080    
00081 
00082    PLUTO_add_option( plint , "Output" , "Output" , TRUE ) ;
00083    PLUTO_add_string( plint , "Prefix" , 0,NULL , 19 ) ;
00084 
00085    
00086 
00087    PLUTO_add_option( plint , "Data Fill" , "Data Fill" , FALSE ) ;
00088    PLUTO_add_string( plint , "Method" ,  NFILL,fill_options , 0 ) ;
00089 
00090    
00091 
00092    PLUTO_add_option( plint , "Dataset" , "Dataset" , FALSE ) ;
00093    PLUTO_add_string( plint , "Type" , NUM_DSET_TYPES,DSET_prefixstr , 0 ) ;
00094 
00095    
00096 
00097    PLUTO_add_option( plint , "Datum" , "Datum" , FALSE ) ;
00098    PLUTO_add_string( plint , "Datum" , NDTYPE,dtype_options, 2 ) ;
00099 
00100    return plint ;
00101 }
00102 
00103 
00104 
00105 
00106 
00107 static char * COPY_main( PLUGIN_interface * plint )
00108 {
00109    char * tag , * new_prefix , * cpt ;
00110    MCW_idcode * idc ;
00111    THD_3dim_dataset * dset , * new_dset ;
00112    int ival , zfill=0 , ftyp=-1 , dtyp=-1, type_index=-1, data_type=-1 ;
00113 
00114    
00115    
00116 
00117    if( plint == NULL )
00118       return "**********************\n"
00119              "COPY_main:  NULL input\n"
00120              "**********************"  ;
00121 
00122    PLUTO_next_option(plint) ;
00123    idc  = PLUTO_get_idcode(plint) ;
00124    dset = PLUTO_find_dset(idc) ;
00125    if( dset == NULL )
00126       return "*****************************\n"
00127              "COPY_main:  bad input dataset\n"
00128              "*****************************"  ;
00129 
00130    dtyp = dset->type ;
00131 
00132    PLUTO_next_option(plint) ;
00133    new_prefix = PLUTO_get_string(plint) ;
00134    if( ! PLUTO_prefix_ok(new_prefix) )
00135       return "**********************\n"
00136              "COPY_main:  bad prefix\n"
00137              "**********************"  ;
00138 
00139    tag = PLUTO_get_optiontag(plint) ;
00140    while( tag != NULL ){
00141 
00142       if( strcmp(tag,"Data Fill") == 0 ){
00143          cpt   = PLUTO_get_string(plint) ;
00144          if( cpt != NULL )
00145             zfill = PLUTO_string_index( cpt , NFILL , fill_options ) ;
00146       }
00147 
00148       else if( strcmp(tag,"Dataset") == 0 ){
00149          cpt  = PLUTO_get_string(plint) ;
00150          ftyp = PLUTO_string_index( cpt , NUM_DSET_TYPES,DSET_prefixstr ) ;
00151          if( ftyp >= 0 ){
00152             if( ftyp <= LAST_FUNC_TYPE ){
00153                dtyp = HEAD_FUNC_TYPE ;
00154             } else {
00155                ftyp -= (LAST_FUNC_TYPE+1) ;  
00156                dtyp  = HEAD_ANAT_TYPE ;
00157             }
00158          }
00159       }
00160 
00161       else if( strcmp(tag, "Datum") == 0 ){
00162          cpt  = PLUTO_get_string(plint) ;
00163          type_index = PLUTO_string_index( cpt, NDTYPE, dtype_options ) ;
00164          if ( (type_index >= 0) && (type_index < NDTYPE) )
00165             data_type = dtype_kinds[type_index] ;
00166       }
00167 
00168       tag = PLUTO_get_optiontag(plint) ;
00169    }
00170 
00171    
00172    
00173 
00174    
00175 
00176    if( zfill == 0 ){
00177       new_dset = PLUTO_copy_dset( dset , new_prefix ) ;
00178    } else {
00179       new_dset = EDIT_empty_copy( dset ) ;
00180 
00181       if( ISFUNCTYPE(dtyp) && ( zfill == 2 ) )
00182          ftyp = FUNC_FIM_TYPE ;  
00183    }
00184 
00185    if( new_dset == NULL )
00186       return  "****************************************\n"
00187               "COPY_main:  failed to copy input dataset\n"
00188               "****************************************"  ;
00189 
00190    DSET_unload( dset ) ;  
00191 
00192    
00193 
00194    if( ftyp >= 0 ) EDIT_dset_items( new_dset ,
00195                                        ADN_type      , dtyp ,
00196                                        ADN_func_type , ftyp ,
00197                                     ADN_none ) ;
00198 
00199    
00200 
00201    if ( (data_type >= 0) )
00202    {
00203       if ( zfill )
00204          EDIT_dset_items( new_dset ,
00205                              ADN_datum_all, data_type,
00206                           ADN_none ) ;
00207       else{
00208          DSET_delete(new_dset) ;
00209 
00210          return  "****************************************************\n"
00211                  "COPY_main:  Cannot change type of non-zeroed dataset\n"
00212                  "****************************************************"  ;
00213       }
00214    }
00215 
00216    
00217 
00218    if( zfill ) {
00219       int ityp , nbytes , nvals , ival ;
00220       void * new_brick , * bp ;
00221 
00222       EDIT_dset_items( new_dset ,
00223                           ADN_prefix , new_prefix ,
00224                           ADN_label1 , new_prefix ,
00225                        ADN_none ) ;
00226 
00227       if ( zfill == 2 ) { 
00228          EDIT_dset_items( new_dset ,
00229                              ADN_nvals  , 1 ,
00230                              ADN_ntt    , 0 ,
00231                           ADN_none ) ;
00232       }
00233 
00234       nvals = DSET_NVALS(new_dset) ;
00235 
00236       for ( ival = 0 ; ival < nvals ; ival++)        
00237       {                                              
00238          ityp      = DSET_BRICK_TYPE(new_dset,ival) ;
00239          nbytes    = DSET_BRICK_BYTES(new_dset,ival) ;   
00240          new_brick = malloc( nbytes ) ;
00241          EDIT_substitute_brick( new_dset , ival , ityp , new_brick ) ;
00242 
00243          bp     = DSET_BRICK_ARRAY(new_dset,ival) ;  
00244          EDIT_BRICK_FACTOR(new_dset,ival,0.0) ;      
00245          memset( bp , 0 , nbytes ) ;
00246       }
00247    }
00248 
00249    { char *his ;
00250      tross_Copy_History( dset , new_dset ) ;
00251      his = PLUTO_commandstring( plint ) ;
00252      tross_Append_History( new_dset , his ) ; free(his) ;
00253    }
00254 
00255    ival = PLUTO_add_dset( plint , new_dset , DSET_ACTION_MAKE_CURRENT ) ;
00256 
00257    if( ival ){
00258       THD_delete_3dim_dataset( new_dset , False ) ;
00259       return "**********************************************\n"
00260              "COPY_main:  failure to add new dataset to AFNI\n"
00261              "**********************************************" ;
00262    }
00263 
00264    
00265 
00266    return NULL ;
00267 }