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 <stdio.h>
8 #include <stdlib.h>
9 #include <math.h>
10 #include <string.h>
11 
12 #include "Aint.h"        /* 17 Nov 2021 */
13 
14 #include "debugtrace.h"  /* 26 Jan 2001 addition */
15 #include "Amalloc.h"     /* 09 Dec 2003 addition */
16 #include "Aomp.h"
17 #include "mcw_malloc.h"
18 #include "vecmat.h"
19 
20 #ifndef _MCW_MRILIB_HEADER_
21 #define _MCW_MRILIB_HEADER_
22 
23 #define MRILIB_7D
24 #define _GNU_SOURCE 1           /* 23 Jun 2011 */
25 
26 #define COXEMAIL "rwcox@nih.gov"        /* or /dev/null, if you prefer */
27 
28 #ifdef  __cplusplus
29 extern "C" {                    /* care of Greg Balls    7 Aug 2006 [rickr] */
30 #endif
31 
32 /*------------------------------------------------------------------*/
33 
34 #undef INLINE
35 
36 #ifdef __GNUC__
37 # define INLINE __inline__
38 #endif
39 
40 #ifndef INLINE
41 # define INLINE /*nada*/
42 #endif
43 
44 #undef RESTRICT
45 #ifdef __GNUC__
46 # define RESTRICT __restrict__
47 #else
48 # define RESTRICT /*nada*/
49 #endif
50 
51 #ifdef MRILIB_MINI
52 # define MRILIB_verb 0
53 /******************* Some sample data structures (from nifti2io.h) ***********************/
54 
55 typedef struct {                   /** 4x4 matrix struct **/
56   float m[4][4] ;
57 } mat44 ;
58 
59 typedef struct {                   /** 3x3 matrix struct **/
60   float m[3][3] ;
61 } mat33 ;
62 
63 typedef struct {                   /** 4x4 matrix struct (double) **/
64   double m[4][4] ;
65 } nifti_dmat44 ;
66 
67 typedef struct {                   /** 3x3 matrix struct (double) **/
68   double m[3][3] ;
69 } nifti_dmat33 ;
70 /*------------------------------------------------------------------*/
71 /*******************3ddata.h stuff  ***********************/
72 typedef struct {
73   int    nvec , nvals , ignore ;
74   int   *ivec ;
75   float *fvec ;
76   int    nx,ny,nz ;
77   float  dx,dy,dz , dt ;
78 } MRI_vectim ;
79 
80 /*------------------------------------------------------------------*/
81 #else
82  extern int MRILIB_verb ;                /* 01 May 2009 */
83  #include "nifti2_io.h"
84  #include "mri_dicom_stuff.h"
85  extern AFD_dicom_header **MRILIB_dicom_header ;
86  /* preferentially include f2c header from local directory, otherwise use system
87   header */
88  #include "f2c.h"
89  /* The following was added to harmonize with system f2c header. Subsequent
90  typedef for complex is now ignored */
91  #define TYPEDEF_complex
92 #endif /* MRILIB_MINI */
93 
94 
95 
96 extern char MRILIB_orients[] ;          /* 12 Mar 2001 */
97 extern float MRILIB_zoff ;              /* global variables from mri_read.c */
98 extern float MRILIB_tr ;                /* 03 Dec 2001 */
99 extern float MRILIB_xoff ;              /* 07 Dec 2001 */
100 extern float MRILIB_yoff ;
101 
102 extern int use_MRILIB_zoff ;            /* 20 Dec 2001 */
103 extern int use_MRILIB_xoff ;
104 extern int use_MRILIB_yoff ;
105 
106 extern int   use_MRILIB_xcos ;         /* 22 Jul 2002 */
107 extern int   use_MRILIB_ycos ;
108 extern int   use_MRILIB_zcos ;
109 extern float MRILIB_xcos[3] ;
110 extern float MRILIB_ycos[3] ;
111 extern float MRILIB_zcos[3] ;
112 
113 extern int   use_MRILIB_slicespacing ;  /* 10 Jan 2004 */
114 extern float MRILIB_slicespacing ;
115 
116 extern int MRILIB_DomainMaxNodeIndex ;         /* 32 Dec 2007 */
117 
118 extern int   assume_dicom_mosaic ;   /* mri_read_dicom.c  13 Mar 2006 [rickr] */
119 extern int   use_new_mosaic_code;    /* mri_process_siemens.c 23 Dec 2010 [r] */
120 
121 /* siemens slice timing info from mri_read.c         13 Apr 2011 [rickr] */
122 extern int     g_siemens_timing_nused;  /* number of times used          */
123 extern float * g_siemens_timing_times;  /* actual list of times          */
124 extern int     g_siemens_timing_units;  /* time units, UNITS_MSEC_TYPE?  */
125 extern int     populate_g_siemens_times(int tunits);
126 extern int     get_and_display_siemens_times(void);
127 extern int     valid_g_siemens_times(int, float, int, int);
128 
129 /*----------------------------------------------------------------------------*/
130 
131 extern int use_MRILIB_dicom_matrix ;    /* 26 Jan 2006 */
132 extern mat44   MRILIB_dicom_matrix ;
133 
134 extern int                MRILIB_dicom_count ;  /* 15 Mar 2006 */
135 extern int                MRILIB_dicom_s16_overflow ;  /* 9 Jul 2013 [rickr] */
136 
137 /*! Clear the MRILIB globals
138     (which transmit info from image files to to3d.c). */
139 
140 #define CLEAR_MRILIB_globals                              \
141  do{ MRILIB_orients[0]='\0';                              \
142      MRILIB_zoff=MRILIB_xoff=MRILIB_yoff=MRILIB_tr=0.0;   \
143      use_MRILIB_xoff=use_MRILIB_yoff=use_MRILIB_zoff=0;   \
144      use_MRILIB_xcos=use_MRILIB_ycos=use_MRILIB_zcos=0;   \
145      use_MRILIB_slicespacing=0;                           \
146      use_MRILIB_dicom_matrix=0;                           \
147      MRILIB_dicom_count=0; MRILIB_dicom_header=NULL;      \
148  } while(0)
149 
150 
151 /*----------------------------------------------------------------------------*/
152 
153 #ifndef PI
154 #  define PI  3.1415926535897932
155 #endif
156 #ifndef HPI
157 #  define HPI 1.5707963267948966
158 #endif
159 
160 #ifndef WAY_BIG
161 /*! A big number (anything over this is infinity). */
162 #  define WAY_BIG 1.e+10
163 #endif
164 
165 #ifndef TINY_NUMBER
166 /*! A tiny, infinitessimal number */
167 #  define TINY_NUMBER 1.e-10
168 #endif
169 
170 #ifndef FLDIF
171 /*! Are 2 floats significantly different? */
172 #  define FLDIF(x,y) ( fabs(x-y) > 1.e-4 * (fabs(x)+fabs(y)) )
173 #endif
174 
175 
176 #ifndef MAX
177 #  define MAX(a,b) (((a)<(b)) ? (b) : (a))
178 #endif
179 
180 #ifndef MIN
181 #  define MIN(a,b) (((a)>(b)) ? (b) : (a))
182 #endif
183 
184 /**** define types ****/
185 
186 /*! The MRI_byte data type. */
187 
188 #ifndef TYPEDEF_byte
189 #define TYPEDEF_byte
190 typedef unsigned char byte ;
191 #endif
192 
193 #ifndef TYPEDEF_sbyte
194 #define TYPEDEF_sbyte
195 typedef signed char sbyte ;
196 #endif
197 
198 /*! RGBA data type; not used anywhere (yet). */
199 
200 #ifndef TYPEDEF_rgba
201 #define TYPEDEF_rgba
202 typedef struct { byte r,g,b,a ; } rgba ;  /* 24 Aug 2001 */
203 #endif
204 
205 #define LOAD_rgba(s,rr,gg,bb,aa)   ((s).r=(rr),(s).g=(gg),(s).b=(bb),(s).a=(bb))
206 #define UNLOAD_rgba(s,rr,gg,bb,aa) ((rr)=(s).r,(gg)=(s).g,(bb)=(s).b,(aa)=(s).a)
207 
208 /*! Scale a byte [0..255] to a float in [0..1). */
209 
210 #define BYTE_TO_ZONE(b) (0.00392157*(b))
211 
212 /*! Scale a float in [0..1] to a byte in [0..255]. */
213 
214 #define ZONE_TO_BYTE(z) ((byte)(255.49*(z)))
215 
216 /*! Integer flags for different image types.  Sometimes called the "datum". */
217 
218 typedef enum MRI_TYPE {
219          MRI_byte , MRI_short  , MRI_int  ,
220         MRI_float , MRI_double , MRI_complex , MRI_rgb , MRI_rgba ,
221         MRI_fvect
222  } MRI_TYPE ;
223 
224 #define MRI_KIND MRI_TYPE ;   /* to alleviate stupidity */
225 #define MRI_type MRI_TYPE ;
226 #define MRI_kind MRI_TYPE ;
227 
228 #define MRI_rgbyte MRI_rgb
229 
230 /*! The last MRI_TYPE yet defined. */
231 
232 #define LAST_MRI_TYPE 7
233 
234 /*! String names for MRI_TYPE. */
235 
236 static char * MRI_TYPE_name[9] =
237   { "byte"  , "short", "int", "float", "double", "complex", "rgb", "RGBA" ,
238     "fvect"
239   } ;
240 
241 #define IS_REAL_TYPE(zkq) ((zkq)==MRI_byte || (zkq)==MRI_short || (zkq)==MRI_float)
242 #define IS_RGB_TYPE(zkq)  ((zkq)==MRI_rgb  || (zkq)==MRI_rgba)
243 
244 #define IS_REAL_IMAGE(iq) IS_REAL_TYPE((iq)->kind)
245 #define IS_RGB_IMAGE(iq)  IS_RGB_TYPE((iq)->kind)
246 
247 #define MRI_type_name MRI_TYPE_name  /* because I forget */
248 
249 #define MRI_type_string(iq) \
250   ( ((iq) < 0 || (iq) > LAST_MRI_TYPE ) ? "unknown" : MRI_TYPE_name[iq] )
251 
252 #define MRI_TYPE_NAME(imm)  MRI_type_string((imm)->kind)
253 
254 /*! Max value of a byte. */
255 
256 #define MRI_maxbyte         255
257 
258 /*! Max value of a short. */
259 
260 #define MRI_maxshort      32767
261 
262 /*! Max value of an int. */
263 
264 #define MRI_maxint   2147483647
265 
266 /*! Max values for various types, if they have them. */
267 
268 static float MRI_TYPE_maxval[9] =
269   { 255.0f, 32767.0f, 2147483647.0f, 0.0f,0.0f,0.0f, 255.0f, 255.0f, 0.0f } ;
270 
271 /*! Force a float into a short. */
272 
273 #define SHORTIZE(xx) (  ((xx) < -32767.0f) ? (short)-32767                    \
274                       : ((xx) >  32767.0f) ? (short) 32767 : (short)rint(xx) )
275 
276 /*! Force a float into a byte. */
277 
278 #define BYTEIZE(xx)  (  ((xx) <   0.0 ) ? (byte)0                             \
279                       : ((xx) > 255.0f) ? (byte)255 : (byte)rintf(xx) )
280 
281 /*! Determine if a MRI_TYPE is an integer type. */
282 
283 #define MRI_IS_INT_TYPE(typ) ((typ) < 3)
284 
285 #define MRI_IS_FLOAT_TYPE(typ) ((typ) >=3 && (typ) <= 5)
286 
287 /*! I suppose that the next C makes this pleonastic. */
288 
289 #if defined(_SUNPERF_COMPLEX) || defined(DONT_DECLARE_COMPLEX)
290 # define TYPEDEF_complex
291 #endif
292 
293 #ifndef TYPEDEF_complex
294 #define TYPEDEF_complex
295 #ifndef complex
296 typedef struct complex { float r , i ; } complex ;
297 #endif
298 #endif
299 
300 #ifndef TYPEDEF_float_pair
301 #define TYPEDEF_float_pair
302 typedef struct { float a,b ; } float_pair ;
303 #endif
304 
305 #ifndef TYPEDEF_float_triple
306 #define TYPEDEF_float_triple
307 typedef struct { float a,b,c ; } float_triple ;
308 #define float_trip float_triple
309 #define ASSIGN_FLOAT_TRIPLE(ft,x,y,z) ( ft.a=(x),ft.b=(y),ft.c=(z) )
310 #endif
311 
312 #ifndef TYPEDEF_float_quad
313 #define TYPEDEF_float_quad
314 typedef struct { float a,b,c,d ; } float_quad ;
315 #endif
316 
317 #ifndef TYPEDEF_float_quint
318 #define TYPEDEF_float_quint
319 typedef struct { float a,b,c,d,e ; } float_quint ;  /* 02 Nov 2015 */
320 #endif
321 
322 #ifndef TYPEDEF_float_sextet
323 #define TYPEDEF_float_sextet
324 typedef struct { float a,b,c,d,e,f ; } float_sextet ;  /* 18 Mar 2021 */
325 #endif
326 
327 #ifndef TYPEDEF_double_pair
328 #define TYPEDEF_double_pair
329 typedef struct { double a,b ; } double_pair ;
330 #endif
331 
332 #ifndef TYPEDEF_double_triple
333 #define TYPEDEF_double_triple
334 typedef struct { double a,b,c ; } double_triple ;
335 #endif
336 
337 #ifndef TYPEDEF_double_quad
338 #define TYPEDEF_double_quad
339 typedef struct { double a,b,c,d ; } double_quad ;
340 #endif
341 
342 #ifndef TYPEDEF_double_quint
343 #define TYPEDEF_double_quint
344 typedef struct { double a,b,c,d,e ; } double_quint ;
345 #endif
346 
347 /*-------*/
348 
349 /*! Triple to hold RGB bytes. */
350 
351 #ifndef TYPEDEF_rgbyte
352 #define TYPEDEF_rgbyte
353 typedef struct rgbyte { byte r,g,b ; } rgbyte ;  /* 15 Feb 1999 */
354 
355 #undef  RGBZEQ
356 #undef  RGBZAS
357 #define RGBZEQ(q) ( (q).r==0 && (q).g==0 && (q).b==0 )  /* is == (0,0,0)? */
358 #define RGBZAS(q) ( (q).r = (q).g = (q).b = 0 )         /* set = (0,0,0). */
359 #endif
360 
361 static rgbyte tEMp_rgbyte_aAa ;
362 
363 /*! Convert one RBG triple (rgbyte) to a single int. */
364 
365 #define RGBYTE_TO_INT(rgb) ( (rgb).r << 16 | (rgb).g << 8 | (rgb).b )
366 
367 /*! Convert one int to a RGB triple (rgbyte). */
368 
369 #define INT_TO_RGB(q) ( tEMp_rgbyte_aAa.r = ((q) >> 16) & 0xff , \
370                         tEMp_rgbyte_aAa.g = ((q) >>  8) & 0xff , \
371                         tEMp_rgbyte_aAa.b = (q)         & 0xff , tEMp_rgbyte_aAa )
372 
373 #define RGB_TO_FLOAT(rgb) (0.299*(rgb).r+0.587*(rgb).g+0.114*(rgb).b)
374 /*-------*/
375 
376 /** Mar 1996: Extended to images up to 7D;
377               Not all routines work with images > 2D --
378               check top of file for "7D SAFE" comments **/
379 
380 #undef USE_MRI_LABELS
381 #ifdef USE_MRI_LABELS
382 #  define MRI_LABEL_SIZE 4
383 #endif
384 
385 #define INPUT_DELAY  1
386 #define BSWAP_DELAY  2
387 #define IS_PURGED    4
388 
389 /*! Stores one image (1D to 7D).
390     Why 7D, you ask?  Well, I originally only had 2D images here.
391     When extending AFNI from 3D to 3D+time and buckets, I thought
392     that I might use 4D images (x,y,z,t) as the basic element.
393     Instead, I decided to use an array of 3D images (in a THD_datablock),
394     but by then I'd extended this typedef to allow (x,y,z,t,u,v,w) dimensioned
395     arrays.  I don't think anyplace ever uses more than 3D images, though.
396 */
397 
398 typedef struct MRI_IMAGE {
399           int nx ;            /*!< 1st dimension of image */
400           int ny ;            /*!< 2nd dimension of image (1 for 1D image) */
401           int nz  ;           /*!< 3rd dimension of image (1 for 2D image) */
402           int nt ;            /*!< 4th dimension of image (1 for 3D image) */
403           int nu ;            /*!< 5th dimension of image (1 for 4D image) */
404           int nv ;            /*!< 6th dimension of image (1 for 5D image) */
405           int nw  ;           /*!< 7th dimension of image (1 for 6D image) */
406           int nxy ;           /*!< nx*ny */
407           int64_t nxyz ;      /*!< nx*ny*nz */
408           int64_t nxyzt  ;    /*!< nx*ny*nz*nt */
409           int64_t nvox   ;    /*!< number of voxels total */
410           int pixel_size ;    /*!< bytes per pixel */
411 
412           MRI_TYPE kind ;     /*!< one of the MRI_TYPE codes above */
413           void    *im ;       /*!< pointer to actual pixel data */
414           char *name ;        /*!< string attached; may be NULL; might be filename */
415 
416           float dx ;          /*!< physical pixel size, if != 0 */
417           float dy ;          /*!< physical pixel size, if != 0 */
418           float dz ;          /*!< physical pixel size, if != 0 */
419           float dt ;          /*!< physical pixel size, if != 0 */
420           float du ;          /*!< physical pixel size, if != 0 */
421           float dv ;          /*!< physical pixel size, if != 0 */
422           float dw ;          /*!< physical pixel size, if != 0 */
423           float xo ;          /*!< spatial origin of axis */
424           float yo ;          /*!< spatial origin of axis */
425           float zo ;          /*!< spatial origin of axis */
426           float to ;          /*!< spatial origin of axis */
427           float uo ;          /*!< spatial origin of axis */
428           float vo ;          /*!< spatial origin of axis */
429           float wo ;          /*!< spatial origin of axis */
430 
431 #ifdef USE_MRI_LABELS
432          char xlab[MRI_LABEL_SIZE] ;  /*!< labels for each dimension */
433               ylab[MRI_LABEL_SIZE] ;  /*!< labels for each dimension */
434               zlab[MRI_LABEL_SIZE] ;  /*!< labels for each dimension */
435               tlab[MRI_LABEL_SIZE] ;  /*!< labels for each dimension */
436               ulab[MRI_LABEL_SIZE] ;  /*!< labels for each dimension */
437               vlab[MRI_LABEL_SIZE] ;  /*!< labels for each dimension */
438               wlab[MRI_LABEL_SIZE] ;  /*!< labels for each dimension */
439 #endif
440 
441          char *fname ;   /*!< to read actual image data after delay */
442          unsigned int foffset ;   /*!< offset into fname of image data */
443          int fondisk ;   /*!< flag to indicate if is on disk (?) */
444 
445          int was_swapped ; /* 07 Mar 2002 */
446          int vdim ;        /* 28 Nov 2008 */
447          int flags ;       /* 21 Mar 2013 */
448 
449          char *comments ;  /* 03 Aug 2016 */
450 } MRI_IMAGE ;
451 
452 #ifdef USE_MRI_LABELS
453 /*! Copy auxiliary data from one MRI_IMAGE to another. */
454 #  define MRI_COPY_AUX_OLD(nn,oo)                                       \
455     ( (nn)->dx = (oo)->dx , (nn)->dy = (oo)->dy , (nn)->dz = (oo)->dz , \
456       (nn)->dt = (oo)->dt , (nn)->du = (oo)->du , (nn)->dv = (oo)->dv , \
457       (nn)->dw = (oo)->dw ,                                             \
458       (nn)->xo = (oo)->xo , (nn)->yo = (oo)->yo , (nn)->zo = (oo)->zo , \
459       (nn)->to = (oo)->to , (nn)->uo = (oo)->uo , (nn)->vo = (oo)->vo , \
460       (nn)->wo = (oo)->wo ,                                             \
461       strcpy((nn)->xlab,(oo)->xlab) , strcpy((nn)->ylab,(oo)->ylab) ,   \
462       strcpy((nn)->zlab,(oo)->zlab) , strcpy((nn)->tlab,(oo)->tlab) ,   \
463       strcpy((nn)->ulab,(oo)->ulab) , strcpy((nn)->vlab,(oo)->vlab) ,   \
464       strcpy((nn)->wlab,(oo)->wlab) ,                                   \
465       mri_add_name( (oo)->name , (nn) ) )
466 #else
467 #  define MRI_COPY_AUX_OLD(nn,oo)                                       \
468     ( (nn)->dx = (oo)->dx , (nn)->dy = (oo)->dy , (nn)->dz = (oo)->dz , \
469       (nn)->dt = (oo)->dt , (nn)->du = (oo)->du , (nn)->dv = (oo)->dv , \
470       (nn)->dw = (oo)->dw ,                                             \
471       (nn)->xo = (oo)->xo , (nn)->yo = (oo)->yo , (nn)->zo = (oo)->zo , \
472       (nn)->to = (oo)->to , (nn)->uo = (oo)->uo , (nn)->vo = (oo)->vo , \
473       (nn)->wo = (oo)->wo ,                                             \
474       mri_add_name( (oo)->name , (nn) ) )
475 #endif
476 
477 #define MRI_COPY_AUX(nn,oo)                                                  \
478   do{ MRI_COPY_AUX_OLD(nn,oo) ;                                              \
479       if( (oo)->comments != NULL ) (nn)->comments = strdup((oo)->comments) ; \
480       else                         (nn)->comments = NULL ;                   \
481   } while(0)
482 
483 /*! Check if MRI_IMAGE is 1D (ny=1) */
484 #define MRI_IS_1D(iq)  ((iq)->ny == 1)
485 
486 /*! Check if MRI_IMAGE is 2D (nz=1) */
487 #define MRI_IS_2D(iq)  ((iq)->ny > 1 && (iq)->nz == 1)
488 
489 /*! Check if MRI_IMAGE is 3D (nt=1) */
490 #define MRI_IS_3D(iq)  ((iq)->nz > 1 && (iq)->nt == 1)
491 
492 /*! Check if MRI_IMAGE is 4D (nu=1) */
493 #define MRI_IS_4D(iq)  ((iq)->nt > 1 && (iq)->nu == 1)
494 
495 /*! Return dimensionality of MRI_IMAGE */
496 
497 #if 1
498 extern int mri_dimensionality(MRI_IMAGE *) ;     /* 12 Dec 2007 */
499 #define MRI_DIMENSIONALITY  mri_dimensionality
500 #else
501 #define MRI_DIMENSIONALITY(iq)                     \
502  ( ((iq)->ny == 1) ? 1 : ((iq)->nz == 1) ? 2 :     \
503    ((iq)->nt == 1) ? 3 : ((iq)->nu == 1) ? 4 :     \
504    ((iq)->nv == 1) ? 5 : ((iq)->nw == 1) ? 6 : 7 )
505 #endif
506 
507 #define MRI_BYTE_PTR(iq)    ((byte *)mri_data_pointer(iq))
508 #define MRI_SHORT_PTR(iq)   ((short *)mri_data_pointer(iq))
509 #define MRI_INT_PTR(iq)     ((int *)mri_data_pointer(iq))
510 #define MRI_FLOAT_PTR(iq)   ((float *)mri_data_pointer(iq))
511 #define MRI_DOUBLE_PTR(iq)  ((double *)mri_data_pointer(iq))
512 #define MRI_COMPLEX_PTR(iq) ((complex *)mri_data_pointer(iq))
513 #define MRI_RGB_PTR(iq)     ((byte *)mri_data_pointer(iq))
514 #define MRI_RGBA_PTR(iq)    ((rgba *)mri_data_pointer(iq))
515 
516 /* only used in afni.c -- don't use these generally! */
517 
518 #define MRI_BYTE_2D(iq,ix,jy)    MRI_BYTE_PTR(iq)[(ix)+(jy)*(iq)->nx]
519 #define MRI_SHORT_2D(iq,ix,jy)   MRI_SHORT_PTR(iq)[(ix)+(jy)*(iq)->nx]
520 #define MRI_INT_2D(iq,ix,jy)     MRI_INT_PTR(iq)[(ix)+(jy)*(iq)->nx]
521 #define MRI_FLOAT_2D(iq,ix,jy)   MRI_FLOAT_PTR(iq)[(ix)+(jy)*(iq)->nx]
522 #define MRI_DOUBLE_2D(iq,ix,jy)  MRI_DOUBLE_PTR(iq)[(ix)+(jy)*(iq)->nx]
523 #define MRI_COMPLEX_2D(iq,ix,jy) MRI_COMPLEX_PTR(iq)[(ix)+(jy)*(iq)->nx]
524 
525 #define FLOAT_TO_BYTE(fff) \
526   ( ((fff)<=0.0) ? (0) : ((fff)>=255.5) ? (255) : (byte)((fff)+0.49) )
527 
528 #define SHORT_TO_BYTE(fff) \
529   ( ((fff)<=0) ? (0) : ((fff)>=255) ? (255) : (byte)(fff) )
530 
531 #define FLOAT_TO_SHORT(fff) ((short)(fff))
532 
533 /*********** Type: array of MRI_IMAGE pointers ***********/
534 
535 /*! Array of MRI_IMAGE pointers. */
536 
537 typedef struct MRI_IMARR {
538       int num ;             /*!< Number of actual MRI_IMAGE here */
539       int nall ;            /*!< Size of imarr array currently allocated */
540       MRI_IMAGE **imarr ;   /*!< Array of MRI_IMAGE pointers */
541 } MRI_IMARR ;
542 
543 /*! Get the nn-th image from the image array "name". */
544 
545 #define IMAGE_IN_IMARR(name,nn) ((name)->imarr[(nn)])
546 #define IMARR_SUBIMAGE          IMAGE_IN_IMARR
547 #define IMARR_SUBIM             IMAGE_IN_IMARR
548 
549 /*! Get the number of images in the image array "name". */
550 
551 #define IMARR_COUNT(name)       ((name)->num)
552 
553 #define IMARR_LASTIM(name)      ((name)->imarr[(name)->num-1])
554 #define IMARR_FIRSTIM(name)     ((name)->imarr[0])
555 
556 #define INC_IMARR 32
557 
558 /*! Initialize an MRI_IMARR struct. */
559 
560 #define INIT_IMARR(name)                                                           \
561    do{ int iq ; (name) = (MRI_IMARR *) malloc(sizeof(MRI_IMARR)) ;                 \
562        (name)->num = 0 ; (name)->nall = INC_IMARR ;                                \
563        (name)->imarr = (MRI_IMAGE **)malloc(sizeof(MRI_IMAGE *)*INC_IMARR) ;       \
564        for( iq=(name)->num ; iq < (name)->nall ; iq++ ) (name)->imarr[iq] = NULL ; \
565        break ; } while(0)
566 
567 /*! Add one MRI_IMAGE to the MRI_IMARR struct. */
568 
569 #define ADDTO_IMARR(name,imm)                                                           \
570    do{ int nn , iq ;                                                                    \
571        if( (name)->num == (name)->nall ){                                               \
572           nn = (name)->nall = 1.1*(name)->nall + INC_IMARR ;                            \
573           (name)->imarr = (MRI_IMAGE **)realloc( (name)->imarr,sizeof(MRI_IMAGE *)*nn );\
574           for( iq=(name)->num ; iq < (name)->nall ; iq++ ) (name)->imarr[iq] = NULL ; } \
575        nn = (name)->num ; ((name)->num)++ ;                                             \
576        (name)->imarr[nn] = (imm) ; break ; } while(0)
577 
578 /*! Free the MRI_IMARR struct (but not the images within). */
579 
580 #define FREE_IMARR(name)                                                        \
581    do{ if( (name) != NULL ){                                                    \
582           free((name)->imarr); free((name)); (name) = NULL; } break; } while(0)
583 
584 /*! Free the MRI_IMARR struct, including the images within. */
585 
586 #define DESTROY_IMARR(name)                                                     \
587    do{ int nn ;                                                                 \
588        if( (name) != NULL ){                                                    \
589           for( nn=0 ; nn < (name)->num ; nn++ ) mri_free((name)->imarr[nn]) ;   \
590           free((name)->imarr); free((name)); (name) = NULL; } break; } while(0)
591 
592 /*! Free all images at-and-after [qq] in the MRI_IMARR struct. */
593 
594 #define TRUNCATE_IMARR(name,qq)                                                 \
595    do{ int nn ;                                                                 \
596        if( (name) != NULL && qq < (name)->num ){                                \
597           for( nn=qq ; nn < (name)->num ; nn++ ) mri_free((name)->imarr[nn]);   \
598           (name)->num = qq ;                                                    \
599        } } while(0)
600 
601 extern MRI_IMARR * mri_to_imarr( MRI_IMAGE *imin ) ; /* 06 Dec 2007 */
602 
603 /*----------------- Misc other types -------------------------------------*/
604 
605 typedef struct { int i,j;   } int_pair ;    /* 12 Aug 2002 */
606 typedef struct { int i,j,k; } int_triple ;
607 typedef struct { int i; float a; } intfloat ; /* 02 Jun 2014 */
608 
609 #ifndef TYPEDEF_int_sextet
610 #define TYPEDEF_int_sextet
611 typedef struct { int a,b,c,d,e,f ; } int_sextet ;  /* 18 Mar 2021 */
612 #endif
613 
614 /******* macros for complex arithmetic, using comma operator *******/
615 
616 static float   MRI_fla ;                      /* float temporaries   */
617 static complex MRI_cxa , MRI_cxb , MRI_cxc ;  /* complex temporaries */
618 
619 /*! Return a complex from two floats. */
620 
621 #define CMPLX(x,y) ( MRI_cxa.r = (x) , MRI_cxa.i = (y) , MRI_cxa )
622 
623 /*! Return complex u+v */
624 
625 #define CADD(u,v) ( MRI_cxa.r = u.r + v.r , \
626                     MRI_cxa.i = u.i + v.r , MRI_cxa )
627 
628 /*! complex u += v */
629 
630 #define CADDTO(u,v) ( u.r += v.r , u.i += v.i )
631 
632 /*! Return complex u-v */
633 #define CSUB(u,v) ( MRI_cxa.r = u.r - v.r , \
634                     MRI_cxa.i = u.i - v.i , MRI_cxa )
635 
636 /*! complex u -= v */
637 
638 #define CSUBFROM(u,v) ( u.r -= v.r , u.i -= v.i )
639 
640 /*! Return complex u*v */
641 
642 #define CMULT(u,v) ( MRI_cxb.r = u.r * v.r - u.i * v.i , \
643                      MRI_cxb.i = u.r * v.i + u.i * v.r , MRI_cxb )
644 
645 /*! complex u *= v */
646 
647 #define CMULTBY(u,v) ( MRI_fla = u.r * v.r - u.i * v.i , \
648                        u.i     = u.r * v.i + u.i * v.r , u.r = MRI_fla )
649 
650 /*! Return complex u * conjg(v) */
651 
652 #define CJMULT(u,v) ( MRI_cxb.r = u.r * v.r + u.i * v.i , \
653                       MRI_cxb.i = u.i * v.r - u.r * v.i , MRI_cxb )
654 
655 /*! complex u *= conjg(v) */
656 
657 #define CJMULTBY(u,v) ( MRI_fla = u.r * v.r + u.i * v.i , \
658                         u.i     = u.i * v.r - u.r * v.i , u.r = MRI_fla )
659 
660 /*! complex w += u*v */
661 
662 #define CMADD(u,v,w) ( w.r += u.r * v.r - u.i * v.i , \
663                        w.i += u.r * v.i + u.i * v.r    )
664 
665 /*! Return complex exp(I*t) */
666 
667 #define CEXPIT(t)   ( MRI_cxc.r = cos(t) , MRI_cxc.i = sin(t) , MRI_cxc )
668 
669 /**** macros ****/
670 
671 static int MRI_mm ;
672 
673 /*! Median of 3. */
674 
675 #define MEDIAN(a,b,c) ( MRI_mm = 4*((a)<(b)) + 2*((a)<(c)) + ((b)<(c)) , \
676                         (MRI_mm==3||MRI_mm==4) ? (a) :                   \
677                         (MRI_mm==7||MRI_mm==0) ? (b) : (c) )
678 
679 #define ABS(a) ( (a) < 0 ? (-(a)):(a) )
680 #define SIGN(a) ( (a) < 0 ? -1:1 )
681 
682 /*! Order-statistic filter of 3. */
683 
684 #define OSFSUM(p,q,r) (0.60*(p)+0.20*((q)+(r)))
685 
686 /*! Order-statistic filter of 3. */
687 
688 #define OSFILT(a,b,c) ( MRI_mm = 4*((a)<(b)) + 2*((a)<(c)) + ((b)<(c)) , \
689                         (MRI_mm==3||MRI_mm==4) ? OSFSUM(a,b,c) :         \
690                         (MRI_mm==7||MRI_mm==0) ? OSFSUM(b,a,c) : OSFSUM(c,a,b) )
691 
692 #ifndef TRUE
693 #   define TRUE  (1)
694 #endif
695 
696 #ifndef FALSE
697 #   define FALSE (0)
698 #endif
699 
700 #ifndef MAYBE
701 #   define MAYBE (-1)  /* 26 Feb 2010 */
702 #endif
703 
704 #define MRI_BILINEAR  (1)   /* for the warping function */
705 #define MRI_LINEAR    (1)
706 #define MRI_BICUBIC   (2)
707 #define MRI_CUBIC     (2)
708 #define MRI_FOURIER   (3)
709 #define MRI_NN        (0)
710 #define MRI_QUINTIC   (4)   /* Nov 1998 */
711 #define MRI_HEPTIC    (5)
712 #define MRI_TSSHIFT   (6)   /* Dec 1999 */
713 
714 #define MRI_VARP1    (71)   /* 24 Dec 2008 */
715 #define MRI_WSINC5   (72)   /* 02 Jan 2009 */
716 #define MRI_WSINC9   (79)   /* 19 Aug 2019 - for 1D shifting only */
717 
718 #define MRI_FOURIER_NOPAD (66)  /* 13 May 2003 */
719 
720 #define MRI_HIGHORDER(x) ((x) != MRI_NN && (x) != MRI_LINEAR)
721 
722 #define SQR(x)   ((x)*(x))
723 #define CSQR(z)  (SQR(z.r)+SQR(z.i))
724 #define CABS(z)  complex_abs(z)
725 #define CARG(z)  ( ((z).r!=0.0 || (z).i!=0.0) ? atan2f((z).i,(z).r) : 0.0 )
726 
727 /*! complex z /= abs(z) */
728 
729 #define CUNITIZE(z) ( MRI_fla=CABS(z) , z.r=z.r/MRI_fla , z.i=z.i/MRI_fla )
730 
731 #ifdef MRI_DEBUG
732 #  define WHOAMI fprintf(stderr,"in file: %s at line %d\n",__FILE__,__LINE__);
733 #else
734 #  define WHOAMI
735 #endif
736 
737 #ifdef MRI_DEBUG
738 #  define IMHEADER(f) \
739           fprintf(stderr,"%s: nx=%d ny=%d kind=%d\n",#f,f->nx,f->ny,f->kind);
740 #else
741 #  define IMHEADER(f)
742 #endif
743 
744 #define MRI_FATAL_ERROR \
745         {fprintf(stderr,"in file: %s at line %d\n",__FILE__,__LINE__);EXIT(1);}
746 
747 #undef  NULL_CHECK
748 #define NULL_CHECK(p) \
749  do{ if( (p)==NULL ){ ERROR_message("malloc failure: out of RAM?"); EXIT(1); } } while(0)
750 
751 /**** prototypes ****/
752 
753 #ifndef MRILIB_MINI
754 
755 extern void        mri_input_delay( MRI_IMAGE * ) ;
756 extern void        mri_purge_delay( MRI_IMAGE * ) ;
757 extern void        mri_add_fname_delay( char * , MRI_IMAGE * ) ;
758 extern MRI_IMARR * mri_read_file_delay( char * ) ;
759 extern MRI_IMARR * mri_read_3D_delay( char * ) ;
760 
761 extern void   mri_purge    ( MRI_IMAGE * ) ;  /* 20 Dec 2006 */
762 extern void   mri_unpurge  ( MRI_IMAGE * ) ;
763 extern void   mri_killpurge( MRI_IMAGE * ) ;
764 extern char * mri_purge_get_tmpdir(void) ;    /* 21 Dec 2006 */
765 extern char * mri_purge_get_tsuf(void) ;      /* 02 Aug 2007 */
766 extern char * mri_get_tempfilename( char * ); /* 27 Jul 2009 */
767 
768 #else
769 
770 # define mri_killpurge(x)         /*nada*/
771 # define mri_purge(x)             /*nada*/
772 # define mri_unpurge(x)           /*nada*/
773 # define mri_get_tempfilename(x)  NULL
774 # define mri_input_delay(x)       /*nada*/
775 # define mri_purge_delay(x)       /*nada*/
776 # define mri_add_fname_delay(x,y) /*nada*/
777 # define mri_read_file_delay(x)   NULL
778 # define mri_read_3D_delay(x)     NULL
779 
780 #endif /* MRILIB_MINI */
781 
782 extern int mri_counter( MRI_IMAGE * , float , float ) ; /* 16 Jul 2007 */
783 
784 #define MRI_IS_PURGED(iq) \
785   ( (iq)!=NULL && (iq)->fondisk==IS_PURGED && (iq)->fname!=NULL )
786 
787 #define MRI_HAS_DATA(iq)                                    \
788   ( (iq)!= NULL &&                                          \
789     ( ( (iq)->fondisk==IS_PURGED && (iq)->fname!=NULL ) ||  \
790       (iq)->im != NULL                                 )  )
791 
792 extern int mri_equal( MRI_IMAGE *, MRI_IMAGE * ) ; /* 30 Jun 2003 */
793 
794 extern MRI_IMARR * mri_read_analyze75( char * ) ;  /* 05 Feb 2001 */
795 extern MRI_IMARR * mri_read_siemens( char * ) ;    /* 12 Mar 2001 */
796 extern MRI_IMARR * mri_read3D_analyze75( char * ); /* 26 Aug 2002 */
797 
798 extern MRI_IMAGE * mri_read_stuff( char * ) ;      /* 22 Nov 2002 */
799 extern void        mri_inflate_pbm( MRI_IMAGE * ); /* 02 Jan 2002 */
800 
801 extern void mri_add_name( char * , MRI_IMAGE * ) ;
802 
803 extern MRI_IMAGE ** mri_stat_seq( MRI_IMAGE * ) ;
804 
805 extern MRI_IMAGE * mri_extract_from_mask( MRI_IMAGE *, byte *, int ) ;
806 extern void binarize_mask( int , byte * ) ;
807 
808 /*--------------------------------*/
809 
810 #define NSTAT_MEAN        0
811 #define NSTAT_SUM         1
812 #define NSTAT_SIGMA       2
813 #define NSTAT_CVAR        3
814 #define NSTAT_MEDIAN      4
815 #define NSTAT_MAD         5
816 #define NSTAT_MAX         6
817 #define NSTAT_MIN         7
818 #define NSTAT_MODE        8       /* DRG 05/21/2015 */
819 #define NSTAT_NZMODE      9       /* DRG 05/21/2015 */
820 #define NSTAT_ABSMAX      13
821 #define NSTAT_VAR         17
822 #define NSTAT_NUM         18
823 #define NSTAT_PERCENTILE  19
824 #define NSTAT_RANK        21      /* ZSS Jan 10 */
825 #define NSTAT_FRANK       22      /* ZSS Jan 10 */
826 #define NSTAT_P2SKEW      23      /* ZSS March 04 10*/
827 #define NSTAT_KURT        24      /* ZSS Jan   04 11*/
828 #define NSTAT_mMP2s0      25
829 #define NSTAT_mMP2s1      26
830 #define NSTAT_mMP2s2      27
831 #define NSTAT_mmMP2s0     28
832 #define NSTAT_mmMP2s1     29
833 #define NSTAT_mmMP2s2     30
834 #define NSTAT_mmMP2s3     31
835 #define NSTAT_NZNUM       32
836 #define NSTAT_FNZNUM      33
837 #define NSTAT_diffs0      34
838 #define NSTAT_diffs1      35
839 #define NSTAT_diffs2      36
840 #define NSTAT_adiffs0     37
841 #define NSTAT_adiffs1     38
842 #define NSTAT_adiffs2     39
843 #define NSTAT_LIST        40
844 #define NSTAT_HIST        41
845 #define NSTAT_FILLED      42
846 #define NSTAT_UNFILLED    43
847 #define NSTAT_MASKED      44
848 #define NSTAT_MASKED2     45
849 
850 #define NSTAT_FWHMx      63   /*these should be after all other NSTAT_* values */
851 #define NSTAT_FWHMy      64
852 #define NSTAT_FWHMz      65
853 #define NSTAT_FWHMbar    66
854 #define NSTAT_FWHMbar12  67
855 
856 #define NBISTAT_BASE               66601
857 #define NBISTAT_SPEARMAN_CORR      66601
858 #define NBISTAT_QUADRANT_CORR      66602
859 #define NBISTAT_PEARSON_CORR       66603
860 #define NBISTAT_MUTUAL_INFO        66604
861 #define NBISTAT_NORMUT_INFO        66605
862 #define NBISTAT_JOINT_ENTROPY      66606
863 #define NBISTAT_HELLINGER          66607
864 #define NBISTAT_CORR_RATIO_M       66608
865 #define NBISTAT_CORR_RATIO_A       66609
866 #define NBISTAT_CORR_RATIO_U       66610
867 #define NBISTAT_NUM                66611
868 #define NBISTAT_NCD                66612
869 #define NBISTAT_KENDALL_TAUB       66613 /* 29 Apr 2010 */
870 #define NBISTAT_TICTACTOE_CORR     66614 /* 30 Mar 2011 */
871 #define NBISTAT_L2SLOPE            66615 /* 26 Apr 2012 */
872 #define NBISTAT_L1SLOPE            66616 /* 26 Apr 2012 */
873 #define NBISTAT_QUANTILE_CORR      66617 /* 11 May 2012 */
874 
875 #define NBISTAT_BC_PEARSON_M       66691
876 #define NBISTAT_BC_PEARSON_V       66692
877 
878 #define NBISTAT_EUCLIDIAN_DIST     66693 /* 4 May 2012, ZSS */
879 #define NBISTAT_CITYBLOCK_DIST     66694 /* 4 May 2012, ZSS */
880 
881 
882 extern float mri_nbistat( int , MRI_IMAGE *, MRI_IMAGE * ) ; /* 26 Oct 2006 */
883 extern void mri_nbistat_setclip( float, float , float, float ) ;
884 extern void mri_bistat_setweight( MRI_IMAGE *wm ) ;  /* 14 Aug 2007 */
885 extern void set_mri_nstat_fillvalue(float tf);
886 extern void set_mri_nstat_unfillvalue(float tf);
887 extern void set_mri_nstat_maskvalue(float tf);
888 extern void set_mri_nstat_maskvalue2(float tf);
889 
890 extern MRI_IMAGE * mri_edit_image( float pthr, float power, MRI_IMAGE * im ) ;
891 
892 extern MRI_IMARR * mri_read_mpeg( char * ) ;    /* 03 Dec 2003 */
893 extern int         mri_isgray( MRI_IMAGE * ) ;
894 extern int         mri_imcount_mpeg( char * ) ;
895 
896 extern void cfft( int , int , float * , float * ) ;
897 extern void cfft2d_cox( int , int , int , float * , float * ) ;
898 extern void csfft_cox( int,int , complex * ) ;
899 extern void csfft_many( int,int,int , complex * ) ;
900 extern int  csfft_nextup(int) ;
901 extern int csfft_nextup_one35(int) ;
902 extern int csfft_nextup_even(int) ;
903 extern void csfft_scale_inverse(int) ;
904 extern void csfft_force_fftn(int) ; /* 08 Oct 2017 */
905 extern int csfft_allows_anything(void) ; /* Jun 2018 */
906 
907 extern void mri_fftshift( MRI_IMAGE *, float,float,float, int ) ; /* 13 May 2003 */
908 
909 extern void * mri_data_pointer(MRI_IMAGE *) ;
910 extern void mri_free( MRI_IMAGE * ) ;
911 extern void mri_clear( MRI_IMAGE * ) ;  /* 31 Jan 2007 */
912 extern void mri_fix_data_pointer( void * , MRI_IMAGE * ) ;
913 #define mri_set_data_pointer(iq,pt) mri_fix_data_pointer((pt),(iq))
914 
915 #define MRI_FREE(iq) do{ mri_free(iq); (iq)=NULL; } while(0)
916 
917 extern char * mri_dicom_header( char * ) ;  /* 15 Jul 2002 */
918 extern void   mri_dicom_pxlarr( off_t *, unsigned int * ) ;
919 extern void   mri_dicom_noname( int ) ;
920 extern void   mri_dicom_nohex ( int ) ;
921 extern void   mri_dicom_setvm ( int ) ;     /* 28 Oct 2002 */
922 extern void   mri_dicom_seterr( int ) ;     /* 05 Nov 2002 */
923 extern void   mri_dicom_header_use_printf( int ) ; /* 02 May 2008 */
924 extern void   mri_dicom_header_show_size_offset( int ); /* 17 Oct 2012 [rcr] */
925 
926 extern MRI_IMARR * mri_read_dicom( char * )  ;
927 extern int         mri_imcount_dicom( char * ) ;
928 extern char *      mri_dicom_sexinfo( void ) ;   /* 23 Dec 2002 */
929 extern char *      mri_dicom_sex1010( void ) ;
930 extern int         mri_possibly_dicom( char * ) ;        /* 07 May 2003 */
931 extern int         mri_siemens_slice_times( int *, int *, float ** );
932 extern int         mri_sst_get_verb( void );
933 extern int         mri_sst_set_verb( int );
934 extern char *      mri_dicom_hdrinfo( char *fname, int natt, char **att ,
935                                       int nposn, char *sepstr) ;
936 extern char *      mri_dicom_hdrinfo_full( char *fname, int natt, char **att ,
937                                       int nposn, char *sepstr ) ;
938 
939 /*! Set the data pointer in an MRI_IMAGE to NULL. */
940 
941 #define mri_clear_data_pointer(iq) mri_fix_data_pointer(NULL,(iq))
942 
943 /*! Clear the data pointer and free the MRI_IMAGE shell */
944 
945 #define mri_clear_and_free(iq) \
946  do{ mri_fix_data_pointer(NULL,(iq)); mri_free((iq)); } while(0)
947 
948 /*! Set all pixels in MRI_IMAGE to zero. */
949 
950 #define mri_zero_image(iq) \
951    memset(mri_data_pointer(iq),0,(iq)->nvox*(iq)->pixel_size)
952 
953 extern int mri_allzero( MRI_IMAGE *im ) ;  /* check if all pixels are 0 */
954 extern int mri_nonzero_count( MRI_IMAGE *im ) ;                   /* 28 Dec 2015 */
955 extern int mri_nonzero_count_inmask( MRI_IMAGE *im, byte *mmm ) ; /* 12 Dec 2019 */
956 
957 extern MRI_IMAGE * mri_zeropad_3D( int,int,int,int,int,int , MRI_IMAGE * ) ;
958 extern MRI_IMAGE * mri_valpad_2D( int,int,int,int, MRI_IMAGE *, byte val ) ;
959 extern MRI_IMAGE * mri_zeropad_2D( int,int,int,int, MRI_IMAGE * ) ;
960 
961 extern double mri_max( MRI_IMAGE * ) ;
962 extern double mri_min( MRI_IMAGE * ) ;
963 extern double mri_maxabs( MRI_IMAGE * ) ;
964 extern double_pair mri_minmax   ( MRI_IMAGE *im ) ;  /* Apr 2013 */
965 extern double_pair mri_minmax_nz( MRI_IMAGE *im ) ;  /* Apr 2013 */
966 
967 intfloat mri_indmax_nz( MRI_IMAGE * ) ;  /* 02 Jun 2014 */
968 intfloat mri_indmin_nz( MRI_IMAGE * ) ;
969 
970 extern MRI_IMAGE * mri_cut_2D( MRI_IMAGE * , int,int,int,int ) ;
971 extern int mri_cut_many_2D(MRI_IMARR *,  int,int,int,int );
972 
973 extern MRI_IMAGE * mri_subset_x2D( int , int * , MRI_IMAGE * ) ;
974 
975 extern MRI_IMAGE * mri_check_2D( int , MRI_IMAGE * , MRI_IMAGE * ) ;
976 
977 #define WIPER_FROM_LEFT   1
978 #define WIPER_FROM_BOTTOM 2
979 #define WIPER_FROM_CENTER 3
980 
981 extern MRI_IMAGE * mri_wiper_2D( int wcode,float wfac, MRI_IMAGE *ima, MRI_IMAGE *imb ) ;
982 extern MRI_IMAGE * mri_mix_2D( float , MRI_IMAGE * , MRI_IMAGE * ) ;
983 
984 extern MRI_IMAGE * mri_cut_3D( MRI_IMAGE * , int,int,int,int,int,int ) ;
985 
986 /** 15 Apr 1999 **/
987 
988 extern void upsample_7( int , int , float * , float * ) ;
989 extern void upsample_1( int , int , float * , float * ) ; /* 12 Mar 2002 */
990 extern MRI_IMAGE * mri_dup2D( int , MRI_IMAGE * ) ;
991 extern void        mri_dup2D_mode( int ) ;                /* 12 Mar 2002 */
992 
993 extern void mri_move_guts( MRI_IMAGE *, MRI_IMAGE * ) ;  /* 28 Mar 2002 */
994 extern MRI_IMAGE * mri_copy( MRI_IMAGE * ) ;             /* 17 Apr 2000 */
995 extern MRI_IMAGE * mri_expand_2D( int , MRI_IMAGE * ) ;  /* 22 Feb 2004 */
996 extern MRI_IMAGE *mri_new( int , int , MRI_TYPE ) ;
997 extern MRI_IMAGE *mri_read( char * ) ;
998 extern MRI_IMAGE *mri_read_ge4( char * ) ;               /* 03 Jun 2003 */
999 
1000 extern void   fclose_maybe( FILE *fp ) ;
1001 extern FILE * fopen_maybe ( char *fname ) ;
1002 
1003 extern int mri_write( char * , MRI_IMAGE * ) ;
1004 extern int mri_write_pnm( char * , MRI_IMAGE * ) ;
1005 extern int mri_write_jpg( char * , MRI_IMAGE * ) ;       /* 15 Apr 2005 */
1006 extern int mri_write_png( char * , MRI_IMAGE * ) ;       /* 11 Dec 2006 */
1007 extern int mri_write_filtered( char * , MRI_IMAGE * ) ;  /* 15 Dec 2006 */
1008 extern int mri_write_7D( char * , MRI_IMAGE * ) ;
1009 extern int mri_datum_size( MRI_TYPE typ ) ;
1010 extern MRI_IMAGE *mri_read_ascii( char * ) ;
1011 extern MRI_IMAGE *mri_read_double_ascii( char * ) ;
1012 extern MRI_IMAGE *mri_read_complex_ascii( char * ) ;
1013 extern MRI_IMAGE *mri_read_ascii_ragged(char *, float) ; /* 28 Jul 2004 */
1014 extern int mri_write_ascii( char * , MRI_IMAGE * ) ;
1015 extern int mri_write_raw( char * , MRI_IMAGE * ) ;       /* 05 Jan 2000 */
1016 extern void mri_write_analyze( char * , MRI_IMAGE * ) ;  /* 29 Nov 2001 */
1017 extern char * mri_1D_tostring( MRI_IMAGE *im ) ;          /* 15 Nov 2007 */
1018 
1019 extern void mri_adjust_fvectim( MRI_IMAGE *im, int vdim ) ;  /* 28 Nov 2008 */
1020 extern MRI_IMAGE * mri_new_fvectim( int nx, int ny, int nz, int vdim ) ;
1021 
1022 extern MRI_IMAGE * mri_read_ascii_ragged_complex(char *,float); /* 08 Mar 2007 */
1023 
1024 extern MRI_IMAGE * mri_read_ascii_ragged_fvect( char *, float, int ) ;
1025 extern MRI_IMARR * mri_fvect_to_imarr( MRI_IMAGE *inim ) ;
1026 extern MRI_IMAGE * mri_imarr_to_fvect( MRI_IMARR *imar ) ;
1027 extern MRI_IMAGE * mri_float_arrays_to_image(float **vecs, int vec_len,
1028                                              int vec_num); /* 16 Apr 2014 */
1029 extern MRI_IMAGE * mri_pair_to_fvect( MRI_IMAGE *, MRI_IMAGE * ) ;
1030 extern MRI_IMAGE * mri_triple_to_fvect( MRI_IMAGE *, MRI_IMAGE *, MRI_IMAGE *) ;
1031 extern MRI_IMAGE * mri_fvect_subimage( MRI_IMAGE *inim , int kk ) ;
1032 
1033 extern MRI_IMAGE * mri_read_ragged_fromstring( char *, float); /* 05 Jan 2007 */
1034 
1035 extern MRI_IMAGE * mri_read_1D( char * ) ;               /* 16 Nov 1999 */
1036 extern MRI_IMAGE * mri_read_double_1D( char * ) ;
1037 extern MRI_IMAGE * mri_read_complex_1D( char * ) ;
1038 extern int mri_write_1D( char * , MRI_IMAGE * ) ;        /* 16 Nov 1999 */
1039 extern MRI_IMAGE * mri_read_1D_stdin(void) ;             /* 25 Jan 2008 */
1040 extern MRI_IMAGE * mri_read_1D_pipe (FILE *fp) ;         /* 26 Aug 2019 */
1041 extern MRI_IMAGE * mri_copy_1D_stdin(void) ;             /* 05 Mar 2010 */
1042 extern void        mri_clear_1D_stdin(void);
1043 extern char * mri_read_1D_headerlines( char * ) ;        /* 05 Dec 2010 */
1044 
1045 extern MRI_IMAGE * mri_read_4x4AffXfrm_1D( char *fname );/* 24 Nov 2009 */
1046 extern MRI_IMAGE * mri_1D_fromstring( char * ) ;         /* 28 Apr 2003 */
1047 
1048 extern int setup_mri_write_angif( void ) ;               /* 28 Jun 2001 */
1049 extern int mri_write_angif( char *, MRI_IMARR * ) ;
1050 extern MRI_IMAGE * mri_colorsetup( int,int,int,int ) ;   /* 05 Oct 2004 */
1051 
1052 extern MRI_IMAGE *mri_new_vol      ( int,int,int , MRI_TYPE ) ;
1053 extern MRI_IMAGE *mri_new_vol_empty( int,int,int , MRI_TYPE ) ;
1054 
1055 #define mri_new_empty(aa,bb,tt) mri_new_vol_empty((aa),(bb),1,(MRI_TYPE)(tt))
1056 
1057 MRI_IMAGE *mri_new_7D_generic( int nx, int ny, int nz, int nt,
1058                                int nu, int nv, int nw,
1059                                MRI_TYPE kind , int make_space ) ;
1060 
1061 /*! Create new MRI_IMAGE of type kk, with same dimensions as iq. */
1062 
1063 #define mri_new_conforming(iq,kk)                                   \
1064    mri_new_7D_generic( (iq)->nx, (iq)->ny, (iq)->nz , (iq)->nt ,    \
1065                        (iq)->nu, (iq)->nv, (iq)->nw , (kk) , TRUE )
1066 
1067 /*! Create new MRI_IMAGE of type kk, with same dimensions as iq,
1068     and with no data space allocated. */
1069 
1070 #define mri_empty_conforming(iq,kk)                                 \
1071    mri_new_7D_generic( (iq)->nx, (iq)->ny, (iq)->nz , (iq)->nt ,    \
1072                        (iq)->nu, (iq)->nv, (iq)->nw , (kk) , FALSE )
1073 
1074 extern MRI_IMARR * mri_read_3D( char * ) ;
1075 extern MRI_IMARR * mri_read_3A( char * ) ;
1076 extern MRI_IMARR * mri_read_file( char * ) ;
1077 extern int mri_imcount( char * ) ;
1078 extern MRI_IMARR * mri_read_many_files( int nf , char * fn[] ) ;
1079 extern MRI_IMARR * mri_read_resamp_many_files( int nf, char * fn[] ,
1080                                                int nxnew, int nynew, byte pval);
1081 
1082 /** returns array of byte images: red, green, blue **/
1083 
1084 extern MRI_IMARR * mri_read_ppm3( char * fname ) ;
1085 extern MRI_IMAGE * mri_read_ppm( char * fname ) ;
1086 
1087 extern void mri_read_ppm_header( char *, int *, int *) ; /* 17 Sep 2001 */
1088 
1089 MRI_IMAGE *mri_read_just_one( char * fname ) ;
1090 MRI_IMAGE *mri_read_nsize( char * fname ) ;
1091 MRI_IMARR *mri_read_many_nsize( int nf , char * fn[] ) ;
1092 
1093 void init_MCW_sizes(void) ;
1094 char * imsized_fname( char * fname ) ;
1095 char * my_strdup( char * str ) ;
1096 
1097 #define mri_filesize THD_filesize  /* 22 Mar 2007 */
1098 
1099 extern void mri_overlay_2D( MRI_IMAGE *, MRI_IMAGE *, int,int ) ;
1100 
1101 extern void mri_swapbytes( MRI_IMAGE * ) ;
1102 
1103 extern void swap_twobytes  ( int n , void * ar ) ;  /* 14 Sep 1998 */
1104 extern void swap_fourbytes ( int n , void * ar ) ;
1105 extern void swap_eightbytes( int n , void * ar ) ;  /* 06 Feb 2003 */
1106 
1107 extern MRI_IMAGE *mri_to_float( MRI_IMAGE * ) ;
1108 extern MRI_IMAGE *mri_to_short( double , MRI_IMAGE * ) ;
1109 extern MRI_IMAGE *mri_to_short_scl( double,double , MRI_IMAGE * ) ;
1110 extern MRI_IMAGE *mri_to_short_sclip( double,double , int,int , MRI_IMAGE * ) ;
1111 extern MRI_IMAGE *mri_to_complex( MRI_IMAGE * ) ;
1112 extern MRI_IMAGE *mri_to_byte( MRI_IMAGE * ) ;
1113 extern byte      *mri_to_bytemask( MRI_IMAGE *, float,float ) ;
1114 extern MRI_IMAGE *mri_to_byte_scl( double , double , MRI_IMAGE * ) ;
1115 
1116 extern MRI_IMAGE * mri_to_rgb( MRI_IMAGE * ) ;
1117 extern MRI_IMAGE * mri_3to_rgb( MRI_IMAGE * , MRI_IMAGE * , MRI_IMAGE * ) ;
1118 extern MRI_IMARR * mri_rgb_to_3float( MRI_IMAGE * ) ;
1119 extern MRI_IMARR * mri_rgb_to_3byte( MRI_IMAGE * ) ;
1120 extern MRI_IMAGE * mri_sharpen_rgb( float , MRI_IMAGE * ) ;
1121 extern MRI_IMAGE * mri_flatten_rgb( MRI_IMAGE * ) ;
1122 extern void mri_invert_inplace( MRI_IMAGE *) ;   /* 07 Apr 2003 */
1123 extern void mri_gamma_rgb_inplace( float gam , MRI_IMAGE *im ) ;
1124 extern void mri_invertcontrast_inplace( MRI_IMAGE *im , float uperc , byte *mask ) ;
1125 
1126 extern MRI_IMAGE * mri_4to_rgba( MRI_IMAGE *rim , MRI_IMAGE *gim , MRI_IMAGE *bim , MRI_IMAGE *aim ) ;
1127 extern MRI_IMARR * mri_rgba_to_4float( MRI_IMAGE *oldim ) ;
1128 extern MRI_IMARR * mri_rgba_to_4byte( MRI_IMAGE *oldim ) ;
1129 
1130 extern void mri_sharpen3D_pos( MRI_IMAGE *im , float phi ) ; /* 13 Feb 2017 */
1131 
1132 extern MRI_IMAGE * mri_median21( MRI_IMAGE *innim ) ; /* 28 Oct 2014 */
1133 extern MRI_IMAGE * mri_sharpness( MRI_IMAGE *inim ) ;
1134 
1135 extern MRI_IMAGE * mri_make_rainbow( int, int, int, rgbyte * ) ;
1136 
1137 extern MRI_IMAGE * mri_to_rgba( MRI_IMAGE * ) ;  /* 20 Mar 2002 */
1138 
1139 extern MRI_IMAGE *mri_pair_to_complex( MRI_IMAGE * , MRI_IMAGE * ) ;
1140 extern MRI_IMARR *mri_complex_to_pair( MRI_IMAGE * ) ;
1141 extern float complex_abs( complex z ) ;             /* 24 Aug 2009 */
1142 MRI_IMAGE * mri_complex_to_real( MRI_IMAGE *cim ) ; /* 17 Sep 2020 */
1143 MRI_IMAGE * mri_complex_to_imag( MRI_IMAGE *cim ) ;
1144 
1145 extern MRI_IMAGE *mri_to_complex_ext( MRI_IMAGE * , int , int , int ) ;
1146 
1147 extern MRI_IMAGE *mri_scale_to_float( float , MRI_IMAGE * ) ;
1148 extern void mri_threshold( double , double , MRI_IMAGE * , MRI_IMAGE * ) ;
1149 extern MRI_IMAGE * mri_mult_to_float( float * , MRI_IMAGE * ) ;
1150 
1151 extern float_pair mri_threshold_minmax( double,double, MRI_IMAGE *, MRI_IMAGE * ) ;
1152 extern int_pair   mri_threshold_minmax_indexes(void) ;
1153 
1154 extern void mri_maskify( MRI_IMAGE *im , byte *mask ) ; /* Jul 2010 */
1155 
1156 extern MRI_IMAGE * mri_scalize( MRI_IMAGE *, int, float * ) ; /* 20 Oct 2003 */
1157 
1158 extern MRI_IMAGE *mri_multiply_complex( int , MRI_IMAGE * , MRI_IMAGE * ) ;
1159 extern MRI_IMAGE *mri_complex_phase( MRI_IMAGE * ) ;
1160 
1161 extern MRI_IMAGE *mri_complex_imag( MRI_IMAGE *im ) ;  /* 18 Apr 2011 */
1162 extern MRI_IMAGE *mri_complex_real( MRI_IMAGE *im ) ;
1163 
1164 extern MRI_IMAGE *mri_to_mri( int , MRI_IMAGE * ) ;
1165 extern MRI_IMAGE *mri_to_mri_scl( int , double , MRI_IMAGE * ) ;
1166 extern MRI_IMAGE *mri_complex_abs( MRI_IMAGE * ) ;
1167 
1168 extern void mri_fft_complex( int , float , MRI_IMAGE * ) ;
1169 extern float *mri_setup_taper( int , float ) ;
1170 extern MRI_IMAGE * mri_fft_3D( int Sign, MRI_IMAGE *inim,
1171                                int Lxx,int Lyy,int Lzz, int alt ) ;
1172 extern MRI_IMAGE * mri_fft_3Dconvolve( MRI_IMAGE *aim , MRI_IMAGE *bim ) ;
1173 
1174 extern MRI_IMAGE *mri_warp( MRI_IMAGE * , int , int , int ,
1175                             void func(float,float,float *,float *) ) ;
1176 
1177 extern MRI_IMAGE *mri_warp_bicubic( MRI_IMAGE * , int , int ,
1178                                     void func(float,float,float *,float *) ) ;
1179 
1180 extern MRI_IMAGE *mri_warp_bilinear( MRI_IMAGE * , int , int ,
1181                                      void func(float,float,float *,float *) ) ;
1182 
1183 #undef WARP_POINT_ROUTINES
1184 #ifdef WARP_POINT_ROUTINES
1185 extern float mri_warp_bicubic_point( MRI_IMAGE * , int , int ,
1186                                      void func( float,float,float *,float *) ) ;
1187 
1188 extern float mri_rotate_point( MRI_IMAGE *im, float,float,float,float, int,int ) ;
1189 #endif /* WARP_POINT_ROUTINES */
1190 
1191 extern void mri_warp_setpow( float gg ) ;  /* 15 Jan 2007 */
1192 
1193 extern MRI_IMAGE *mri_resize( MRI_IMAGE * , int , int ) ;
1194 
1195 extern MRI_IMAGE *mri_resize_NN( MRI_IMAGE *, int , int ) ;  /* 08 Jun 2004 */
1196 extern MRI_IMAGE *mri_squareaspect( MRI_IMAGE * ) ;
1197 
1198 extern MRI_IMAGE *mri_rotate         ( MRI_IMAGE * , float,float,float,float ) ;
1199 extern MRI_IMAGE *mri_rotate_bilinear( MRI_IMAGE * , float,float,float,float ) ;
1200 
1201 extern MRI_IMAGE *mri_rota         ( MRI_IMAGE * , float,float,float ) ;
1202 extern MRI_IMAGE *mri_rota_bilinear( MRI_IMAGE * , float,float,float ) ;
1203 extern MRI_IMAGE *mri_rota_shear   ( MRI_IMAGE * , float,float,float ) ;
1204 extern MRI_IMAGE *mri_rota_variable( int, MRI_IMAGE * , float,float,float ) ;
1205 
1206 extern MRI_IMAGE *mri_aff2d_byte( MRI_IMAGE *,int,float,float,float,float) ;
1207 extern MRI_IMAGE *mri_aff2d_rgb ( MRI_IMAGE *,int,float,float,float,float) ;
1208 
1209 /** 27 Nov 2001: mri_scale.c **/
1210 
1211 extern void mri_scale_inplace( float , MRI_IMAGE * ) ;
1212 
1213 extern void ft_shift2( int, int, float, float *, float, float * ) ;
1214 
1215 extern MRI_IMAGE *mri_float_func( int,int,
1216                                   float , float , float , float ,
1217                                   float (*func)(float,float) ) ;
1218 
1219 extern void mri_histogram( MRI_IMAGE * , float,float ,
1220                                          int,int, int h[] ) ;
1221 
1222 extern void mri_histobyte        ( MRI_IMAGE * , int * ) ;
1223 extern void mri_histoshort_all   ( MRI_IMAGE * , int * ) ;    /* 25 Jul 2001 */
1224 extern void mri_histoshort_nonneg( MRI_IMAGE * , int * ) ;
1225 
1226 extern void mri_percents( MRI_IMAGE * , int nper , float per[] ) ;
1227 extern MRI_IMAGE * mri_flatten( MRI_IMAGE * ) ;
1228 extern void mri_flatten_set_bfac(float b) ;                   /* 16 Mar 2017 */
1229 extern float mri_quantile( MRI_IMAGE * im , float alpha ) ;
1230 float percentile_nzabs( int nvox , float *far , float per ) ; /* 24 May 2019 */
1231 
1232 extern float_pair mri_twoquantiles( MRI_IMAGE * im, float alpha, float beta ) ;
1233 
1234 extern void qsort_short( int , short * ) ;
1235 extern void qsort_float( int , float * ) ;
1236 extern void qsort_float_rev( int , float * ) ;
1237 
1238 extern void qsort_pair( int , float * , int * ) ;
1239 extern void qsort_pair_rev( int , float * , int * ) ; /* 08 Mar 2019 */
1240 extern void qsort_pairX( int , float * , int * ) ;    /* 08 Mar 2019 */
1241 
1242 extern void qsort_int( int , int * ) ;
1243 extern void qsort_int_mostly( int , int * , int ) ;   /* 12 Sep 2017 */
1244 
1245 extern void isort_short( int , short * ) ;
1246 extern void isort_float( int , float * ) ;
1247 extern void isort_pair ( int , float * , int * ) ;
1248 
1249 extern void mri_xsort_inplace( MRI_IMAGE *im , int rev ) ;
1250 extern void mri_csort_inplace( MRI_IMAGE *im , int rev , int jc ) ; /* 07 Oct 2011 */
1251 
1252 extern MRI_IMAGE * mri_nsize( MRI_IMAGE * ) ;
1253 
1254 extern float * mri_lsqfit( MRI_IMAGE * fitim , MRI_IMARR * refim , MRI_IMAGE * ) ;
1255 extern double * mri_startup_lsqfit( MRI_IMARR * , MRI_IMAGE * ) ;
1256 extern float * mri_delayed_lsqfit( MRI_IMAGE * , MRI_IMARR * , double * ) ;
1257 extern float * lsqfit( int , float * , float * , int , float *ref[] ) ;
1258 extern double * startup_lsqfit( int , float * , int , float *ref[] ) ;
1259 extern float * delayed_lsqfit( int , float * , int , float *ref[] , double * ) ;
1260 
1261 extern void mri_polyfit_verb( int ) ;
1262 extern void mri_polyfit_set_basis( char *str );
1263 extern MRI_IMAGE * mri_polyfit( MRI_IMAGE *, int, MRI_IMARR *, byte *, float, int ) ;
1264 extern MRI_IMAGE * mri_polyfit_byslice( MRI_IMAGE *, int, MRI_IMARR *, byte *, float, int ) ;
1265 
1266 extern MRI_IMAGE * mri_pcvector  ( MRI_IMARR *imar , int,int ) ;
1267 extern MRI_IMAGE * mri_meanvector( MRI_IMARR *imar , int,int ) ;
1268 extern MRI_IMAGE * mri_MMBvector ( MRI_IMARR *imar , int,int,int ) ; /* 05 Aug 2010 */
1269 
1270 extern MRI_IMAGE * mri_sobel( int , int , MRI_IMAGE * ) ;
1271 extern MRI_IMAGE * mri_sharpen( float , int , MRI_IMAGE * ) ;
1272 extern MRI_IMAGE * mri_transpose( MRI_IMAGE * ) ;
1273 extern MRI_IMAGE * mri_interleave_columns(MRI_IMAGE *, int) ; /* 27 Jul 2009 */
1274 extern MRI_IMAGE * mri_rowmajorize_1D( MRI_IMAGE *im ) ;      /* 14 Mar 2013 */
1275 
1276 #define FILT_FFT_WRAPAROUND  1
1277 
1278 extern MRI_IMAGE * mri_filt_fft( MRI_IMAGE * im , float,int,int,int ) ;
1279 
1280 extern MRI_IMAGE *mri_medianfilter( MRI_IMAGE *, float, byte *, int ); /* 22 Feb 2005 */
1281 extern MRI_IMAGE *mri_flatfilter  ( MRI_IMAGE *, float, byte *, int ); /* 24 Jul 2008 */
1282 extern void mri_medianfilter_usedxyz( int i ) ;                        /* 08 Aug 2006 */
1283 extern void mri_flatfilter_usedxyz  ( int i ) ;
1284 
1285 void mri_Set_KO_catwrap(void);
1286 void mri_Set_OK_catwrap(void);
1287 void mri_Set_OK_catrandwrap(void);
1288 void mri_Set_OK_WrapZero(byte vv);
1289 void mri_Set_KO_WrapZero(void);
1290 extern MRI_IMAGE * mri_cat2D( int,int,int,void *,MRI_IMARR *) ;
1291 extern MRI_IMARR * mri_uncat2D( int , int , MRI_IMAGE * im ) ; /* 09 May 2000 */
1292 
1293 extern MRI_IMAGE * mri_catvol_1D( MRI_IMARR *imar , int dir ); /* 08 Dec 2010 */
1294 extern MRI_IMAGE * mri_catvol_1D_ab( MRI_IMARR *imar , int dir, int na,int nb );
1295 
1296 extern MRI_IMAGE * mri_shift_1D( MRI_IMAGE * im , float shift ) ;
1297 
1298 /*** image alignment procedures and constants ***/
1299 
1300 #define ALIGN_DFSPACE_TYPE    1
1301 #define ALIGN_DFTIME_TYPE     2
1302 
1303 #define ALIGN_VERBOSE_CODE    1   /* verbose output during routine */
1304 #define ALIGN_NOITER_CODE     2   /* don't iterate alignment algorithm */
1305 #define ALIGN_REGISTER_CODE   4   /* return MRI_IMARR * of registered images */
1306 #define ALIGN_DETREND_CODE    8   /* remove trend from registered images (DFTIME only) */
1307 #define ALIGN_DOBOTH_CODE    16   /* do dfspace before dftime (DFTIME only) */
1308 #define ALIGN_DEBUG_CODE     32   /* print out debugging info */
1309 #define ALIGN_FREEUP_CODE    64   /* free input images when no longer needed */
1310 #define ALIGN_BILINEAR_CODE 128   /* use bilinear interpolation in mri_align */
1311 #define ALIGN_FOURIER_CODE  256   /* use Fourier interpolation in mri_align */
1312 
1313 extern MRI_IMARR * mri_align_dfspace( MRI_IMAGE *, MRI_IMAGE * , MRI_IMARR *,
1314                                       int, float *, float *, float * ) ;
1315 
1316 extern MRI_IMARR * mri_align_dftime( MRI_IMAGE *, MRI_IMAGE * , MRI_IMARR *,
1317                                      int, float *, float *, float * ) ;
1318 
1319 extern void mri_align_params( int,float,float,float,float,float,float ) ;
1320 extern void mri_align_method( int,int,int ) ;  /* 01 Oct 1998 */
1321 
1322 extern void mri_get_cmass_2D( MRI_IMAGE *, float *, float * ); /* 12 Nov 2001 */
1323 extern void mri_get_cmass_3D( MRI_IMAGE *, float *, float * , float *);
1324 
1325 extern float mri_spearman_corr( MRI_IMAGE *, MRI_IMAGE * ) ;  /* 08 Mar 2006 */
1326 
1327 /*---------------------------------------------------------------------*/
1328 /* 07 April 1998: routines for one-at-a-time alignment (mri_2dalign.c) */
1329 
1330 /*! Struct used in 2D image registration. */
1331 
1332 typedef struct {
1333    MRI_IMARR * fitim , * fine_fitim ;
1334    double * chol_fitim , * chol_fine_fitim ;
1335 } MRI_2dalign_basis ;
1336 
1337 extern void mri_2dalign_params( int,float,float,float,float,float,float ) ;
1338 extern void mri_2dalign_method( int,int,int ) ;
1339 extern MRI_2dalign_basis * mri_2dalign_setup( MRI_IMAGE * , MRI_IMAGE * ) ;
1340 extern MRI_IMAGE * mri_2dalign_one( MRI_2dalign_basis * , MRI_IMAGE * ,
1341                                     float * , float * , float * ) ;
1342 extern MRI_IMARR * mri_2dalign_many( MRI_IMAGE *, MRI_IMAGE * , MRI_IMARR *,
1343                                      float * , float * , float * ) ;
1344 extern void mri_2dalign_cleanup( MRI_2dalign_basis * ) ;
1345 /*---------------------------------------------------------------------*/
1346 
1347 /*** routine to flip 2D images around ***/
1348 
1349 #define MRI_ROT_0   1  /* codes for various rotations */
1350 #define MRI_ROT_90  2  /* [do not change these unless */
1351 #define MRI_ROT_180 4  /*  imseq.h is changed also!]  */
1352 #define MRI_ROT_270 8
1353 #define MRI_FLMADD  128
1354 
1355 extern MRI_IMAGE * mri_flippo( int rot , int mirror , MRI_IMAGE * im ) ;
1356 
1357 extern MRI_IMAGE * mri_flip3D( int,int,int , MRI_IMAGE *inim ) ; /* 19 Mar 2003 */
1358 
1359 /*---------------------------------------------------------------------*/
1360 /*--------- 22 April 1998: byte order routines (mri_order.c) ----------*/
1361 
1362 #define LSB_FIRST      1
1363 #define MSB_FIRST      2
1364 #define NATIVE_ORDER  -1
1365 
1366 #define REVERSE_ORDER(bord) (3-(bord))  /* 21 Jun 2002 */
1367 
1368 #define ORDER_LEN        9
1369 #define LSB_FIRST_STRING "LSB_FIRST"
1370 #define MSB_FIRST_STRING "MSB_FIRST"
1371 #define NATIVE_STRING    "NATIVE_ORDER"
1372 
1373 #define BYTE_ORDER_STRING(qq) (  ((qq)==LSB_FIRST) ? LSB_FIRST_STRING \
1374                                : ((qq)==MSB_FIRST) ? MSB_FIRST_STRING \
1375                                                    : "Illegal Value" )
1376 extern int mri_short_order(void) ;
1377 extern int mri_int_order(void) ;
1378 extern void mri_swap2( int , short * ) ;
1379 extern void mri_swap4( int , int * ) ;
1380 
1381 #undef min
1382 #undef max
1383 
1384 /*---------------------------------------------------------------------*/
1385 /*------------------ 18 Sep 2001: drawing stuff -----------------------*/
1386 
1387 extern void mri_drawline( MRI_IMAGE *im, int x0,int y0, int x1,int y1,
1388                           byte r,byte g,byte b );
1389 
1390 extern void mri_drawfilledrectangle( MRI_IMAGE *im ,
1391                                      int x, int y, int width, int height ,
1392                                      byte r,byte g,byte b );
1393 
1394 extern void mri_drawemptyrectangle( MRI_IMAGE *im ,
1395                                     int x, int y, int width, int height ,
1396                                     byte r,byte g,byte b );
1397 
1398 extern void mri_drawtext( MRI_IMAGE *im ,
1399                           int x, int y, int height, int angle, char *s,
1400                           byte r,byte g,byte b );
1401 
1402 extern void mri_draw_opacity( float ) ;
1403 extern void mri_draw_force_opaque(int fo) ;
1404 
1405 extern void mri_drawcircle( MRI_IMAGE *im ,
1406                             int cx, int cy, int radius, byte r,byte g,byte b, int fill ) ;
1407 
1408 /**********************************************************************/
1409 
1410 extern MRI_IMAGE * mri_downsize_by2( MRI_IMAGE * ) ;    /* 27 Apr 2012 */
1411 
1412 /************************ Statistics routines *************************/
1413 /**
1414   if the math library doesn't have the log(gamma(x))
1415   function (as on Linux, for example)
1416 **/
1417 
1418 #ifdef NO_GAMMA
1419 /* extern double gamma_12    ( double ) ;   these are static functions */
1420 /* extern double gamma_asympt( double ) ;           7 Aug 2006 [rickr] */
1421 extern double gamma       ( double ) ;
1422 #endif
1423 
1424 extern double lnbeta         ( double , double ) ;
1425 extern double incbeta        ( double , double , double , double ) ;
1426 extern double incbeta_inverse( double , double , double , double ) ;
1427 extern double qginv          ( double ) ;
1428 extern double qg             ( double ) ;     /* 21 Mar 2001 */
1429 extern double log10qg        ( double ) ;
1430 #define QG(x) (0.5*erfc(x/1.414213562373095))
1431 
1432 #define erfcinv(y) (0.70710678*qginv(0.5*y))  /* 07 Oct 1999 */
1433 
1434 extern double student_t2p( double , double ) ;
1435 extern double student_p2t( double , double ) ;
1436 extern double student_t2z( double , double ) ;
1437 
1438 extern double correl_t2p ( double , double , double , double ) ;
1439 extern double correl_t2z ( double , double , double , double ) ;
1440 extern double correl_p2t ( double , double , double , double ) ;
1441 
1442 extern double studave_t2p( double , double , double ) ;  /* not implemented */
1443 extern double studave_t2z( double , double , double ) ;  /* not implemented */
1444 extern double studave_p2t( double , double , double ) ;
1445 
1446 extern double fstat_p2t( double , double , double ) ;
1447 extern double fstat_t2p( double , double , double ) ;
1448 extern double fstat_t2z( double , double , double ) ;
1449 
1450 extern double normal_t2p  ( double zz ) ;
1451 extern double normal_p2t  ( double qq ) ;
1452 #define       normal_t2z(x) (x)                     /* no function needed here! */
1453 
1454 extern double chisq_t2p   ( double xx , double dof ) ;
1455 extern double chisq_t2z   ( double xx , double dof ) ;
1456 extern double chisq_p2t   ( double qq , double dof ) ;
1457 
1458 extern double beta_t2p    ( double xx , double aa , double bb ) ;
1459 extern double beta_t2z    ( double xx , double aa , double bb ) ;
1460 extern double beta_p2t    ( double qq , double aa , double bb ) ;
1461 
1462 extern double binomial_t2p( double ss , double ntrial , double ptrial ) ;
1463 extern double binomial_t2z( double ss , double ntrial , double ptrial ) ;
1464 extern double binomial_p2t( double qq , double ntrial , double ptrial ) ;
1465 
1466 extern double gamma_t2p   ( double xx , double sh , double sc ) ;
1467 extern double gamma_t2z   ( double xx , double sh , double sc ) ;
1468 extern double gamma_p2t   ( double qq , double sh , double sc ) ;
1469 
1470 extern double poisson_t2p ( double xx , double lambda ) ;
1471 extern double poisson_t2z ( double xx , double lambda ) ;
1472 extern double poisson_p2t ( double qq , double lambda ) ;
1473 
1474 /*-----------------------------------------------------*/
1475 /* Add extra int 'kk' to floatvec struct [26 Jun 2018] */
1476 
1477 typedef struct { int nar ; float  *ar , dx,x0 ; int kk ; } floatvec ;
1478 typedef struct { int nar ; double *ar , dx,x0 ; int kk ; } doublevec ;
1479 #define KILL_floatvec(fv)                      \
1480   do{ if( (fv) != NULL ){                      \
1481         if( (fv)->ar != NULL ) free((fv)->ar); \
1482         free(fv); (fv) = NULL;                 \
1483   }} while(0)
1484 #define KILL_doublevec KILL_floatvec
1485 
1486 #define MAKE_floatvec(fv,n)                             \
1487   do{ (fv) = (floatvec *)malloc(sizeof(floatvec)) ;     \
1488       (fv)->nar = (n) ; (fv)->dx=1.0f; (fv)->x0=0.0f;   \
1489       (fv)->ar  = (float *)calloc(sizeof(float),(n)) ;  \
1490       (fv)->kk  = 0 ;                                   \
1491       if( (fv)->ar == NULL ) fprintf(stderr,"** ERROR: MAKE_floatvec malloc fails\n"); \
1492   } while(0)
1493 
1494 #define MAKE_doublevec(dv,n)                              \
1495   do{ (dv) = (doublevec *)malloc(sizeof(doublevec)) ;     \
1496       (dv)->nar = (n) ; (dv)->dx=1.0; (dv)->x0=0.0;       \
1497       (dv)->ar  = (double *)calloc(sizeof(double),(n)) ;  \
1498       (dv)->kk  = 0 ;                                     \
1499       if( (dv)->ar == NULL ) fprintf(stderr,"** ERROR: MAKE_doublevec malloc fails\n"); \
1500   } while(0)
1501 
1502 #define COPY_floatvec(ev,fv)                          \
1503  do{ int n = (fv)->nar ; MAKE_floatvec((ev),n) ;      \
1504      (ev)->dx = (fv)->dx ; (ev)->x0 = (fv)->x0 ;      \
1505      memcpy( (ev)->ar, (fv)->ar, sizeof(float)*n ) ;  \
1506      (ev)->kk = (fv)->kk ;                            \
1507  } while(0)
1508 
1509 #define RESIZE_floatvec(fv,m)                                     \
1510   do{ if( (fv)->nar != (m) ){                                     \
1511         (fv)->nar = (m) ;                                         \
1512         (fv)->ar  = (float *)realloc((fv)->ar,sizeof(float)*(m)); \
1513         if( (fv)->ar == NULL ) fprintf(stderr,"** ERROR: RESIZE_floatvec malloc fails\n"); \
1514   }} while(0)
1515 
1516 #define COPY_doublevec(ev,fv)                          \
1517  do{ int n = (fv)->nar ; MAKE_doublevec((ev),n) ;      \
1518      (ev)->dx = (fv)->dx ; (ev)->x0 = (fv)->x0 ;       \
1519      memcpy( (ev)->ar, (fv)->ar, sizeof(double)*n ) ;  \
1520      (ev)->kk = (fv)->kk ;                             \
1521  } while(0)
1522 
1523 #define RESIZE_doublevec(fv,m)                                      \
1524   do{ if( (fv)->nar != (m) ){                                       \
1525         (fv)->nar = (m) ;                                           \
1526         (fv)->ar  = (double *)realloc((fv)->ar,sizeof(double)*(m)); \
1527         if( (fv)->ar == NULL ) fprintf(stderr,"** ERROR: RESIZE_doublevec malloc fails\n"); \
1528   }} while(0)
1529 
1530 extern float  interp_floatvec ( floatvec  *fv , float  x ) ;
1531 extern double interp_doublevec( doublevec *dv , double x ) ;
1532 extern void mri_write_floatvec( char *fname , floatvec *fv ) ; /* 21 Jan 2016 */
1533 
1534 extern float interp_inverse_floatvec( floatvec *fv , float y ) ;
1535 
1536 typedef struct { int nvec ; floatvec *fvar ; } floatvecvec ;
1537 typedef struct { int nvec ; doublevec *dvar ; } doublevecvec ;
1538 
1539 extern MRI_IMAGE *mri_to_pval  ( MRI_IMAGE *im , int , float * ) ;
1540 extern MRI_IMAGE *mri_to_zscore( MRI_IMAGE *im , int , float * ) ;
1541 extern MRI_IMAGE *mri_to_qval( MRI_IMAGE * , floatvec * ) ; /* 01 Feb 2020 */
1542 
1543 /*-----------------------------------------------------*/
1544 
1545 extern floatvec * mri_polyfit_get_fitvec(void) ; /* 26 Feb 2019 */
1546 
1547 /*-----------------------------------------------------*/
1548 
1549 typedef struct { int nar ; int *ar ; } intvec ;
1550 #define KILL_intvec(iv)                        \
1551   do{ if( (iv) != NULL ){                      \
1552         if( (iv)->ar != NULL ) free((iv)->ar); \
1553         free(iv); (iv) = NULL;                 \
1554   } } while(0)
1555 
1556 typedef struct { int nvec ; intvec *ivar ; } intvecvec ;
1557 
1558 #define MAKE_intvec(iv,n)                           \
1559   do{ (iv) = (intvec *)malloc(sizeof(intvec)) ;     \
1560       (iv)->nar = (n) ;                             \
1561       (iv)->ar  = (int *)calloc(sizeof(int),(n)) ;  \
1562       if( (iv)->ar == NULL ) fprintf(stderr,"** ERROR: MAKE_intvec malloc fails\n"); \
1563   } while(0)
1564 
1565 #define RESIZE_intvec(iv,m)                                   \
1566   do{ if( (iv)->nar != (m) ){                                 \
1567         (iv)->nar = (m) ;                                     \
1568         (iv)->ar  = (int *)realloc((iv)->ar,sizeof(int)*(m)); \
1569         if( (iv)->ar == NULL ) fprintf(stderr,"** ERROR: RESIZE_intvec malloc fails\n"); \
1570   }} while(0)
1571 
1572 #define APPEND_intvec(iv,jv)                                   \
1573   do{ int ni = (iv)->nar ;                                     \
1574       RESIZE_intvec((iv),ni+(jv)->nar) ;                       \
1575       memcpy( (iv)->ar+ni, (jv)->ar, sizeof(int)*(jv)->nar ) ; \
1576   } while(0)
1577 
1578 /* 04 Aug 2021 */
1579 #define DUMP_intvec(iv,str)                                               \
1580   do{ int ni = (iv)->nar , qq ;                                           \
1581       INFO_message("intvec %s has %d elements:",(str),ni) ;               \
1582       if( ni > 0 ){                                                       \
1583         for( qq=0 ; qq < ni ; qq++ ) fprintf(stderr," %d",(iv)->ar[qq]) ; \
1584         fprintf(stderr,"\n") ;                                            \
1585       }                                                                   \
1586   } while(0)
1587 
1588 /* 06 Aug 2021 */
1589 #define COPY_intvec(nv,ov)                              \
1590   do{ int qq ;                                          \
1591       MAKE_intvec(nv,ov->nar) ;                         \
1592       memcpy( nv->ar , ov->ar , sizeof(int)*ov->nar ) ; \
1593   } while(0) ;
1594 
1595 /*--------------------------------------------------*/  /* 20 Jan 2016 */
1596 
1597 typedef struct { int nar ; int64_t *ar ; } int64vec ;
1598 #define KILL_int64vec(iv)                      \
1599   do{ if( (iv) != NULL ){                      \
1600         if( (iv)->ar != NULL ) free((iv)->ar); \
1601         free(iv); (iv) = NULL;                 \
1602   } } while(0)
1603 
1604 
1605 #define MAKE_int64vec(iv,n)                                \
1606   do{ (iv) = (int64vec *)malloc(sizeof(int64vec)) ;        \
1607       (iv)->nar = (n) ;                                    \
1608       (iv)->ar  = (int64_t *)calloc(sizeof(int64_t),(n)) ; \
1609       if( (iv)->ar == NULL ) fprintf(stderr,"** ERROR: MAKE_int64vec malloc fails\n"); \
1610   } while(0)
1611 
1612 /*--------------------------------------------------*/
1613 
1614 typedef struct { int nar ; short *ar ; } shortvec ;
1615 #define KILL_shortvec(iv)                    \
1616   do{ if( (iv) != NULL ){                     \
1617         if( (iv)->ar != NULL ) free((iv)->ar); \
1618         free(iv); (iv) = NULL;                  \
1619   } } while(0)
1620 
1621 #define MAKE_shortvec(iv,n)                          \
1622   do{ (iv) = (shortvec *)malloc(sizeof(shortvec)) ;   \
1623       (iv)->nar = (n) ;                                \
1624       (iv)->ar  = (short *)calloc(sizeof(short),(n)) ;  \
1625       if( (iv)->ar == NULL ) fprintf(stderr,"** ERROR: MAKE_shortvec malloc fails\n"); \
1626   } while(0)
1627 
1628 #define RESIZE_shortvec(iv,m)                                  \
1629   do{ if( (iv)->nar != (m) ){                                   \
1630         (iv)->nar = (m) ;                                        \
1631         (iv)->ar  = (short *)realloc((iv)->ar,sizeof(short)*(m)); \
1632         if( (iv)->ar == NULL ) fprintf(stderr,"** ERROR: RESIZE_shortvec malloc fails\n"); \
1633   }} while(0)
1634 
1635 /*--------------------------------------------------*/
1636 /* Jul 2010 */
1637 
1638 typedef struct { int nar ; byte *ar ; } bytevec ;
1639 #define KILL_bytevec(iv)                     \
1640   do{ if( (iv) != NULL ){                     \
1641         if( (iv)->ar != NULL ) free((iv)->ar); \
1642         free(iv); (iv) = NULL;                  \
1643   } } while(0)
1644 
1645 #define MAKE_bytevec(iv,n)                         \
1646   do{ (iv) = (bytevec *)malloc(sizeof(bytevec)) ;   \
1647       (iv)->nar = (n) ;                              \
1648       (iv)->ar  = (byte *)calloc(sizeof(byte),(n)) ;  \
1649       if( (iv)->ar == NULL ) fprintf(stderr,"** ERROR: MAKE_bytevec malloc fails\n"); \
1650   } while(0)
1651 
1652 #define RESIZE_bytevec(iv,m)                                 \
1653   do{ if( (iv)->nar != (m) ){                                 \
1654         (iv)->nar = (m) ;                                      \
1655         (iv)->ar  = (byte *)realloc((iv)->ar,sizeof(byte)*(m)); \
1656         if( (iv)->ar == NULL ) fprintf(stderr,"** ERROR: RESIZE_bytevec malloc fails\n"); \
1657   }} while(0)
1658 
1659 /*--------------------------------------------------*/
1660 
1661 typedef struct {
1662   int nbot, ntop , gbot ;
1663   char name[64] ;
1664 } SYM_irange ;
1665 
1666 extern floatvecvec * SYM_expand_ranges( int, int, SYM_irange *, char * );
1667 extern int SYM_expand_errcount(void) ;                          /* 03 May 2007 */
1668 extern char * SYM_test_gltsym( char *varlist , char *gltsym ) ; /* 01 May 2015 */
1669 
1670 /*-----------------  30 Oct 1996: incorporation of cdflib ----------------*/
1671 /*-----------------  09 May 2007: get them from nifticdf  ----------------*/
1672 #ifndef __COMPILE_UNUSED_FUNCTIONS__
1673 #define __COMPILE_UNUSED_FUNCTIONS__
1674 #endif
1675 
1676 /*-----------------  06 Dec 2004: incorporation of list_struct  ----------*/
1677 #include "list_struct.h"
1678 
1679 #ifndef MRILIB_MINI
1680 
1681 #include "nifticdf.h"    /* was cdflib.h */
1682 #include "thd_iochan.h"
1683 #include "3ddata.h"
1684 #include "thd_maker.h"
1685 #include "editvol.h"
1686 
1687 #include "cs.h"            /* 17 Aug 1998 addition */
1688 #include "multivector.h"   /* 18 May 1999 addition */
1689 #include "afni_environ.h"  /* 07 Jun 1999 addition */
1690 #include "r_new_resam_dset.h" /* 31 Jul 2007 */
1691 #include "r_idisp.h"
1692 #include "r_misc.h"
1693 
1694 #include "thd_atlas.h"        /* 22 Feb 2012 [rickr] */
1695 #include "thd_StatsPDL.h"     /* 22 Jul 2020 [PDL] */
1696 
1697 #include "thd_euler_dist.h"   /* 10 Dec 2021 [ptaylor] */
1698 #include "thd_edge_dog.h"     /* 10 Dec 2021 [ptaylor] */
1699 
1700 THD_string_array * mri_read_1D_headerline( char *fname ) ; /* 18 May 2010 */
1701 
1702 #endif /* MRILIB_MINI */
1703 
1704 #include "rcmat.h"            /* 30 Dec 2008 */
1705 /*------------------------------------------------------------------------*/
1706 
1707 /*-----------------  01 Feb 1998: incoroporation of mcw_glob -------------*/
1708 #include "mcw_glob.h"
1709 /*------------------------------------------------------------------------*/
1710 
1711 /*-----------------  02 Feb 1998:
1712                      incoroporation of 3ddata, 3dmaker, iochan -----------*/
1713 
1714 #ifdef HAVE_ZLIB
1715 #include <zlib.h>             /* 02 Mar 2009 */
1716 #endif
1717 
1718 #include "misc_math.h"        /* 21 Jun 2010 [rickr] */
1719 
1720 /* 09 Feb 2017: change the way thresholds are short-ified,
1721                 along with changes in the relevant functions */
1722 
1723 #define THRESH_SHORTIZE(ttt) \
1724   ( AFNI_yesenv("AFNI_OLD_SHORT_THRESH") ?  (float)SHORTIZE(ttt) : (ttt) )
1725 
1726 /*------------------------------------------------------------------------*/
1727 /* 13 Feb 2009: generic 4x4 matrix struct stuff */
1728 
1729 typedef struct { double m[4][4] ; } dmat44 ;
1730 extern dmat44 generic_dmat44_inverse    ( dmat44 P ) ;
1731 extern double generic_dmat44_determinant( dmat44 P ) ;
1732 
1733 /* apply a dmat44 matrix to a 4 vector (x,y,z,w) to produce (a,b,c,d) */
1734 
1735 #undef  DMAT44_VEC
1736 #define DMAT44_VEC(A,x,y,z,w,a,b,c,d)                                    \
1737  ( (a) = A.m[0][0]*(x) + A.m[0][1]*(y) + A.m[0][2]*(z) + A.m[0][3]*(w) , \
1738    (b) = A.m[1][0]*(x) + A.m[1][1]*(y) + A.m[1][2]*(z) + A.m[1][3]*(w) , \
1739    (c) = A.m[2][0]*(x) + A.m[2][1]*(y) + A.m[2][2]*(z) + A.m[2][3]*(w) , \
1740    (d) = A.m[3][0]*(x) + A.m[3][1]*(y) + A.m[3][2]*(z) + A.m[3][3]*(w)  )
1741 
1742 /* print a dmat44 struct to stdout (with a string) */
1743 
1744 #undef  DUMP_DMAT44
1745 #define DUMP_DMAT44(SS,AA)                             \
1746     fprintf(stderr,                                    \
1747             "# dmat44 %s:\n"                           \
1748             " %13.6g %13.6g %13.6g %13.6g\n"           \
1749             " %13.6g %13.6g %13.6g %13.6g\n"           \
1750             " %13.6g %13.6g %13.6g %13.6g\n"           \
1751             " %13.6g %13.6g %13.6g %13.6g\n" ,         \
1752   SS, AA.m[0][0], AA.m[0][1], AA.m[0][2], AA.m[0][3],  \
1753       AA.m[1][0], AA.m[1][1], AA.m[1][2], AA.m[1][3],  \
1754       AA.m[2][0], AA.m[2][1], AA.m[2][2], AA.m[2][3],  \
1755       AA.m[3][0], AA.m[3][1], AA.m[3][2], AA.m[3][3] )
1756 
1757 /* load elements of a dmat44 */
1758 
1759 #undef  LOAD_DMAT44
1760 #define LOAD_DMAT44(A,a11,a12,a13,a14,a21,a22,a23,a24,a31,a32,a33,a34,a41,a42,a43,a44) \
1761  ( A.m[0][0] = (a11), A.m[0][1] = (a12), A.m[0][2] = (a13), A.m[0][3] = (a14),         \
1762    A.m[1][0] = (a21), A.m[1][1] = (a22), A.m[1][2] = (a23), A.m[1][3] = (a24),         \
1763    A.m[2][0] = (a31), A.m[2][1] = (a32), A.m[2][2] = (a33), A.m[2][3] = (a34),         \
1764    A.m[3][0] = (a41), A.m[3][1] = (a42), A.m[3][2] = (a43), A.m[3][3] = (a44)  )
1765 
1766 /*------------------------------------------------------------------------*/
1767 
1768 extern MRI_IMAGE * mri_genARMA11( int nlen, int nvec, float ap, float lm, float sg ) ;
1769 extern void mri_genARMA11_set_tdof( float ttt ) ;
1770 
1771 /*------------------------------------------------------------------------*/
1772 /* some of these clusterize prototypes require editvol.h */
1773 
1774 #ifndef MRILIB_MINI
1775 typedef struct {
1776   int nvox ;
1777   float volume , xcm , ycm , zcm ;
1778   float          xpk , ypk , zpk ;
1779   float          xmi , ymi , zmi ;  /* 08 May 2019 */
1780 } mri_cluster_detail ;
1781 
1782 extern MRI_IMAGE * mri_clusterize( float,float, MRI_IMAGE * ,
1783                                    float,float, MRI_IMAGE * , int , byte * );
1784 extern char * mri_clusterize_report(void) ;
1785 extern MCW_cluster_array * mri_clusterize_array(int clear) ;
1786 extern mri_cluster_detail mri_clusterize_detailize( MCW_cluster *cl, int icent);
1787 
1788 extern MRI_IMAGE * mri_bi_clusterize( float rmm , float vmul , MRI_IMAGE *bim ,
1789                                       float thb , float tht  , MRI_IMAGE *tim ,
1790                                       byte *mask ) ;  /* 29 Jan 2015 */
1791 #endif /* MRILIB_MINI */
1792 
1793 extern void mri_fdr_setmask( byte *mmm ) ;                /* 27 Mar 2009 */
1794 extern int mri_fdrize( MRI_IMAGE *, int, float *, int ) ; /* 17 Jan 2008 */
1795 extern floatvec * mri_fdr_curve( MRI_IMAGE *, int , float * ) ;
1796 extern floatvec * mri_fdr_getmdf(void) ;                  /* 22 Oct 2008 */
1797 
1798 /*------------------------------------------------------------------------*/
1799 /*--- Functions in mri_matrix.c (matrix operations, stored as images) ----*/
1800 
1801 extern MRI_IMAGE * mri_matrix_mult     ( MRI_IMAGE *, MRI_IMAGE *);
1802 extern MRI_IMAGE * mri_matrix_multranA ( MRI_IMAGE *, MRI_IMAGE *);
1803 extern MRI_IMAGE * mri_matrix_multranB ( MRI_IMAGE *, MRI_IMAGE *);
1804 extern MRI_IMAGE * mri_matrix_psinv    ( MRI_IMAGE *, float * , float );
1805 extern void        mri_matrix_psinv_svd( int ) ;
1806 extern MRI_IMAGE * mri_matrix_ortproj  ( MRI_IMAGE * , int ) ;
1807 extern MRI_IMAGE * mri_matrix_sadd     ( float, MRI_IMAGE *, float, MRI_IMAGE * ) ;
1808 extern MRI_IMAGE * mri_matrix_scale    ( float, MRI_IMAGE * ) ;
1809 extern MRI_IMAGE * mri_matrix_evalrpn  ( char * ) ;
1810 extern char      * mri_matrix_evalrpn_help(void) ;
1811 extern void        mri_matrix_evalrpn_verb(int) ;
1812 extern float mri_matrix_size( MRI_IMAGE * ) ;
1813 extern MRI_IMARR * mri_matrix_psinv_ortproj( MRI_IMAGE *, int ) ; /* 13 Dec 2011 */
1814 
1815 extern MRI_IMARR * mri_matrix_psinv_pair( MRI_IMAGE *, float ) ;
1816 extern MRI_IMAGE * mri_matrix_singvals  ( MRI_IMAGE * ) ;
1817 
1818 extern void mri_matrix_detrend( MRI_IMAGE *, MRI_IMAGE *, MRI_IMAGE * ) ;
1819 
1820 #define            mri_matrix_transpose(x) mri_transpose(x)
1821 
1822 extern double Plegendre( double x , int m ) ;
1823 
1824 extern void mri_matrix_print( FILE *fp , MRI_IMAGE *ima , char *label ) ;
1825 
1826 /*------------------------------------------------------------------------*/
1827 
1828 #ifndef MRILIB_MINI
1829 extern MRI_IMAGE * THD_average_timeseries( MCW_cluster_array *, THD_3dim_dataset *) ;
1830 extern MRI_IMAGE * THD_average_one_timeseries( MCW_cluster *, THD_3dim_dataset *) ;
1831 #endif
1832 
1833 /** mri_warp3D.c functions: 14 Apr 2003 */
1834 
1835 extern MRI_IMAGE *mri_warp3D_cubic ( MRI_IMAGE *, int,int,int ,
1836                                      void func( float,float,float,
1837                                                 float *,float *,float *) ) ;
1838 extern MRI_IMAGE *mri_warp3D_linear( MRI_IMAGE *, int,int,int ,
1839                                      void func( float,float,float,
1840                                                 float *,float *,float *) ) ;
1841 extern MRI_IMAGE *mri_warp3D_NN    ( MRI_IMAGE *, int,int,int ,
1842                                      void func( float,float,float,
1843                                                 float *,float *,float *) ) ;
1844 extern MRI_IMAGE *mri_warp3D       ( MRI_IMAGE *, int,int,int ,
1845                                      void func( float,float,float,
1846                                                 float *,float *,float *) ) ;
1847 extern void mri_warp3D_method( int ) ;
1848 extern void mri_warp3D_zerout( int ) ;
1849 
1850 extern void mri_warp3D_set_womask( MRI_IMAGE * ) ;  /* 19 Nov 2004 */
1851 
1852 extern MRI_IMAGE *mri_warp3D_quintic( MRI_IMAGE *, int,int,int ,
1853                                       void func( float,float,float,
1854                                                  float *,float *,float *) ) ;
1855 
1856 extern MRI_IMAGE * mri_warp3D_affine( MRI_IMAGE * , THD_vecmat ) ;
1857 extern MRI_IMAGE * mri_warp3D_resize( MRI_IMAGE *, int,int,int ) ;
1858 
1859 extern double mri_entropy16( MRI_IMAGE * ) ;  /* 09 Jan 2004 */
1860 extern double mri_entropy8 ( MRI_IMAGE * ) ;  /* 09 Jan 2004 */
1861 
1862 extern float mri_scaled_diff( MRI_IMAGE *bim, MRI_IMAGE *nim, MRI_IMAGE *msk ) ;
1863 
1864 /*------------------------------------------------------------------*/
1865 
1866 #ifndef MRILIB_MINI
1867 
1868 #include "AFNI_version.h"
1869 #undef  PRINT_VERSION
1870 #define PRINT_VERSION(pp)                                             \
1871  do{ if( !machdep_be_quiet() )                                        \
1872       INFO_message("%s: AFNI version=%s (" __DATE__ ") [%d-bit]",     \
1873                    (pp),AFNI_VERSION_LABEL,(int)(sizeof(void *)*8)) ; \
1874  } while(0)
1875 
1876 #undef  PRINT_COMPILE_DATE
1877 #if defined(AFNI_VERSION_LABEL) && defined(AFNI_VERSION_PLATFORM)
1878 # define PRINT_COMPILE_DATE                     \
1879          printf("\n++ Compile date = " __DATE__ \
1880                 " {%s:%s}\n\n",AFNI_VERSION_LABEL,AFNI_VERSION_PLATFORM)
1881 #else
1882 # define PRINT_COMPILE_DATE  \
1883          printf("\n++ Compile date = " __DATE__ "\n\n")
1884 #endif
1885 
1886 #undef  AUTHOR
1887 #define AUTHOR(aa) \
1888  do{ if( !machdep_be_quiet() ) INFO_message("Authored by: %s",aa) ; } while(0)
1889 
1890 #endif /* MRILIB_MINI */
1891 
1892 #undef  WROTE_DSET_MSG
1893 #define WROTE_DSET_MSG(dd,ss)                                        \
1894   do{ if( THD_is_file(DSET_BRIKNAME(dd)) && !machdep_be_quiet() )    \
1895        INFO_message("Output dataset %s {%s}",DSET_BRIKNAME(dd),(ss)); } while(0)
1896 
1897 #undef  WROTE_DSET
1898 #define WROTE_DSET(dd)                                                  \
1899   do{ if( !machdep_be_quiet() && THD_is_file(DSET_BRIKNAME(dd)) )       \
1900         INFO_message("Output dataset %s",DSET_BRIKNAME(dd)); } while(0)
1901 
1902 #undef  WROTE_DSETI
1903 #define WROTE_DSETI(dd)                                                  \
1904   do{ if( !machdep_be_quiet() && THD_is_file(DSET_BRIKNAME(dd)) )        \
1905         ININFO_message("Output dataset %s",DSET_BRIKNAME(dd)); } while(0)
1906 
1907 #undef  CHECK_OPEN_ERROR
1908 #define CHECK_OPEN_ERROR(dd,nn) \
1909  do{ if( !ISVALID_DSET(dd) ) ERROR_exit("Can't open dataset '%s'",nn); }while(0)
1910 
1911 /* note that the following is a fatal error! */
1912 
1913 #undef  CHECK_LOAD_ERROR
1914 #define CHECK_LOAD_ERROR(dd)                                                   \
1915  do{ if( ISVALID_DSET(dd) && !DSET_LOADED(dd) )                                \
1916       ERROR_exit("Can't load dataset '%s': is it complete?",DSET_BRIKNAME(dd));\
1917  } while(0)
1918 
1919 
1920 /*------------------------------------------------------------------*/
1921 
1922 #define METRIC_KULL  0
1923 #define METRIC_HELL  1
1924 #define METRIC_TRIA  2
1925 #define METRIC_JDIV  3
1926 #define METRIC_JSDV  4
1927 #define METRIC_XISQ  5
1928 #define METRIC_XXSQ  6
1929 #define METRIC_AGDV  7
1930 extern void mri_metrics( MRI_IMAGE *, MRI_IMAGE *, float * ) ;
1931 
1932 /*--------------------------------------------------------------------------*/
1933 /*--------------------------------------------------------------------------*/
1934 /* July 2006: for generic alignment functions: mri_genalign.c (3dAllineate) */
1935 
1936 #ifndef MRILIB_MINI
1937 
1938 #include "mri_warpfield.h"
1939 
1940 #define ALLOW_NWARP   /* for the 3dAllineate -nwarp option */
1941 
1942  /* method codes for matching scalar-valued images */
1943 
1944 #define GA_MATCH_PEARSON_SCALAR     1  /* least squares, more-or-less */
1945 #define GA_MATCH_SPEARMAN_SCALAR    2  /* rank-order correlation */
1946 #define GA_MATCH_KULLBACK_SCALAR    3  /* Mutual Info */
1947 #define GA_MATCH_MUTINFO_SCALAR     3
1948 #define GA_MATCH_CORRATIO_SCALAR    4  /* Correlation Ratio: Sym Mul */
1949 #define GA_MATCH_NORMUTIN_SCALAR    5  /* Normalized Mutual Info */
1950 #define GA_MATCH_JOINTENT_SCALAR    6  /* Joint Entropy */
1951 #define GA_MATCH_HELLINGER_SCALAR   7  /* Hellinger metric */
1952 #define GA_MATCH_CRAT_SADD_SCALAR   8  /* Correlation Ratio: Sym Add */
1953 #define GA_MATCH_CRAT_USYM_SCALAR   9  /* Correlation Ratio: Unsym */
1954 
1955 #define GA_MATCH_PEARSON_SIGNED    10  /* experimental */
1956 #define GA_MATCH_PEARSON_LOCALS    11  /* experimental */
1957 #define GA_MATCH_PEARSON_LOCALA    12  /* experimental */
1958 
1959 #define GA_MATCH_LPC_MICHO_SCALAR  13  /* 24 Feb 2010 */
1960 #define GA_MATCH_LPA_MICHO_SCALAR  14  /* 28 Nov 2018 */
1961 
1962 #define GA_MATCH_NCDZLIB           15  /* very experimental */
1963 
1964 #define GA_MATCH_PEARCLP_SCALAR    16
1965 
1966 #define GA_MATCH_METHNUM_SCALAR    14  /* Largest useful value in
1967                                           sequence above;
1968                                           -> actually, mostly just
1969                                           used to desc length of
1970                                           arrays in 3dAllineate.c
1971                                           (meth_visible) */
1972 
1973  /* methods for smoothing images */
1974 
1975 #define GA_SMOOTH_GAUSSIAN          1
1976 #define GA_SMOOTH_MEDIAN            2
1977 
1978  /* kernels for histogram estimation */
1979 
1980 #define GA_KERNEL_GAUSSIAN          1
1981 #define GA_KERNEL_QUADRATIC         2
1982 #define GA_KERNEL_QUARTIC           3
1983 
1984  /* prototype/typedef for a spatial warping function */
1985 
1986 typedef void GA_warpfunc( int, float *,
1987                           int, float *,float *,float *,
1988                                float *,float *,float * );
1989 
1990 typedef MRI_warp3D_param_def GA_param ;  /* cf. 3ddata.h */
1991 
1992 /* codes for how the 2D histogram is constructed (thd_correlate.c) */
1993 
1994 #define GA_HIST_EQWIDE 1
1995 #define GA_HIST_EQHIGH 2
1996 #define GA_HIST_CLEQWD 3
1997 
1998 /* definition of various convex neighborhoods (BLOKs) for LPC */
1999 
2000 #define GA_BLOK_BALL 1  /* sphere */
2001 #define GA_BLOK_CUBE 2  /* cube */
2002 #define GA_BLOK_RHDD 3  /* rhombic dodecahedron */
2003 #define GA_BLOK_TOHD 4  /* truncated octahedron */
2004 
2005 #define GA_BLOK_STRING(b)  ( ((b)==GA_BLOK_BALL) ? "BALL" :          \
2006                              ((b)==GA_BLOK_CUBE) ? "CUBE" :          \
2007                              ((b)==GA_BLOK_RHDD) ? "RHDD" :          \
2008                              ((b)==GA_BLOK_TOHD) ? "TOHD" :          \
2009                                                             "UNKNOWN" )
2010 
2011 
2012 /* volume is this factor, times blokrad^3 */
2013 
2014 #define GA_BLOK_VOLFAC(b)  ( ((b)==GA_BLOK_BALL) ? 4.188f :          \
2015                              ((b)==GA_BLOK_CUBE) ? 8.0f   :          \
2016                              ((b)==GA_BLOK_RHDD) ? 2.0f   :          \
2017                              ((b)==GA_BLOK_TOHD) ? 4.0f   :          \
2018                                                             0.0f      )
2019 
2020 /***** struct and macro for local statistics in BLOKs (e.g., LPC) *****/
2021 
2022 typedef struct { int num , *nelm , **elm;
2023                  int nx,ny,nz; float dx,dy,dz; float ppow; } GA_BLOK_set ;
2024 
2025 /** delete a GA_BLOK_set struct and its contents **/
2026 
2027 #define GA_BLOK_KILL(gbs)                                     \
2028  do{ int ee ;                                                 \
2029      if( (gbs)->nelm != NULL ) free((gbs)->nelm) ;            \
2030      if( (gbs)->elm != NULL ){                                \
2031        for( ee=0 ; ee < (gbs)->num ; ee++ )                   \
2032          if( (gbs)->elm[ee] != NULL ) free((gbs)->elm[ee]) ;  \
2033        free((gbs)->elm) ;                                     \
2034      }                                                        \
2035      free((gbs)) ;                                            \
2036  } while(0)
2037 
2038 /** create a GA_BLOK_set; cf. mri_genalign_util.c **/
2039 
2040 extern GA_BLOK_set * create_GA_BLOK_set( int   nx , int   ny , int   nz ,
2041                                          float dx , float dy , float dz ,
2042                                          int npt, float *im, float *jm, float *km,
2043                                          int bloktype, float blokrad, int minel,
2044                                                        float shfac  , int verb ) ;
2045 
2046 extern void GA_pearson_ignore_zero_voxels(int) ; /* 23 Feb 2010 */
2047 
2048 /******* end of BLOK-ization stuff here -- also see mri_genalign_util.c *******/
2049 
2050 extern float total_rotation_degrees( float ax, float ay, float az ) ; /* 02 Jan 2019 */
2051 
2052 /* struct to control mri_genalign.c optimization -- gets named 'stup' in places */
2053 
2054 typedef struct {
2055   int match_code  ;             /* set by user */
2056   int smooth_code ;             /* set by user */
2057   float smooth_radius_base ;    /* set by user */
2058   float smooth_radius_targ ;    /* set by user */
2059   int interp_code ;             /* set by user */
2060   mat44 base_cmat , targ_cmat ; /* set by user */
2061   mat44 base_imat , targ_imat ;
2062   float base_di,base_dj,base_dk ;
2063   float targ_di,targ_dj,targ_dk ;
2064   int usetemp ;                 /* set by user */
2065 
2066   int   bloktype, blokmin ;     /* set by user */
2067   float blokrad ;               /* set by user */
2068   GA_BLOK_set *blokset ;
2069 
2070   int old_sc ; float old_sr_base , old_sr_targ ;
2071 
2072   MRI_IMAGE *bsim , *bsims , *bsmask ;
2073   float bsbot,bstop , bsclip ;
2074   int dim_bvec    ;
2075   int   nmask     ;
2076   int   nvox_mask ;
2077   int   nbsmask   ;
2078   byte *bmask     ;
2079   MRI_IMAGE *bwght ;
2080 
2081   MRI_IMAGE *ajim , *ajims , *ajmask , *ajimor;
2082   float ajbot,ajtop , ajclip , aj_ubot,aj_usiz ;
2083   int dim_avec , abdim , najmask ;
2084   int ajmask_ranfill ;
2085 
2086   int npt_match   ;            /* set by user */
2087   floatvec *im, *jm, *km , *bvm , *wvm ;
2088   float bvstat ;
2089   int hist_mode ;              /* set by user */
2090   float hist_param ;           /* set by user */
2091   int need_hist_setup ;
2092 
2093   int   ccount_do   , ccount_val  ;  /* 22 Feb 2010 */
2094   float ccount_bthr , ccount_athr ;
2095 
2096 #if 0
2097                              /*** NOT USED YET ***/
2098   int kernel_code ;            /* set by user */
2099   float kernel_radius ;        /* set by user */
2100   int npt_sum ;                /* set by user */
2101   intvec *is, *js, *ks ;
2102   floatvec *bvs ;            /********************/
2103 #endif
2104 
2105   int          wfunc_numpar ;  /* set by user */
2106   GA_param    *wfunc_param ;   /* set by user */
2107   GA_warpfunc *wfunc ;         /* set by user */
2108   int          wfunc_numfree ;
2109   int         *wfunc_pma ;
2110   int          wfunc_ntrial ;
2111 
2112   int          setup ;
2113   float        vbest ;
2114 } GA_setup ;
2115 
2116 /** compute correlations in each blok, using the alignment setup **/
2117 
2118 extern floatvec * GA_pearson_vector( GA_BLOK_set *, float *, float *, float * );
2119 extern MRI_IMAGE * GA_pearson_image( GA_setup *stup , floatvec *pv ) ;            /* Biden day 3 */
2120 extern MRI_IMAGE * mri_genalign_map_pearson_local( GA_setup *stup , float *parm ) ; /* Biden day 6 */
2121 
2122 /* free if it isn't null */
2123 
2124 #undef  IFREE
2125 #define IFREE(x) do{ if((x)!=NULL)free(x); (x)=NULL; }while(0)
2126 
2127 #undef  FREE_GA_setup
2128 #define FREE_GA_setup(st)                                                   \
2129  do{ if( (st) != NULL ){                                                    \
2130        mri_free((st)->bsim); mri_free((st)->ajim); IFREE((st)->bmask);      \
2131        mri_free((st)->bsims);mri_free((st)->ajims);mri_free((st)->bwght);   \
2132        KILL_floatvec((st)->im); KILL_floatvec((st)->jm);                    \
2133        KILL_floatvec((st)->km); KILL_floatvec((st)->bvm);                   \
2134        KILL_floatvec((st)->wvm); IFREE((st)->wfunc_param) ;                 \
2135        mri_free((st)->ajmask); mri_free((st)->ajimor);                      \
2136        mri_free((st)->bsmask);                                              \
2137      }                                                                      \
2138  } while(0)
2139 
2140 #define GA_LEGENDRE 1
2141 #define GA_HERMITE  2
2142 
2143 extern void GA_setup_polywarp(int) ;
2144 
2145 extern void mri_genalign_scalar_setup( MRI_IMAGE *, MRI_IMAGE *,
2146                                        MRI_IMAGE *, GA_setup  * ) ;
2147 extern int mri_genalign_scalar_optim( GA_setup *, double, double, int) ;
2148 extern void mri_genalign_scalar_ransetup( GA_setup *, int ) ;
2149 extern void mri_genalign_affine( int, float *,
2150                                  int, float *, float *, float *,
2151                                       float *, float *, float * ) ;
2152 extern MRI_IMAGE * mri_genalign_scalar_warpim( GA_setup * ) ;
2153 extern void mri_genalign_verbose(int) ;
2154 extern void mri_genalign_round(int v) ; /* 04 Jun 2021 */
2155 extern void mri_genalign_mat44( int, float *,
2156                                 int, float *, float *, float *,
2157                                      float *, float *, float * ) ;
2158 extern void mri_genalign_set_pgmat( int ) ;
2159 
2160 #ifdef ALLOW_NWARP
2161 extern void mri_genalign_bilinear( int, float *,
2162                                    int, float *, float *, float *,
2163                                         float *, float *, float * ) ;
2164 
2165 extern void mri_genalign_cubic( int, float *,
2166                                 int, float *, float *, float *,
2167                                      float *, float *, float * ) ;
2168 extern void mri_genalign_quintic( int, float *,
2169                                   int, float *, float *, float *,
2170                                        float *, float *, float * ) ;
2171 extern void mri_genalign_heptic( int, float *,
2172                                  int, float *, float *, float *,
2173                                       float *, float *, float * ) ;
2174 extern void mri_genalign_nonic( int, float *,
2175                                 int, float *, float *, float *,
2176                                      float *, float *, float * ) ;
2177 
2178 extern int    GA_polywarp_coordcode( int pnum ) ; /* 06 Dec 2010 */
2179 extern char * GA_polywarp_funcname ( int pnum ) ; /* 09 Dec 2010 */
2180 #endif
2181 
2182 void mri_genalign_set_targmask( MRI_IMAGE *, GA_setup * ) ; /* 07 Aug 2007 */
2183 void mri_genalign_set_basemask( MRI_IMAGE *, GA_setup * ) ; /* 25 Feb 2010 */
2184 
2185 extern void GA_reset_fit_callback( void (*fc)(int,double*) ) ;
2186 extern void GA_do_dots(int) ;
2187 extern void GA_do_cost(int, byte) ;
2188 extern void GA_do_params(int) ;
2189 extern float mri_genalign_scalar_cost( GA_setup * , float *) ;
2190 extern void GA_set_outval( float ) ;
2191 extern float GA_get_outval(void) ;
2192 extern void GA_allow_ccount( int ) ; /* 22 Feb 2010 */
2193 extern void GA_setup_micho( double,double,double,double,double ) ; /* 24 Feb 2010 */
2194 extern void GA_set_nperval( int ) ; /* 15 Nov 2010 */
2195 
2196 /**------ these functions are now in mri_genalign_util.c [10 Dec 2008] ------**/
2197 
2198 extern void GA_interp_NN     ( MRI_IMAGE *fim , int npp,
2199                                float *ip, float *jp, float *kp, float *vv ) ;
2200 extern void GA_interp_linear ( MRI_IMAGE *fim , int npp,
2201                                float *ip, float *jp, float *kp, float *vv ) ;
2202 extern void GA_interp_cubic  ( MRI_IMAGE *fim , int npp,
2203                                float *ip, float *jp, float *kp, float *vv ) ;
2204 extern void GA_interp_quintic( MRI_IMAGE *fim , int npp,
2205                                float *ip, float *jp, float *kp, float *vv ) ;
2206 extern void GA_interp_varp1  ( MRI_IMAGE *fim , int npp,
2207                                float *ip, float *jp, float *kp, float *vv ) ;
2208 extern void GA_interp_wsinc5 ( MRI_IMAGE *fim , int npp,
2209                                float *ip, float *jp, float *kp, float *vv ) ;
2210 extern void GA_interp_wsinc5_2D( MRI_IMAGE *fim ,
2211                                  int npp, float *ip, float *jp, float *vv ) ;
2212 extern int GA_gcd(int,int) ;
2213 extern int GA_find_relprime_fixed(int) ;
2214 extern MRI_IMAGE * GA_smooth( MRI_IMAGE *im, int meth, float rad ) ;
2215 
2216 extern MRI_IMAGE * GA_indexwarp( MRI_IMAGE *, int, MRI_IMAGE * ) ;
2217 extern MRI_IMAGE * GA_indexwarp_plus( MRI_IMAGE *, int, MRI_IMAGE *,
2218                                       float_triple , byte * ) ;
2219 extern void GA_affine_edit_warp( mat44 aff , MRI_IMAGE *wpim ) ;
2220 /*----------------------------------------------------------------------------*/
2221 
2222 extern floatvec * mri_genalign_scalar_allcosts( GA_setup * , float * ); /* 19 Sep 2007 */
2223 
2224 #define MATORDER_SDU  1  /* matrix multiplication order: */
2225 #define MATORDER_SUD  2  /* S = shear matrix             */
2226 #define MATORDER_DSU  3  /* D = diagonal scaling matrix  */
2227 #define MATORDER_DUS  4  /* U = rotation matrix          */
2228 #define MATORDER_USD  5
2229 #define MATORDER_UDS  6
2230 
2231 #define SMAT_UPPER    1  /* shear matrix is upper */
2232 #define SMAT_LOWER    2  /* or lower triangular  */
2233 #define SMAT_XXX      3  /* x-axis only shears  */
2234 #define SMAT_YYY      4  /* y-axis only shears  */
2235 #define SMAT_ZZZ      5  /* z-axis only shears  */
2236 
2237 extern void mri_genalign_affine_setup( int,int,int ) ;
2238 extern void mri_genalign_affine_set_befafter( mat44 *, mat44 * ) ;
2239 extern void mri_genalign_affine_get_befafter( mat44 *, mat44 * ) ;
2240 extern void mri_genalign_affine_get_gammaijk( mat44 * ) ; /* 04 Apr 2007 */
2241 extern void mri_genalign_affine_get_gammaxyz( mat44 * ) ;
2242 
2243 void mri_genalign_affine_use_befafter(int,int) ; /* 10 Dec 2010 */
2244 
2245 extern MRI_IMAGE * mri_genalign_scalar_warpone(      /* 26 Sep 2006 */
2246                     int npar, float *wpar, GA_warpfunc *wfunc,
2247                     MRI_IMAGE *imtarg ,
2248                     int nnx , int nny , int nnz , int icode ) ;
2249 
2250 extern MRI_IMARR * mri_genalign_scalar_xyzwarp(      /* 10 Dec 2010 */
2251                     int npar, float *wpar, GA_warpfunc *wfunc,
2252                     int nnx , int nny , int nnz ) ;
2253 
2254 
2255 extern void mri_genalign_scalar_clrwght( GA_setup * ) ;  /* 18 Oct 2006 */
2256 
2257 #endif /* MRILIB_MINI */
2258 
2259 /*--------------------------------------------------------------------*/
2260 
2261 extern THD_fvec3 mri_estimate_FWHM_1dif( MRI_IMAGE * , byte * ) ;
2262 extern void FHWM_1dif_dontcheckplus( int ) ;
2263 extern THD_fvec3 mriarr_estimate_FWHM_1dif( MRI_IMARR *, byte * , int ) ;
2264 #ifndef MRILIB_MINI
2265 extern MRI_IMAGE * THD_estimate_FWHM_all( THD_3dim_dataset *, byte *, int,int ) ;
2266 #endif
2267 
2268 
2269 extern THD_fvec3 mri_estimate_FWHM_12dif( MRI_IMAGE * , byte * ) ;
2270 extern THD_fvec3 mri_estimate_FWHM_12dif_MAD( MRI_IMAGE * , byte * ) ; /* 24 Mar 2010 */
2271 
2272 extern THD_fvec3 mri_FWHM_1dif_mom12( MRI_IMAGE * , byte * ) ; /* 11 Aug 2015 */
2273 
2274 #ifndef MRILIB_MINI
2275 extern MCW_cluster * THD_estimate_ACF( THD_3dim_dataset *dset,
2276                                        byte *mask, int demed, int unif, float radius ) ;
2277 extern float_quad ACF_cluster_to_modelE( MCW_cluster *acf, float dx, float dy, float dz ) ;
2278 extern MRI_IMAGE * ACF_get_1D(void) ;
2279 extern float mriarr_estimate_FWHM_acf( MRI_IMARR *imar, byte *mask, int unif, float radius ) ;
2280 #endif
2281 
2282 void set_ACF_2D( int nn ) ; /* 25 Oct 2018 */
2283 
2284 void mri_fwhm_setfester( THD_fvec3 (*func)(MRI_IMAGE *, byte *) ) ;
2285 
2286 #ifndef MRILIB_MINI
2287 extern float mri_nstat  ( int , int , float * , float, MCW_cluster *) ;  /* 19 Aug 2005 */
2288 extern THD_fvec3 mri_nstat_fwhmxyz( int,int,int ,
2289                                     MRI_IMAGE *, byte *, MCW_cluster * );
2290 #endif
2291 
2292 extern int mri_nstat_mMP2S( int npt , float *far , float voxval, float *fv5);
2293 extern int mri_nstat_diffs( int npt , float *far , float *fv5, int doabs);
2294 extern void mri_blur3D_variable( MRI_IMAGE * , byte * ,
2295                                  MRI_IMAGE * , MRI_IMAGE * , MRI_IMAGE * ) ;
2296 extern void mri_blur3D_inmask( MRI_IMAGE *, byte *, float,float,float,int );
2297 extern void mri_blur3D_inmask_speedy( MRI_IMAGE *, byte *,
2298                                       float,float,float,int );
2299 extern void mri_blur3D_addfwhm( MRI_IMAGE *, byte *, float ) ;
2300 extern void mri_blur3D_addfwhm_speedy( MRI_IMAGE *, byte *, float ) ;
2301 extern void mri_blur3D_inmask_NN( MRI_IMAGE *im, byte *mask, int  ) ;
2302 extern void mri_blur3D_getfac ( float, float, float, float,
2303                                 int *, float *, float *, float * ) ;
2304 extern void mri_blur3D_getfac3( float fwhmx, float fwhmy, float fhwmz ,  /* 24 Mar 2021 */
2305                                 float dx   , float dy   , float dz    ,
2306                                 int *nrep , float *fx , float *fy , float *fz ) ;
2307 extern void mri_blur3D_addfwhm3( MRI_IMAGE *im , byte *mask ,
2308                                  float fwhmx,float fwhmy,float fwhmz ) ;
2309 
2310 
2311 extern MRI_IMAGE * mri_rgb_blur2D  ( float sig , MRI_IMAGE *im ) ;
2312 extern MRI_IMAGE * mri_byte_blur2D( float sig , MRI_IMAGE *im );
2313 extern MRI_IMAGE * mri_float_blur2D( float sig , MRI_IMAGE *im ) ;
2314 extern MRI_IMAGE * mri_float_blur3D( float sig , MRI_IMAGE *im ) ;
2315 
2316 void *Percentate (void *vec, byte *mm, int nxyz,
2317                   int type, double *mpv, int N_mp,
2318                   int option, double *perc ,
2319                   int zero_flag, int positive_flag, int negative_flag);
2320 
2321 /*----------------------------------------------------------------------------*/
2322 /* RBF stuff -- cf. mri_rbfinterp.c -- 05 Feb 2009 */
2323 
2324 typedef unsigned short RBFKINT ;
2325 #define RBFKINT_MAX 65535u
2326 
2327 typedef struct {
2328   int nknot ;                    /* number of knots */
2329   float rad  , rqq ;             /* RBF radius and radius squared */
2330   float xmid , ymid , zmid ;     /* middle of the knots */
2331   float xscl , yscl , zscl ;     /* scale reciprocal of the knots */
2332   float *xknot, *yknot, *zknot ; /* each is an nknot-long vector */
2333   dmat44 Qmat ;                  /* 4x4 Q matrix for linear coefficents */
2334   rcmat *Lmat ;                  /* Choleski factor of M matrix */
2335   int uselin ;                   /* using linear coefficients? */
2336   float *P0, *Px , *Py , *Pz ;   /* each is an nknot-long vector */
2337 } RBF_knots ;
2338 
2339 #undef  DESTROY_RBF_knots
2340 #define DESTROY_RBF_knots(rk)                                            \
2341  do{ IFREE((rk)->xknot); IFREE((rk)->yknot); IFREE((rk)->zknot);         \
2342      IFREE((rk)->P0); IFREE((rk)->Px); IFREE((rk)->Py); IFREE((rk)->Pz); \
2343      rcmat_destroy((rk)->Lmat); free(rk);                                \
2344  } while(0)
2345 
2346 typedef struct {
2347   int npt ;                   /* number of grid points */
2348   float *xpt , *ypt , *zpt ;  /* grid points on which to evaluate RBF */
2349   RBFKINT *kfirst , *klast ;  /* first & last knot indexes for each grid pt */
2350 } RBF_evalgrid ;
2351 
2352 #undef  MAKE_RBF_evalgrid
2353 #define MAKE_RBF_evalgrid(rg,nn)                             \
2354  do{ (rg) = (RBF_evalgrid *)malloc(sizeof(RBF_evalgrid)) ;   \
2355      (rg)->npt = (nn) ;                                      \
2356      (rg)->xpt = (float *)calloc(sizeof(float),(nn)) ;       \
2357      (rg)->ypt = (float *)calloc(sizeof(float),(nn)) ;       \
2358      (rg)->zpt = (float *)calloc(sizeof(float),(nn)) ;       \
2359      (rg)->kfirst = (rg)->klast = NULL ;                     \
2360  } while(0)
2361 
2362 #undef  DESTROY_RBF_evalgrid
2363 #define DESTROY_RBF_evalgrid(rg)                             \
2364  do{ free((rg)->xpt); free((rg)->ypt); free((rg)->zpt);      \
2365      if( (rg)->klast != NULL ) free((rg)->klast ) ;          \
2366      if( (rg)->kfirst!= NULL ) free((rg)->kfirst) ;          \
2367      free(rg) ;                                              \
2368  } while(0)
2369 
2370 typedef struct {
2371   int code ;                /* 0 ==> val has func values; >0 ==> knot wts */
2372   float b0 , bx , by , bz ; /* linear polynomial coefficients */
2373   float *val ;              /* nknot of these */
2374 } RBF_evalues ;
2375 
2376 #undef  MAKE_RBF_evalues
2377 #define MAKE_RBF_evalues(rv,nn)                               \
2378  do{ (rv) = (RBF_evalues *)calloc(1,sizeof(RBF_evalues)) ;    \
2379      (rv)->code = 0 ;                                         \
2380      (rv)->val  = (float *)malloc(sizeof(float)*(nn)) ; } while(0)
2381 
2382 #undef  DESTROY_RBF_evalues
2383 #define DESTROY_RBF_evalues(rv) do{ free((rv)->val); free(rv); } while(0)
2384 
2385 extern RBF_knots * RBF_setup_knots( int, float, int, float *, float *, float * ) ;
2386 extern int RBF_setup_evalues( RBF_knots *rbk, RBF_evalues *rbe ) ;
2387 extern int RBF_evaluate( RBF_knots *, RBF_evalues *, RBF_evalgrid *, float * ) ;
2388 extern void RBF_set_verbosity( int ) ;
2389 extern void RBF_setup_kranges( RBF_knots *rbk , RBF_evalgrid *rbg ) ;
2390 
2391 /*----------------------------------------------------------------------------*/
2392 /** Test if a image is vector-valued (fvect, rgb, rgba, or complex) **/
2393 
2394 #undef  ISVECTIM
2395 #define ISVECTIM(tim) ((tim)->kind==MRI_fvect || (tim)->kind==MRI_rgb ||   \
2396                        (tim)->kind==MRI_rgba  || (tim)->kind==MRI_complex)
2397 
2398 /** Vectorize a call to an image producing function that takes as input
2399     1 float image and produces as output 1 float image.  To use this macro,
2400     first #define the CALLME macro which takes 2 arguments, the input image
2401     pointer and the output image name.  For example, to median filter a
2402     possible vector image named 'inim' into the output 'outim' do
2403 
2404  #undef  CALLME
2405  #define CALLME(inn,out) (out) = mri_medianfilter( (inn), irad,mask,verb )
2406  if( ISVECTIM(inim) ){ VECTORME(inim,outim) ; return outim ; }
2407  ... normal processing of a scalar image goes here
2408  ... idea is that the CALLME macro is recursive to the local function
2409  ... this example would be inserted into source code mri_medianfilter.c **/
2410 
2411 #undef  VECTORME
2412 #define VECTORME(inpp,outp)                                                   \
2413  do{ int vv ; MRI_IMARR *qxmpq=NULL; MRI_IMAGE *qxm=NULL;                     \
2414      (outp) = NULL ;                                                          \
2415      switch( (inpp)->kind ){                                                  \
2416        default:                                             break ;           \
2417        case MRI_fvect:   qxmpq = mri_fvect_to_imarr(inpp) ; break ;           \
2418        case MRI_rgb:     qxmpq = mri_rgb_to_3float (inpp) ; break ;           \
2419        case MRI_rgba:    qxmpq = mri_rgba_to_4float (inpp); break ;           \
2420        case MRI_complex: qxmpq = mri_complex_to_pair(inpp); break ;           \
2421      }                                                                        \
2422      if( qxmpq == NULL ) break ;                                              \
2423      for( vv=0 ; vv < IMARR_COUNT(qxmpq) ; vv++ ){                            \
2424        CALLME( IMARR_SUBIM(qxmpq,vv) , qxm ) ;                                \
2425        mri_free(IMARR_SUBIM(qxmpq,vv)) ;                                      \
2426        IMARR_SUBIM(qxmpq,vv) = qxm ;                                          \
2427      }                                                                        \
2428      switch( (inpp)->kind ){                                                  \
2429        default:          break ;                                              \
2430        case MRI_fvect:   (outp) = mri_imarr_to_fvect(qxmpq) ;                 \
2431                          break ;                                              \
2432        case MRI_rgb:     (outp) = mri_3to_rgb(IMARR_SUBIM(qxmpq,0),           \
2433                                               IMARR_SUBIM(qxmpq,1),           \
2434                                               IMARR_SUBIM(qxmpq,2) ) ;        \
2435                          break ;                                              \
2436        case MRI_rgba:    (outp) = mri_4to_rgba(IMARR_SUBIM(qxmpq,0),          \
2437                                                IMARR_SUBIM(qxmpq,1),          \
2438                                                IMARR_SUBIM(qxmpq,2),          \
2439                                                IMARR_SUBIM(qxmpq,3) ) ;       \
2440                          break ;                                              \
2441        case MRI_complex: (outp) = mri_pair_to_complex(IMARR_SUBIM(qxmpq,0),   \
2442                                                       IMARR_SUBIM(qxmpq,1) ); \
2443                          break ;                                              \
2444      }                                                                        \
2445      DESTROY_IMARR(qxmpq) ;                                                   \
2446    } while(0)
2447 /*----------------------------------------------------------------------------*/
2448 
2449 #ifndef MRILIB_MINI
2450 extern THD_3dim_dataset * THD_svdblur( THD_3dim_dataset *inset, byte *mask,
2451                                 float rad, int pdim, int nort, float **ort ) ;
2452 extern MRI_IMARR * THD_get_dset_nbhd_array( THD_3dim_dataset *dset, byte *mask,
2453                                             int xx, int yy, int zz, MCW_cluster *nbhd ) ;
2454 extern MRI_IMAGE * mri_svdproj( MRI_IMARR *imar , int nev ) ;
2455 extern MRI_IMAGE * mri_first_principal_vector( MRI_IMARR *imar ) ;
2456 extern int mri_principal_vectors( MRI_IMARR *imar, int nvec, float *sval, float *uvec ) ;
2457 #endif
2458 
2459 /*----------------------------------------------------------------------------*/
2460 #include "mri_nwarp.h"
2461 /*----------------------------------------------------------------------------*/
2462 /* Aug 2018 - sound stuff - cs_playsound.c */
2463 
2464 extern void play_sound_1D( int nn , float *xx ) ;
2465 extern void mri_play_sound( MRI_IMAGE *im , int ignore ) ;
2466 extern void set_sound_note_type( char *typ ) ;
2467 extern void set_sound_gain_value( int ggg ) ;
2468 extern void set_sound_twotone( int ggg ) ;      /* do not use this */
2469 extern char * get_sound_player(void) ;
2470 extern void sound_set_note_ADSR(int) ;
2471 
2472 extern void sound_write_au_header( FILE *fp, int nn, int srate, int code ) ;
2473 extern void sound_write_au_ulaw( char *fname, int nn, float *aa, int srate, float scl ) ;
2474 extern void sound_write_au_8PCM( char *fname, int nn, float *aa, int srate, float scl ) ;
2475 
2476 extern void sound_write_au_16PCM( char *fname, int nn, float *aa, int srate, float scl ) ;
2477 
2478 extern MRI_IMAGE * mri_sound_1D_to_FM( MRI_IMAGE *imin,
2479                                        float fbot, float ftop, int srate, int nsper ) ;
2480 
2481 extern void kill_sound_players(void) ;
2482 
2483 extern void mri_sound_play_append( char *app ) ;  /* 09 Aug 2019 */
2484 extern void mri_play_sound_notify( int ) ;
2485 extern void mri_play_sound_rate_fac( float fff ) ;     /* 23 Aug 2019 */
2486 
2487 #define SOUND_WAVEFORM_SINE     1
2488 #define SOUND_WAVEFORM_SQUARE   2
2489 #define SOUND_WAVEFORM_TRIANGLE 3
2490 #define SOUND_WAVEFORM_H2SINE   4
2491 #define SOUND_WAVEFORM_SQSINE   5
2492 
2493 #define SOUND_WAVECODE_BASE     1048576.0f
2494 
2495 extern void sound_set_note_waveform( int nn ) ;
2496 extern void sound_make_note( float frq, int waveform, int srate, int nsam, float *sam ) ;
2497 extern MRI_IMAGE * mri_sound_1D_to_notes( MRI_IMAGE *imin, int srate, int nsper,
2498                                           int ny, int ignore , int use_wavecodes ) ;
2499 
2500 /*----------------------------------------------------------------------------*/
2501 
2502 #define CPU_IS_64_BIT() ((sizeof(void *) == 8) ? 1 : 0 )
2503 
2504 #ifdef  __cplusplus
2505 }
2506 #endif
2507 
2508 #endif /* _MCW_MRILIB_HEADER_ */
2509