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 #ifndef _MAXIMA_H_
8 #define _MAXIMA_H_
9 
10 #ifdef  __cplusplus
11 extern "C" {
12 #endif
13 
14 /*  AFNI stuff  */
15 
16 /*--------------------  defines    ----------------------------------------*/
17 
18 #define R_MAX_AFNI_DSETS          3
19 #define R_MESSAGE_L             256
20 #define R_FILE_L                512
21 
22 #define rWARNING( string ) fprintf( stderr, "\033[1m%s\033[0m\n", string )
23 #define rERROR(   string ) fprintf( stderr, "\007\033[2m%s\033[0m\n", string )
24 
25 
26 /*--------------------  typedefs   ----------------------------------------*/
27 
28 typedef struct
29 {
30     /* set before reading afni info */
31     int                 must_be_short;               /* must input be short   */
32     int                 want_floats;                 /* create fimage ?       */
33     int                 subs_must_equal;             /* require same number   */
34     int                 max_subs;                    /* max number of subs    */
35 
36     /* basic afni stuff */
37     THD_3dim_dataset  * dset    [ R_MAX_AFNI_DSETS ];
38     short             * simage  [ R_MAX_AFNI_DSETS ];
39     float               factor  [ R_MAX_AFNI_DSETS ];
40     int                 subs    [ R_MAX_AFNI_DSETS ]; /* number of subbricks  */
41     int                 sub_brick;                    /* user chosen index    */
42 
43     int                 nx, ny, nz, nvox;
44 
45     /* other */
46     float             * fimage  [ R_MAX_AFNI_DSETS ]; /* shorts with factor   */
47 
48     u_short             max_u_short;                  /* no factor considered */
49     int                 num_dsets;                    /* number read in       */
50 } r_afni_s;
51 
52 
53 /*--------------------  globals    ----------------------------------------*/
54 
55 extern char grMessage [ R_MESSAGE_L ];          /* global message string */
56 
57 
58 
59 
60 /*-- maxima stuff ---*/
61 
62 
63 #define MAX_MASK_FILL_VAL	1
64 
65 #define MAX_SORT_N_REMOVE_STYLE	1
66 #define MAX_WEIGHTED_AVE_STYLE	2
67 #define MAX_MAX_STYLE		2
68 
69 typedef struct
70 {
71     int   * plist;
72     int     used;
73     int     M;
74 } point_list_s;
75 
76 
77 typedef struct
78 {
79     THD_3dim_dataset  * dset;			/* input dset 		      */
80     short             * sdata;			/* short data from dset       */
81     short             * result;			/* requires own M 	      */
82     int			nx, ny, nz, nxy, nvox;
83 
84     point_list_s        P;			/* point list in result       */
85 
86     int			extrema_count;		/* number of extrema 	      */
87 
88     int			data_type;		/* MRI_short, etc	      */
89     int			adn_type;		/* HEAD_ANAT/FUNC 	      */
90     int			func_type;		/* FUNC_FIM_TYPE? 	      */
91 
92     char 		outfile[ R_FILE_L ];	/* output prefix 	      */
93     int 		sval_style;             /* sphere vals: 1, 1toN, Nto1 */
94 
95     float		cutoff;		        /* acceptable value cutoff    */
96     float		min_dist;	        /* distance between extrema   */
97     float		out_rad;	        /* mask radius around extrema */
98 
99     int			negatives;		/* find negative extrema      */
100     int			ngbr_style;		/* what to do with neighbors  */
101     int			overwrite;		/* do we allow overwrite      */
102     int			quiet;			/* no text output of results  */
103     int			coords_only;		/* only output coordinates    */
104     int			true_max;		/* no neighbor equality       */
105     int			dicom_coords;		/* no neighbor equality       */
106     int			debug;  		/* debug level                */
107 } maxima_s;
108 
109 
110 static int	add_point_to_list  ( point_list_s *, int );
111 static int	apply_fill_radius  ( maxima_s * );
112 static int	apply_min_dist     ( maxima_s * );
113 static int	clear_around_point ( int, maxima_s *, point_list_s * );
114 static int	create_point_list  ( maxima_s * );
115 static int	find_local_maxima  ( maxima_s * );
116 static int	radial_fill	   ( int, int, int, maxima_s *, int );
117 static int	weighted_index     ( point_list_s *, maxima_s * );
118 static int	write_results      ( r_afni_s *, maxima_s *,PLUGIN_interface *);
119 
120 static void	show_point_list_s  ( char * mesg, point_list_s *, int );
121 
122 int 		point_comp_neg     ( const void *, const void * );
123 int 		point_comp_pos     ( const void *, const void * );
124 
125 
126 int  disp_r_afni_s         ( char * mesg, r_afni_s * A );
127 int  disp_str_list         ( char * list[], int len );
128 int  display_coords        ( r_afni_s *, maxima_s * );
129 void free_memory           ( r_afni_s *, maxima_s * );
130 int  init_afni_s	   ( r_afni_s * );
131 int  init_maxima_s	   ( maxima_s *, r_afni_s *, char * );
132 int  process_data          ( maxima_s * );
133 int  r_set_afni_s_from_dset( r_afni_s *, THD_3dim_dataset *, int debug );
134 int  set_results           ( r_afni_s *, maxima_s *, THD_3dim_dataset * );
135 void show_maxima_s         ( char * mesg, maxima_s * );
136 
137 u_short r_get_max_u_short( ushort *, int );
138 
139 /* global var for comparison function use */
140 static short *  gr_orig_data = NULL;
141 static float    gr_fac = 1.0;
142 
143 #ifdef  __cplusplus
144 }
145 #endif
146 
147 #endif /* _MAXIMA_H_ */
148