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 _MCW_EDITVOL_
8 #define _MCW_EDITVOL_
9 
10 #undef CLUST_DEBUG
11 
12 #ifdef SPARKY
13 #undef _POSIX_SOURCE
14 #endif
15 
16 #include <sys/types.h>      /* to fix a bug in gcc */
17 #include <stddef.h>
18 /** #include <stdarg.h> **/        /* for variable number of arguments processing */
19 
20 #include "replaceXt.h"  /* 09 Nov 2018 */
21 
22 #include "mrilib.h"
23 #include "afni_warp.h"
24 
25 #define INC_CLUSTER 32
26 
27 /*! In a cluster struct, the (i,j,k) indexes for each voxel
28     are stored in a single integer ijk (to save space).
29     The macros below translate between (i,j,k) [THREE] and ijk. */
30 
31 #define IJK_TO_THREE(ijk,i,j,k,nx,nxy) \
32   ( (k) = (ijk)/(nxy) , (j)=((ijk)%(nxy))/(nx) , (i)=(ijk)%(nx) )
33 
34 /*! \see IJK_TO_THREE() */
35 
36 #define THREE_TO_IJK(i,j,k,nx,nxy) ((i)+(j)*(nx)+(k)*(nxy))
37 
38 /*! Struct to store a cluster.
39 
40     The cluster structure was modified to store the individual coordinate
41     indices for each voxel.  This avoids ambiguity in voxel identification.
42      \date BDW, 06 March 1997.
43 */
44 
45 typedef struct {
46    int num_pt  ;    /*!< Number of points in cluster */
47    int num_all ;    /*!< Number of points allocated for cluster */
48    short *i;        /*!< x index */
49    short *j;        /*!< y index */
50    short *k;        /*!< z index */
51    float *mag ;     /* stores value at each voxel in cluster */
52 } MCW_cluster ;
53 
54 /*! Initialize a MCW_cluster. */
55 
56 #define INIT_CLUSTER(cc)               \
57   ( (cc) = RwcNew(MCW_cluster) ,        \
58     (cc)->num_pt = (cc)->num_all = 0 , \
59     (cc)->i = NULL , (cc)->j = NULL , (cc)->k = NULL ,(cc)->mag = NULL )
60 
61 /*! Delete an MCW_cluster. */
62 
63 #define KILL_CLUSTER(cc)       \
64   do{ if( cc != NULL ){        \
65          myRwcFree((cc)->i) ;   \
66          myRwcFree((cc)->j) ;   \
67          myRwcFree((cc)->k) ;   \
68          myRwcFree((cc)->mag) ; \
69          myRwcFree((cc)) ;      \
70          (cc) = NULL ;         \
71       }} while(0)
72 
73 #ifdef CLUST_DEBUG
74 #  define DBMALL(n) printf(" -- Realloc-ing cluster: %d\n",(n))
75 #else
76 #  define DBMALL(n)
77 #endif
78 
79 /*! Duplicate an MCW_cluster */
80 
81 #define COPY_CLUSTER(dd,cc)                              \
82  do{ int nn ; INIT_CLUSTER(dd) ;                         \
83      (dd)->num_pt = (dd)->num_all = nn = (cc)->num_pt ;  \
84      (dd)->i   = (short *)RwcMalloc(sizeof(short)*nn);    \
85      (dd)->j   = (short *)RwcMalloc(sizeof(short)*nn);    \
86      (dd)->k   = (short *)RwcMalloc(sizeof(short)*nn);    \
87      (dd)->mag = (float *)RwcMalloc(sizeof(float)*nn);    \
88      memcpy((dd)->i  ,(cc)->i  ,sizeof(short)*nn);       \
89      memcpy((dd)->j  ,(cc)->j  ,sizeof(short)*nn);       \
90      memcpy((dd)->k  ,(cc)->k  ,sizeof(short)*nn);       \
91      memcpy((dd)->mag,(cc)->mag,sizeof(float)*nn);       \
92  } while(0)
93 
94 #define DUMP_CLUSTER(cc)                             \
95  do{ int cii;                                        \
96   printf("# points in cluster = %d\n",(cc)->num_pt); \
97   printf("     i   j   k  - mag\n");                 \
98   for(cii=0;cii<(cc)->num_pt;cii++)                  \
99       printf("    %d %d %d  - %f\n",                 \
100        (cc)->i[cii], (cc)->j[cii], (cc)->k[cii], (cc)->mag[cii]); \
101  } while(0)
102 
103 /*! Add point (ii,jj,kk) with magnitude mm to a MCW_cluster. */
104 
105 #define ADDTO_CLUSTER(cc,ii,jj,kk,m)                                        \
106   do{ int nn ;                                                              \
107       if( (cc)->num_pt == (cc)->num_all ){                                  \
108          (cc)->num_all = 2*(cc)->num_all + INC_CLUSTER ;                    \
109          nn = (cc)->num_all ;                                               \
110          (cc)->i=(short *)   RwcRealloc((char *)(cc)->i,sizeof(short)*nn  ); \
111          (cc)->j=(short *)   RwcRealloc((char *)(cc)->j,sizeof(short)*nn  ); \
112          (cc)->k=(short *)   RwcRealloc((char *)(cc)->k,sizeof(short)*nn  ); \
113          (cc)->mag=(float *) RwcRealloc((char *)(cc)->mag,sizeof(float)*nn); \
114          DBMALL(nn) ; }                                                     \
115       nn = (cc)->num_pt ; ((cc)->num_pt)++ ;                                \
116       (cc)->i[nn] = (ii) ; (cc)->j[nn] = (jj) ; (cc)->k[nn] = (kk) ;        \
117       (cc)->mag[nn] = (m) ; break ; } while(0)
118 
119 /*! Add point (ii,jj,kk) a MCW_cluster, don't save mag. */
120 
121 #define ADDTO_CLUSTER_NOMAG(cc,ii,jj,kk)                               \
122   do{ int nn ;                                                         \
123       if( (cc)->num_pt == (cc)->num_all ){                             \
124          (cc)->num_all = 2*(cc)->num_all + INC_CLUSTER ;               \
125          nn = (cc)->num_all ;                                          \
126          (cc)->i=(short *)RwcRealloc((char *)(cc)->i,sizeof(short)*nn); \
127          (cc)->j=(short *)RwcRealloc((char *)(cc)->j,sizeof(short)*nn); \
128          (cc)->k=(short *)RwcRealloc((char *)(cc)->k,sizeof(short)*nn); \
129       }                                                                \
130       nn = (cc)->num_pt ; ((cc)->num_pt)++ ;                           \
131       (cc)->i[nn] = (ii) ; (cc)->j[nn] = (jj) ; (cc)->k[nn] = (kk) ;   \
132    } while(0)
133 
134 #define ISOVALUE_MODE  1
135 #define ISOMERGE_MODE  2
136 
137 /*----------------------------------------------------------------------------*/
138 
139 /*! Struct to store a bunch of MCW_cluster stuff. */
140 
141 typedef struct {
142    int num_clu , num_all ;
143    MCW_cluster ** clar ;
144    int grid_nx, grid_ny, grid_nz;   /* ZSS March 02 2010 */
145 } MCW_cluster_array ;
146 
147 /*! Initialize a MCW_cluster_array. */
148 
149 #define INIT_CLARR(cl)                \
150   ( (cl) = RwcNew(MCW_cluster_array) , \
151     (cl)->num_clu = (cl)->num_all = 0 , (cl)->clar = NULL, \
152     (cl)->grid_nz = 0, (cl)->grid_ny = 0, (cl)->grid_nz = 0)
153 
154 /*! Add a MCW_cluster to a MCW_cluster_array. */
155 
156 #define ADDTO_CLARR(cl,cc)                                                     \
157   do{ int nn ;                                                                 \
158       if( (cl)->num_clu == (cl)->num_all ){                                    \
159          (cl)->num_all += INC_CLUSTER+(cl)->num_all/2 ; nn = (cl)->num_all ;   \
160          (cl)->clar = (MCW_cluster **) RwcRealloc( (char *)(cl)->clar ,         \
161                                                  sizeof(MCW_cluster *) * nn ); \
162       }                                                                        \
163       (cl)->clar[((cl)->num_clu)++] = (cc) ; break ; } while(0)
164 
165 /*! Delete a MCW_cluster_array (including all MCW_cluster inside). */
166 
167 #define DESTROY_CLARR(cl) \
168   do{ int ii ; if( cl != NULL ){                    \
169          for( ii=0 ; ii < (cl)->num_clu ; ii++ )    \
170             KILL_CLUSTER( (cl)->clar[ii] ) ;        \
171          myRwcFree((cl)->clar) ; (cl) = NULL ; \
172       } break ; } while(0)
173 
174 /*! Determine if 2 MCW_cluster are ordered. */
175 
176 #define CLUST_ORDERED(c1,c2) ( (c1)->num_pt >= (c2)->num_pt )
177 
178 /*! Swap 2 MCW_cluster. */
179 
180 #define CLUST_SWAP(c1,c2) (ct=(c1),(c1)=(c2),(c2)=ct,sss=1)
181 
182 /*! Bubble sort a MCW_cluster_array. */
183 
184 #define SORT_CLARR(name) \
185    if( (name) != NULL && (name)->num_clu > 1 ){                             \
186       int iic , jjc , sss ; MCW_cluster *ct ;                               \
187       for( iic=0 ; iic < (name)->num_clu ; iic++ ){                         \
188          sss = 0 ;                                                          \
189          for( jjc=1 ; jjc < (name)->num_clu ; jjc++ ){                      \
190             if( !CLUST_ORDERED( (name)->clar[jjc-1] , (name)->clar[jjc] ) ) \
191                CLUST_SWAP( (name)->clar[jjc-1] , (name)->clar[jjc] ) ;      \
192    } if( !sss ) break ; }}
193 
194 /*----------------------------------------------------------------------------*/
195 
196 #ifdef  __cplusplus
197 extern "C" {
198 #endif
199 
200 extern MCW_cluster_array * MCW_find_clusters( int,int,int , float,float,float ,
201                                               int , void * , float ) ;
202 
203  /* 30 Apr 2002 */
204 extern MCW_cluster_array * NIH_find_clusters( int,int,int , float,float,float ,
205                                               int , void * , float , int ) ;
206 
207 extern void MCW_cluster_to_vol( int,int,int, int,void *, MCW_cluster * ) ;
208 extern void MCW_vol_to_cluster( int,int,int, int,void *, MCW_cluster * ) ;
209 
210 extern void MCW_scale_to_max( int,int,int, int,void * );
211 
212 extern float MCW_vol_amax( int,int,int , int,void *) ;
213 
214 /* 11 Sept 1996 */
215 extern MCW_cluster * MCW_build_mask(float, float, float, float);
216 
217 extern MCW_cluster * MCW_spheremask( float,float,float,float ) ;
218 extern MCW_cluster * MCW_rectmask  ( float,float,float,float,float,float ) ;
219 extern MCW_cluster * MCW_rhddmask  ( float,float,float,float ) ;
220 extern MCW_cluster * MCW_tohdmask  ( float,float,float,float ) ;
221 void MCW_showmask (MCW_cluster *nbhd, char *opening, char *closing, FILE *fout);
222 
223 /* 16 June 1998 */
224 extern void MCW_erode_clusters (int, int, int, float, float, float, int,
225 				  void *, float, float, int);
226 
227 extern void MCW_sort_cluster( MCW_cluster * ) ; /* 10 Jul 2001 */
228 extern void MCW_radsort_cluster( MCW_cluster *, float, float, float ) ;
229 
230 /*----------------------------------------------------------------------------*/
231 #undef ALLOW_SCALE_TO_MAX
232 char * EDIT_options_help(void) ;  /* was a string, now a prototype */
233 /*----------------------------------------------------------------------------*/
234 
235 /*! Data structure filled in EDIT_check_argv,
236     and used to control EDIT_one_dataset (options applied in order given).
237     \see INIT_EDOPT()
238 */
239 
240 typedef struct EDIT_options {
241    int thtoin ;                   /*!< copy thresh data over intensity data */
242    int noneg ;                    /*!< throw away negative intensities      */
243    int abss ;                     /*!< take absolute values of intensities  */
244 
245    float clip_bot ;               /*!< zero out voxels with value in clip_bot..clip_top */
246    float clip_top ;               /*!< zero out voxels with value in clip_bot..clip_top */
247    int   clip_unscaled ;          /*!< clip without scaling? [09 Aug 1996]  */
248 
249    float thresh ;                 /*!< zero out if threshold < thresh     */
250    float thbot ;                  /*!< 26 Dec 2007 */
251    float clust_rmm ;              /*!< cluster data with rmm radius       */
252    float clust_vmul ;             /*!< remove clusters smaller than vmul  */
253    float blur ;                   /*!< Gaussian blur data with sigma = blur */
254    float thrblur ;                /*!< Gaussian blur threshold data,
255                                       with sigma = thrblur (4 Oct 1996)    */
256 
257    float erode_pv;                /*!< erosion percentage   (16 June 1998)  */
258    int dilate;                    /*!< dilation option      (16 June 1998)  */
259 
260 
261    int edit_clust;                /*!< edit cluster option  (10 Sept 1996)  */
262 
263    float filter_rmm;              /*!< filter radius        (11 Sept 1996)  */
264    int   filter_opt;              /*!< filter option        (11 Sept 1996)  */
265 
266    float thrfilter_rmm;           /*!< threshold filter radius (1 Oct 1996) */
267    int   thrfilter_opt;           /*!< threshold filter option (1 Oct 1996) */
268 
269    int   scale ;                  /*!< linearly scale data so max = 10000   */
270 
271    float mult ;                   /*!< multiply all voxels by this          */
272 
273    int   do_zvol ;                /*!< zero out a 3D sub-volume             */
274    float zv_x1 ;                  /*!< dimensions of sub-volume to massacre */
275    float zv_x2 ;                  /*!< dimensions of sub-volume to massacre */
276    float zv_y1 ;                  /*!< dimensions of sub-volume to massacre */
277    float zv_y2 ;                  /*!< dimensions of sub-volume to massacre */
278    float zv_z1 ;                  /*!< dimensions of sub-volume to massacre */
279    float zv_z2 ;                  /*!< dimensions of sub-volume to massacre */
280 
281    int   iv_fim ;                 /*!< use this sub-brick for voxel values */
282    int   iv_thr ;                 /*!< use this sub-brick for threshold    */
283 
284    int   zscore ;                 /*!< 17 Sep 1998 --> convert statistic to Z   */
285 
286    int   verbose ;                /*!< 01 Nov 1999 --> verbose output during editing */
287 
288    int  nfmask ;                  /*!< 09 Aug 2000 --> filter mask */
289    byte *fmask ;
290    char *fexpr ;                  /*!< 09 Aug 2000 --> filter expression */
291    int   fmclip ;                 /*!< 11 Oct 2007 --> clip at fmask? */
292 
293    int fake_dxyz ;                /*!< 11 Sep 2000 -> use dx=dy=dz=1.0? */
294 
295    int rank;                      /*!< 13 Nov 2007 --> ZSS: Rank dset values. */
296    char rankmapname[THD_MAX_NAME+THD_MAX_PREFIX+1];
297 
298    int isomode;                  /*!< 03 March 2010, ZSS: Use value for clust */
299 
300    float blurx,blury,blurz ;     /*!< 18 Jun 2019, RWC: aniso blurring */
301 } EDIT_options ;
302 
303 /*--- cluster editing options ---*/   /* 10 Sept 1996 */
304 #define ECFLAG_NONE   0
305 #define ECFLAG_SAME   1
306 #define ECFLAG_MEAN   2
307 #define ECFLAG_MAX    3
308 #define ECFLAG_AMAX   4
309 #define ECFLAG_SMAX   5
310 #define ECFLAG_SIZE   6
311 #define ECFLAG_ORDER  7         /* 09 June 1998 */
312 #define ECFLAG_DEPTH  8         /* 02 March 2010 ZSS */
313 
314 /*--- filtering options ---*/   /* 11 Sept 1996 */
315 #define FCFLAG_NONE   0
316 #define FCFLAG_MEAN   1
317 #define FCFLAG_NZMEAN 2
318 #define FCFLAG_MAX    3
319 #define FCFLAG_AMAX   4
320 #define FCFLAG_SMAX   5
321 
322 #define FCFLAG_AVER   66        /*  7 Jan 1998 */
323 #define FCFLAG_EXPR   77        /* 09 Aug 2000 */
324 
325 #define FCFLAG_ONE_STEP 100000
326 #define FCFLAG_WINSOR   (2*FCFLAG_ONE_STEP)  /* 11 Sep 2000 */
327 
328 /*! Initialize an EDIT_options struct. */
329 
330 #define INIT_EDOPT(edopt)              \
331       ( (edopt)->thtoin        = 0   , \
332         (edopt)->noneg         = 0   , \
333         (edopt)->abss          = 0   , \
334         (edopt)->clip_bot      = 0.0 , \
335         (edopt)->clip_top      = 0.0 , \
336         (edopt)->thresh        = 0.0 , \
337         (edopt)->thbot         = 0.0 , \
338         (edopt)->clust_rmm     = -1.0, \
339         (edopt)->clust_vmul    = 0.0 , \
340         (edopt)->edit_clust    = 0   , \
341 	(edopt)->erode_pv      = 0.0 , \
342 	(edopt)->dilate        = 0   , \
343         (edopt)->filter_rmm    = 0.0 , \
344         (edopt)->filter_opt    = 0   , \
345         (edopt)->thrfilter_rmm = 0.0 , \
346         (edopt)->thrfilter_opt = 0   , \
347         (edopt)->blur          = 0.0 , \
348         (edopt)->blurx         = 0.0 , \
349         (edopt)->blury         = 0.0 , \
350         (edopt)->blurz         = 0.0 , \
351         (edopt)->thrblur       = 0.0 , \
352         (edopt)->scale         = 0   , \
353         (edopt)->mult          = 0.0 , \
354         (edopt)->do_zvol       = 0   , \
355         (edopt)->clip_unscaled = 0   , \
356         (edopt)->iv_fim        = -1  , \
357         (edopt)->iv_thr        = -1  , \
358         (edopt)->zscore        = 0   , \
359         (edopt)->verbose       = 0   , \
360         (edopt)->nfmask        = 0   , \
361         (edopt)->fmask         = NULL, \
362         (edopt)->fexpr         = NULL, \
363         (edopt)->fmclip        = 1,    \
364         (edopt)->fake_dxyz     = 0   , \
365         (edopt)->rank          = 0,    \
366         (edopt)->rankmapname[0]= '\0', \
367         (edopt)->isomode       = 0, \
368        0 )
369 
370 extern void EDIT_one_dataset( THD_3dim_dataset * dset , EDIT_options * edopt ) ;
371 
372 extern void EDIT_blur_volume( int,int,int , float,float,float , int,void * , float ) ;
373 extern void EDIT_blur_volume_3d( int,int,int , float,float,float , int,void * , float, float, float ) ;
374 
375 void EDIT_blur_allow_fir( int ) ;  /* 04 Oct 2005 */
376 
377 /* Gaussian blur in image space, not FFT space: 04 Oct 2005 */
378 
379 extern void FIR_blur_volume( int nx, int ny, int nz,
380                              float dx, float dy, float dz,
381                              float *ffim , float sigma ) ;
382 
383 extern void FIR_blur_volume_3d( int nx, int ny, int nz,
384                                 float dx, float dy, float dz,
385                                 float *ffim ,
386                                 float sigmax, float sigmay, float sigmaz ) ;
387 
388 /*! Convert Gaussian blur RMS width to sigma [1/sqrt(3)] */
389 #define RMS_TO_SIGMA(rms) (0.57735027*(rms))
390 
391 /*! Convert Gaussian blur FWHM width to sigma [1/sqrt(log(2)*8)] */
392 #define FWHM_TO_SIGMA(fh) (0.42466090*(fh))
393 
394 extern int EDIT_check_argv( int , char * argv[] , int , EDIT_options * ) ;
395 
396 extern void EDIT_coerce_type      ( int , int,void * , int,void * ) ;
397 extern void EDIT_coerce_scale_type( int , float , int,void * , int,void * ) ;
398 extern float EDIT_coerce_autoscale( int , int,void * , int,void * ) ;
399 extern float EDIT_convert_dtype   ( int , int,void * , int,void *, int ) ;
400 extern int   is_integral_data     ( int , int , void * ) ;
401 
402 extern float EDIT_coerce_autoscale_new( int nxyz , int itype ,
403                                         void *ivol , int otype , void *ovol ) ;
404 extern float EDIT_scale_misfit( int nxyz, float fac, short *sar, float *far ) ;
405 extern void EDIT_misfit_report( char *name, int ib,
406                                 int nxyz, float fac, short *sar, float *far ) ;
407 extern void EDIT_set_misfit_mask( byte * ) ;
408 
409 extern void EDIT_floatize_dataset( THD_3dim_dataset *dset ) ;
410 extern int DSET_pure_type( THD_3dim_dataset *dset ) ;
411 
412 #undef  DSET_IS_FLOAT
413 #define DSET_IS_FLOAT(ds) (DSET_pure_type((ds))==MRI_float)
414 
415 #undef  DSET_IS_SHORT
416 #define DSET_IS_SHORT(ds) (DSET_pure_type((ds))==MRI_short)
417 
418 #undef  DSET_IS_BYTE
419 #define DSET_IS_BYTE(ds) (DSET_pure_type((ds))==MRI_byte)
420 
421 extern void EDIT_aver_fvol( int, int, int,
422                             float, float, float, float *, float) ;
423 
424 extern void EDIT_zscore_vol( int,int,float,void *,int,float * ) ;
425 
426 extern void EDIT_clip_float( float , int , float * ) ;
427 
428 extern byte * EDT_calcmask( char * , int * , int) ;  /* 16 Mar 2000 */
429 
430 extern void * EDIT_volpad( int,int,int,int,int,int ,
431                            int,int,int , int,void * ) ; /* 09 Feb 2001 */
432 
433 #define EDIT_zeropad EDIT_volpad                        /* 14 Feb 2001 */
434 
435 extern void EDIT_set_padval( int ftype , char *pval ) ; /* 22 Sep 2020 */
436 #define EDIT_clear_padval EDIT_set_padval(-1,NULL)      /* 22 Sep 2020 */
437 
438 #define EDIT_volpad_even(px,py,pz,nx,ny,nz,ft,vv) \
439    EDIT_volpad( (px),(px), (py),(py), (pz),(pz), (nx),(ny),(nz), (ft),(vv) )
440 
441 /********************* New routines for AFNI-96 ****************************/
442 
443 /**----------------------- prototypes -----------------------**/
444 
445 extern THD_3dim_dataset * EDIT_empty_copy( THD_3dim_dataset * ) ;
446 extern THD_3dim_dataset * EDIT_full_copy ( THD_3dim_dataset * , char * ) ;
447 extern int                EDIT_dset_items( THD_3dim_dataset * , ... ) ;
448 extern THD_3dim_dataset * EDIT_geometry_constructor( char * , char * ) ; /* 05 Jan 2008 */
449 extern char * EDIT_get_geometry_string( THD_3dim_dataset *dset ) ;
450 extern char * EDIT_imat_to_geometry_string( mat44 imat , int nx,int ny,int nz ) ;
451 
452 extern float EDIT_geometry_string_diff( char *astr , char *bstr ) ;  /* 30 Oct 2014 */
453 extern mat44_nxyz EDIT_geometry_string_to_mat44( char *gstr ) ;
454 extern char * EDIT_geometry_string_pad( char *gsin , int npad ) ; /* Halloween 2014 */
455 extern char * EDIT_geomstring_from_collection( int nstr , char **gsin ) ;
456 extern float_triple EDIT_geometry_string_to_delxyz( char *gstr ) ;
457 
458 extern char * EDIT_geomstring_from_corners( float xxbot, float xxtop ,  /* 17 May 2021 */
459                                             float yybot, float yytop ,
460                                             float zzbot, float zztop ,
461                                             float dx, float dy, float dz ) ;
462 
463 extern THD_3dim_dataset * jRandomDataset(int,int,int,int) ; /* 16 Mar 2016 */
464 extern MRI_IMAGE *        jRandom1D(int,int) ;              /* 17 Mar 2016 */
465 
466 #define ISVALID_GEOMETRY_STRING(ggg)                      \
467  ( (ggg) != NULL && strncasecmp((ggg),"matrix(",7) == 0 )
468 
469 extern int THD_volDXYZscale(  THD_dataxes  *daxes,
470                               float xyzscale,
471                               int reuse_shift);    /* ZSS Dec 07 */
472 extern THD_3dim_dataset * EDIT_wod_copy( THD_3dim_dataset * ) ; /* 31 Jul 2002 */
473 extern THD_datablock *    EDIT_empty_datablock(void) ;          /* 11 Mar 2005 */
474 
475 extern void EDIT_add_bricklist( THD_3dim_dataset *,int,int *,float *,void *sbr[] ) ;
476 
477 extern void EDIT_add_brick( THD_3dim_dataset * , int , float , void * ) ;
478 extern int EDIT_add_bricks_from_far(THD_3dim_dataset *dset,
479                                       float **far, int nval,
480                                       int otype, char scaleopt,
481                                       int verb);
482 
483 extern void EDIT_substitute_brick( THD_3dim_dataset *,  int,int, void * ) ;
484 extern void EDIT_substscale_brick( THD_3dim_dataset *,  int,int, void *, int,float ) ;
485 
486 /* 10 Sept 1996 */
487 extern void EDIT_cluster_array (MCW_cluster_array * , int, float, float);
488 
489 /* 11 Sept 1996 */
490 extern void EDIT_filter_volume (int, int, int, float, float, float,
491                                 int, void *, int, float, byte *, int, char * );
492 
493 /* 13 Sept 2005 [rickr] */
494 extern THD_marker_set * create_empty_marker_set(void);
495 extern int              okay_to_add_markers(THD_3dim_dataset * dset);
496 
497 /* 15 Jan 2007 [RWC] -- in edt_clustalpha.c */
498 
499 extern int cluster_alphaindex_64( int csize, int nz, float fw, float pv ) ;
500 
501 
502 /**---------------- AFNI Dataset item Names ----------------**/
503 
504 #define ADN_none                 0
505 
506 /** values in the diskptr **/
507 
508 #define ADN_prefix               6001     /*=  char *  =*/
509 #define ADN_directory_name       6002     /*=  char *  =*/
510 
511 /** values in the datablock **/
512 
513 #define ADN_brick_fac            6011     /*=  float *  =*/
514 #define ADN_malloc_type          6012     /*=  int      =*/
515 #define ADN_datum_all            6013     /*=  int      =*/
516 #define ADN_datum_array          6014     /*=  int *    =*/
517 #define ADN_nvals                6016     /*=  int      =*/
518 
519 /** values in the dataxes **/
520 
521 #define ADN_nxyz                 6020     /*=  THD_ivec3  =*/
522 #define ADN_xyzdel               6021     /*=  THD_fvec3  =*/
523 #define ADN_xyzorg               6022     /*=  THD_fvec3  =*/
524 #define ADN_xyzorient            6023     /*=  THD_ivec3  =*/
525 #define ADN_to_dicomm            6024     /*=  THD_mat33  =*/
526 
527 #define ADN_ijk_to_dicom         6026     /*=  mat44 [19 Dec 2005] =*/
528 
529 /** values in the timeaxis **/
530 
531 #define ADN_ntt                  6031     /*=  int    =*/
532 #define ADN_ttorg                6032     /*=  float  =*/
533 #define ADN_ttdel                6033     /*=  float  =*/
534 #define ADN_ttdur                6034     /*=  float  =*/
535 
536 #define ADN_nsl                  6035     /*=  int      =*/
537 #define ADN_zorg_sl              6036     /*   float    =*/
538 #define ADN_dz_sl                6037     /*   float    =*/
539 #define ADN_toff_sl              6039     /*=  float *  =*/
540 #define ADN_tunits               6040     /*=  int      =*/  /* 21 Oct 1996 */
541 
542 /** values in the 3dim_dataset itself **/
543 
544 #define ADN_type                 6051     /*=  int     =*/
545 #define ADN_view_type            6052     /*=  int     =*/
546 #define ADN_func_type            6053     /*=  int     =*/
547 #define ADN_label1               6054     /*=  char *  =*/
548 #define ADN_label2               6055     /*=  char *  =*/
549 #define ADN_self_name            6056     /*=  char *  =*/
550 #define ADN_keywords_replace     6057     /*=  char *  =*/
551 #define ADN_keywords_append      6058     /*=  char *  =*/
552 
553 #define ADN_warp_parent          6061     /*=  THD_3dim_dataset *  =*/
554 #define ADN_anat_parent          6062     /*=  THD_3dim_dataset *  =*/
555 #define ADN_stat_aux             6063     /*=  float *             =*/
556 #define ADN_warp                 6064     /*=  THD_warp *          =*/
557 #define ADN_anatpar_idcode       6065     /*=  MCW_idcode * [13 Dec 1999] =*/
558 
559 /* 30 Nov 1997 */
560 /* 100000 -> 10000000                            3 Oct 2011 [rickr]
561  * Allow for 10 million sub-bricks in output datasets.  Many people
562  * currently need more than just 100000 (e.g. HJ Jo, Javier, Meghan). */
563 #define ADN_ONE_STEP            10000000
564 #define ADN_brick_label_one             (2*ADN_ONE_STEP)  /*=  char *   =*/
565 #define ADN_brick_fac_one               (3*ADN_ONE_STEP)  /*=  float    =*/
566 #define ADN_brick_stataux_one           (4*ADN_ONE_STEP)  /*=  float *  =*/
567 #define ADN_brick_keywords_replace_one  (5*ADN_ONE_STEP)  /*=  char *   =*/
568 #define ADN_brick_keywords_append_one   (6*ADN_ONE_STEP)  /*=  char *   =*/
569 
570 /*------------------------------------------------------------------*/
571 /* These 2 macros added 14 Dec 1999 */
572 /*------------------------------------------------------------------*/
573 
574 /*! Copy anat parent from old datasets ods to new dataset nds. */
575 
576 #define EDIT_COPY_ANATOMY_PARENT_ID(nds,ods)                   \
577   do{ if( ISVALID_DSET(nds) && ISVALID_DSET(ods) )              \
578          (nds)->anat_parent_idcode = (ods)->anat_parent_idcode ; \
579     } while(0)
580 
581 /*! Null out the anat parent of dataset nds. */
582 
583 #define EDIT_ZERO_ANATOMY_PARENT_ID(nds)                 \
584   do{ if( ISVALID_DSET(nds) )                             \
585          ZERO_IDCODE((nds)->anat_parent_idcode); } while(0)
586 
587 /*------------------------------------------------------------------*/
588 /* These 2 macros added 20 Aug 2008 */
589 /*------------------------------------------------------------------*/
590 
591 #define EDIT_TO_FUNC_BUCKET(ds)                        \
592   EDIT_dset_items( (ds) ,                              \
593                     ADN_type      , HEAD_FUNC_TYPE ,   \
594                     ADN_func_type , FUNC_BUCK_TYPE ,   \
595                     ADN_ntt       , 0              ,   \
596                    ADN_none )
597 
598 #define EDIT_TO_ANAT_BUCKET(ds)                        \
599   EDIT_dset_items( (ds) ,                              \
600                     ADN_type      , HEAD_ANAT_TYPE ,   \
601                     ADN_func_type , ANAT_BUCK_TYPE ,   \
602                     ADN_ntt       , 0              ,   \
603                    ADN_none )
604 
605 /*------------------------------------------------------------------*/
606 /* Added 15 Mar 2021 -- why didn't I do this before??? */
607 /*------------------------------------------------------------------*/
608 
609 #define EDIT_DSET_PREFIX(ds,pf)                        \
610   EDIT_dset_items( (ds), ADN_prefix,(pf) , ADN_none )
611 
612 /*------------------------------------------------------------------*/
613 /*! Change statistical parameters in dataset ds, sub-brick iv,
614     to statistical type ft, with parameters a,b,c,d.
615 */
616 
617 #define EDIT_STATAUX4(ds,iv,ft,a,b,c,d)                     \
618  do{ float sqq[6] ;                                           \
619      if( ISVALID_DSET(ds) &&                                    \
620          (iv) >= 0 && (iv) < DSET_NVALS(ds) &&                    \
621          (ft) >= 0 && (ft) <= LAST_FUNC_TYPE   ){                   \
622         sqq[0] = (ft) ; sqq[1] = FUNC_need_stat_aux[ft] ;             \
623         sqq[2] = (a) ; sqq[3] = (b) ; sqq[4] = (c) ; sqq[5] = (d) ;     \
624         EDIT_dset_items( (ds),ADN_brick_stataux_one+(iv),sqq,ADN_none ) ; \
625      } } while(0)
626 
627 /*! Convert sub-brick iv of dataset ds to a no-statistic [16 Jun 2003] */
628 
629 #define EDIT_BRICK_TO_NOSTAT(ds,iv) \
630   EDIT_STATAUX4(ds,iv,FUNC_FIM_TYPE,0,0,0,0)
631 
632 /*! Convert sub-brick iv of dataset ds to a fico (correlation) statistic. */
633 
634 #define EDIT_BRICK_TO_FICO(ds,iv,nsam,nfit,nort) \
635   EDIT_STATAUX4(ds,iv,FUNC_COR_TYPE,nsam,nfit,nort,0)
636 
637 /*! Convert sub-brick iv of dataset ds to a fitt (t test) statistic. */
638 
639 #define EDIT_BRICK_TO_FITT(ds,iv,ndof) \
640   EDIT_STATAUX4(ds,iv,FUNC_TT_TYPE,ndof,0,0,0)
641 
642 /*! Convert sub-brick iv of dataset ds to a fift (F test) statistic. */
643 
644 #define EDIT_BRICK_TO_FIFT(ds,iv,ndof,ddof) \
645   EDIT_STATAUX4(ds,iv,FUNC_FT_TYPE,ndof,ddof,0,0)
646 
647 /*! Convert sub-brick iv of dataset ds to a fizt (z score) statistic. */
648 
649 #define EDIT_BRICK_TO_FIZT(ds,iv) \
650   EDIT_STATAUX4(ds,iv,FUNC_ZT_TYPE,0,0,0,0)
651 
652 /*! Convert sub-brick iv of dataset ds to a fict (chi square) statistic. */
653 
654 #define EDIT_BRICK_TO_FICT(ds,iv,ndof) \
655   EDIT_STATAUX4(ds,iv,FUNC_CT_TYPE,ndof,0,0,0)
656 
657 /*! Convert sub-brick iv of dataset ds to a fibt (beta variable) statistic. */
658 
659 #define EDIT_BRICK_TO_FIBT(ds,iv,a,b) \
660     EDIT_STATAUX4(ds,iv,FUNC_BT_TYPE,a,b,0,0)
661 
662 /*! Convert sub-brick iv of dataset ds to a fibn (binomial variable) statistic. */
663 
664 #define EDIT_BRICK_TO_FIBN(ds,iv,ntrial,prob) \
665     EDIT_STATAUX4(ds,iv,FUNC_BN_TYPE,ntrial,prob,0,0)
666 
667 /*! Convert sub-brick iv of dataset ds to a figt (gamma variable) statistic. */
668 
669 #define EDIT_BRICK_TO_FIGT(ds,iv,shape,scale) \
670     EDIT_STATAUX4(ds,iv,FUNC_GT_TYPE,shape,scale,0,0)
671 
672 /*! Convert sub-brick iv of dataset ds to a fipt (Poisson variable) statistic. */
673 
674 #define EDIT_BRICK_TO_FIPT(ds,iv,mean) \
675     EDIT_STATAUX4(ds,iv,FUNC_PT_TYPE,mean,0,0,0)
676 
677 /*------------------------------------------------------------------*/
678 
679 /*! Change the iv-th sub-brick label in dataset ds to str. */
680 
681 #define EDIT_BRICK_LABEL(ds,iv,str) \
682      EDIT_dset_items( (ds), ADN_brick_label_one+(iv), (str), ADN_none )
683 
684 /*! Change the iv-th sub-brick scale factor in dataset ds to fac
685     (factor=0 means "don't scale"). */
686 
687 #define EDIT_BRICK_FACTOR(ds,iv,fac) \
688      EDIT_dset_items( (ds), ADN_brick_fac_one+(iv), (fac), ADN_none )
689 
690 /*! Add a keyword to sub-brick #iv of dataset ds. */
691 
692 #define EDIT_BRICK_ADDKEY(ds,iv,str) \
693      EDIT_dset_items( (ds), ADN_brick_keywords_append_one+(iv), (str), ADN_none )
694 
695 /*------------------------------------------------------------------*/
696 /* Prefix for an indicial sub-label for a statistic volume */
697 
698 extern char EDIT_get_index_prefix(void) ;
699 extern void EDIT_set_index_prefix(char c) ;
700 
701 #if 0
702 /*------------------------------------------------------------------------*/
703 /*! Change the orientation of a dataset */
704 
705 #define EDIT_DSET_ORIENT(ds,ox,oy,oz)                              \
706  do{ THD_ivec3 orixyz ;                                            \
707      LOAD_IVEC3( orixyz , (ox),(oy),(oz) ) ;                       \
708      EDIT_dset_items( (ds) , ADN_xyzorient , orixyz , ADN_none ) ; \
709  } while(0)
710 
711 #define DSET_TO_RAI(ds) EDIT_DSET_ORIENT((ds),ORI_R2L_TYPE,ORI_A2P_TYPE,ORI_I2S_TYPE)
712 #define DSET_TO_LPI(ds) EDIT_DSET_ORIENT((ds),ORI_L2R_TYPE,ORI_P2A_TYPE,ORI_I2S_TYPE)
713 #endif
714 
715 /*------------------------------------------------------------------*/
716 /* 22 Aug 2005: neighborhood/local operations. */
717 
718 #define MAX_NCODE 666
719 #define MAX_CODE_PARAMS 16
720 
721 #define NTYPE_SPHERE 1  /* mask types: sphere */
722 #define NTYPE_RECT   2              /* rectangular block */
723 #define NTYPE_RHDD   3              /* rhombic dodecahedron */
724 #define NTYPE_TOHD   4              /* truncated octahedron */
725 
726 extern void SetSearchAboutMaskedVoxel(int v);  /* ZSS */
727 extern MRI_IMAGE * THD_get_dset_nbhd( THD_3dim_dataset *, int, byte *,
728                                       int, int, int, MCW_cluster *    ) ;
729 extern MRI_IMARR * THD_get_dset_indexed_nbhd(
730                                       THD_3dim_dataset *, int, byte *,
731                                       int, int, int, MCW_cluster *    ) ;
732 
733 extern MRI_IMAGE * mri_get_nbhd( MRI_IMAGE *, byte *,
734                                  int, int, int, MCW_cluster * ) ;
735 extern MRI_IMARR * mri_get_indexed_nbhd( MRI_IMAGE *, byte *,
736                                          int, int, int, MCW_cluster * ) ;
737 
738 extern int mri_get_nbhd_array( MRI_IMAGE *inim , byte *mask ,
739                                int xx, int yy, int zz, MCW_cluster *nbhd, void *nar ) ;
740 extern int mri_load_nbhd_indices ( int nx, int ny, int nz , byte *mask ,
741                           int xx, int yy, int zz, MCW_cluster *nbhd,
742                           int *nind);
743 extern MRI_IMAGE * mri_localstat( MRI_IMAGE *, byte *, MCW_cluster *, int ) ;
744 extern THD_3dim_dataset * THD_localstat( THD_3dim_dataset *, byte *,
745                                          MCW_cluster *, int, int *,
746                                          float p[][MAX_CODE_PARAMS+1],
747                                          float *reduce_grid, int resam_mode) ;
748 
749 extern void THD_localstat_datum(int);
750 extern void THD_localstat_verb(int);
751 
752 extern int DSET_1Dindex_to_regrid_ijk( THD_3dim_dataset *iset, int ijk,
753                                  THD_3dim_dataset *gset,
754                                  int *ii, int *jj, int *kk);
755 extern THD_3dim_dataset * THD_reduced_grid_copy(THD_3dim_dataset *dset,
756                                  float *redx);
757 extern MRI_IMAGE * mri_localbistat( MRI_IMAGE *, MRI_IMAGE *,
758                                     byte *, MCW_cluster *, int ) ;
759 extern THD_3dim_dataset * THD_localbistat( THD_3dim_dataset *, THD_3dim_dataset *,
760                                            byte *, MCW_cluster *, int, int *) ;
761 extern void THD_localbistat_verb(int) ;
762 
763 
764 #ifdef  __cplusplus
765 }
766 #endif
767 
768 #endif /* _MCW_EDITVOL_ */
769