1 /*****************************************************************************
2    Major portions of this software are copyrighted by the Medical College
3    of Wisconsin, 1994-2000, and are released under the Gnu General Public
4    License, Version 2.  See the file README.Copyright for details.
5 ******************************************************************************/
6 
7 #include "afni.h"
8 
9 #ifndef ALLOW_PLUGINS
10 #  error "Plugins not properly set up -- see machdep.h"
11 #endif
12 
13 /***********************************************************************
14   Simple plugin to copy a dataset and make a new one with zeropadding
15 ************************************************************************/
16 
17 char * ZPAD_main( PLUGIN_interface * ) ;
18 
19 static char helpstring[] =
20    " Purpose: Creating a zero-padded copy of a dataset [like 3dZeropad].\n"
21    " Inputs:\n"
22    " Dataset = A dataset in the current session that exists in memory\n"
23    "             (not warp-on-demand).\n"
24    " Prefix  = Filename prefix to be used for the output dataset.\n"
25    " Type    = Lets you change the 'type' of the output dataset, for\n"
26    "             example from anat to func.\n"
27    " Padding = I, S, A, P, L, R = number of planes to add (or subtract)\n"
28    "           at the Inferior, Superior, Anterior, Posterior, Left,\n"
29    "           and Right edges, respectively.\n"
30    "\n"
31    "Author -- RWCox - Oct 2000"
32 ;
33 
34 /***********************************************************************
35    Set up the interface to the user
36 ************************************************************************/
37 
38 
39 DEFINE_PLUGIN_PROTOTYPE
40 
PLUGIN_init(int ncall)41 PLUGIN_interface * PLUGIN_init( int ncall )
42 {
43    PLUGIN_interface * plint ;
44 
45    if( ncall > 0 ) return NULL ;  /* only one interface */
46 
47    CHECK_IF_ALLOWED("DSETZEROPAD","Dset Zeropad") ;  /* 30 Sep 2016 */
48 
49    /*-- set titles and call point --*/
50 
51    plint = PLUTO_new_interface( "Dset Zeropad" ,
52                                 "Make a Zero-Padded Copy of a Dataset" ,
53                                 helpstring ,
54                                 PLUGIN_CALL_VIA_MENU , ZPAD_main  ) ;
55 
56    PLUTO_add_hint( plint , "Copy and Zero-Pad a Dataset" ) ;
57 
58    PLUTO_set_sequence( plint , "A:newdset:copy" ) ;
59 
60    PLUTO_set_runlabels( plint , "Copy+Keep" , "Copy+Close" ) ;  /* 04 Nov 2003 */
61 
62    /*-- first line of input: Dataset --*/
63 
64    PLUTO_add_option( plint , "Input" , "Input" , TRUE ) ;
65    PLUTO_add_dataset(plint , "Dataset" ,
66                                     ANAT_ALL_MASK , FUNC_ALL_MASK ,
67                                     DIMEN_ALL_MASK | BRICK_ALLTYPE_MASK ) ;
68 
69    /*-- second line of input: Prefix for output dataset --*/
70 
71    PLUTO_add_option( plint , "Output" , "Output" , TRUE ) ;
72    PLUTO_add_string( plint , "Prefix" , 0,NULL , 19 ) ;
73 
74    /*-- 3rd line of input: padding --*/
75 
76    PLUTO_add_option( plint , "Padding" , "Padding" , TRUE ) ;
77    PLUTO_add_number( plint , "I" , -19 , 19 , 0 , 0 , FALSE ) ;
78    PLUTO_add_number( plint , "S" , -19 , 19 , 0 , 0 , FALSE ) ;
79    PLUTO_add_number( plint , "A" , -19 , 19 , 0 , 0 , FALSE ) ;
80    PLUTO_add_number( plint , "P" , -19 , 19 , 0 , 0 , FALSE ) ;
81    PLUTO_add_number( plint , "L" , -19 , 19 , 0 , 0 , FALSE ) ;
82    PLUTO_add_number( plint , "R" , -19 , 19 , 0 , 0 , FALSE ) ;
83 
84    /*-- 4th line of input: Type option --*/
85 
86    PLUTO_add_option( plint , "Dataset" , "Dataset" , FALSE ) ;
87    PLUTO_add_string( plint , "Type" , NUM_DSET_TYPES,DSET_prefixstr , 0 ) ;
88 
89    return plint ;
90 }
91 
92 /***************************************************************************
93   Main routine for this plugin (will be called from AFNI).
94 ****************************************************************************/
95 
ZPAD_main(PLUGIN_interface * plint)96 char * ZPAD_main( PLUGIN_interface * plint )
97 {
98    char * tag , * new_prefix , * cpt ;
99    MCW_idcode * idc ;
100    THD_3dim_dataset * dset , * new_dset ;
101    int ftyp=-1 , dtyp=-1 , ival ;
102    int add_I, add_S, add_A, add_P, add_L, add_R;
103 
104    /*--------------------------------------------------------------------*/
105    /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/
106 
107    if( plint == NULL )
108       return "**********************\n"
109              "ZPAD_main:  NULL input\n"
110              "**********************"  ;
111 
112    PLUTO_next_option(plint) ;
113    idc  = PLUTO_get_idcode(plint) ;
114    dset = PLUTO_find_dset(idc) ;
115    if( dset == NULL )
116       return "*****************************\n"
117              "ZPAD_main:  bad input dataset\n"
118              "*****************************"  ;
119 
120    dtyp = dset->type ;
121 
122    PLUTO_next_option(plint) ;
123    new_prefix = PLUTO_get_string(plint) ;
124    if( ! PLUTO_prefix_ok(new_prefix) )
125       return "**********************\n"
126              "ZPAD_main:  bad prefix\n"
127              "**********************"  ;
128 
129    PLUTO_next_option(plint) ;
130    add_I = PLUTO_get_number(plint) ;
131    add_S = PLUTO_get_number(plint) ;
132    add_A = PLUTO_get_number(plint) ;
133    add_P = PLUTO_get_number(plint) ;
134    add_L = PLUTO_get_number(plint) ;
135    add_R = PLUTO_get_number(plint) ;
136    if( add_I==0 && add_S==0 && add_P==0 && add_A==0 && add_L==0 && add_R==0 )
137       return "***********************\n"
138              "ZPAD_main: no padding?!\n"
139              "***********************"  ;
140 
141    tag = PLUTO_get_optiontag(plint) ;
142    while( tag != NULL ){
143 
144       if( strcmp(tag,"Dataset") == 0 ){
145          cpt  = PLUTO_get_string(plint) ;
146          ftyp = PLUTO_string_index( cpt , NUM_DSET_TYPES,DSET_prefixstr ) ;
147          if( ftyp >= 0 ){
148             if( ftyp <= LAST_FUNC_TYPE ){
149                dtyp = HEAD_FUNC_TYPE ;
150             } else {
151                ftyp -= (LAST_FUNC_TYPE+1) ;  /* 14 Jul 1998 */
152                dtyp  = HEAD_ANAT_TYPE ;
153             }
154          }
155       }
156 
157       tag = PLUTO_get_optiontag(plint) ;
158    }
159 
160    /*------------------------------------------------------*/
161    /*---------- At this point, the inputs are OK ----------*/
162 
163    /*-- make a new dataset --*/
164 
165    new_dset = THD_zeropad( dset ,
166                            add_I, add_S, add_A, add_P, add_L, add_R,
167                            new_prefix , ZPAD_PURGE ) ;
168 
169    if( new_dset == NULL )
170       return  "****************************************\n"
171               "ZPAD_main:  failed to create new dataset\n"
172               "****************************************"  ;
173 
174    DSET_unload( dset ) ;  /* unload old one from memory */
175 
176    /*--- modify dataset, if desired ---*/
177 
178    if( ftyp >= 0 ) EDIT_dset_items( new_dset ,
179                                        ADN_type      , dtyp ,
180                                        ADN_func_type , ftyp ,
181                                     ADN_none ) ;
182 
183    ival = PLUTO_add_dset( plint , new_dset , DSET_ACTION_MAKE_CURRENT ) ;
184 
185    if( ival ){
186       DSET_delete(new_dset) ;
187       return "**********************************************\n"
188              "ZPAD_main:  failure to add new dataset to AFNI\n"
189              "**********************************************" ;
190    }
191 
192    /*-- done successfully!!! --*/
193 
194    return NULL ;
195 }
196