1
2 /* ----------------------------------------------------------------------
3 * Just a very basic example program for reading and writing AFNI datasets.
4 * Output dataset is input * 2.
5 *
6 * usage: 3dexample -input INSET+orig -prefix OUTPUT
7 *
8 * compile example:
9 *
10 * gcc -DREAD_WRITE_64 -o 3dexample1 3dexample1.c \
11 * -I$HOME/abin -L$HOME/abin -lmri -lf2c -lXt -lz -lexpat -lm
12 *
13 * Author: R Reynolds 19 Sep, 2014
14 */
15
16 #include "mrilib.h"
17
18 int compute_output_dset(THD_3dim_dataset ** dout, THD_3dim_dataset * din,
19 char * oname);
20 int open_input_dset(THD_3dim_dataset ** din, char * fname);
21 int process_options(int argc, char *argv[], char ** iname, char ** oname);
22 int show_help(void);
23
24 /*--------------- main routine ---------------*/
main(int argc,char * argv[])25 int main( int argc, char *argv[] )
26 {
27 THD_3dim_dataset * din, * dout;
28 char * inname, * outname;
29
30 /* process options: a negative return is considered an error */
31 if( process_options(argc, argv, &inname, &outname) ) return 1;
32
33 if( open_input_dset(&din, inname) ) return 1;
34
35 if( compute_output_dset(&dout, din, outname) ) return 1;
36
37 DSET_write(dout);
38
39 return 0; /* success */
40 }
41
compute_output_dset(THD_3dim_dataset ** dout,THD_3dim_dataset * din,char * oname)42 int compute_output_dset(THD_3dim_dataset ** dout, THD_3dim_dataset * din,
43 char * oname)
44 {
45 float * indata, * outdata;
46 int index, nxyz;
47
48 /* create output data via malloc, need number of voxels */
49 nxyz = DSET_NVOX(din);
50 outdata = (float *)malloc(nxyz * sizeof(float));
51 if( ! outdata ) {
52 fprintf(stderr,"** failed to alloc %d voxels for output\n", nxyz);
53 return 1;
54 }
55
56 /* note input data pointer, from sub-brick #0 of input dataset struct */
57 indata = DSET_ARRAY(din, 0);
58
59 /* do the work: just double the values */
60 for( index = 0; index < nxyz; index++ )
61 outdata[index] = 2.0 * indata[index];
62
63 /* create empty dataset, set output name, attach data (at volume 0) */
64 *dout = EDIT_empty_copy(din);
65 EDIT_dset_items(*dout, ADN_prefix, oname, ADN_none);
66 EDIT_substitute_brick(*dout, 0, MRI_float, outdata);
67
68 return 0;
69 }
70
open_input_dset(THD_3dim_dataset ** din,char * fname)71 int open_input_dset(THD_3dim_dataset ** din, char * fname)
72 {
73 *din = THD_open_dataset(fname);
74 if( ! *din ) {
75 fprintf(stderr,"** failed to read input dataset '%s'\n", fname);
76 return 1;
77 }
78
79 /* refuse to work with anything but float here */
80 if( DSET_BRICK_TYPE(*din, 0) != MRI_float ) {
81 fprintf(stderr,"** input must be of type float, failing...\n");
82 return 1;
83 }
84
85 /* data is not automatically read in, do it now */
86 DSET_load(*din);
87
88 return 0;
89 }
90
91
92 /* return 0 on success, else error */
process_options(int argc,char * argv[],char ** iname,char ** oname)93 int process_options(int argc, char *argv[], char ** iname, char ** oname)
94 {
95 /* be very simple, require the exact parameter set */
96 if( argc != 5 || strcmp(argv[1], "-input") || strcmp(argv[3], "-prefix") ) {
97 show_help();
98 return 1; /* error */
99 }
100
101 *iname = argv[2];
102 *oname = argv[4];
103
104 return 0;
105 }
106
show_help(void)107 int show_help(void)
108 {
109 printf("This is a demo AFNI program that multiplies a dataset by 2.\n"
110 "See 3dToyProg.c for a more detailed example.\n\n"
111 "usage: 3dexample1 -input INSET -prefix PREFIX\n\n");
112 return 0;
113 }
114
115