1 #include "mrilib.h"
2 
3 /*! Scale a float image to an integer type [20 Oct 2003]:
4      - inim   = MRI_float image
5      - kind   = integer type to scale to (MRI_byte, MRI_short, MRI_int)
6      - return = scaled image
7      - *sfac  = scale factor (nonzero on input ==> smallest value allowed)
8 */
9 
mri_scalize(MRI_IMAGE * inim,int kind,float * sfac)10 MRI_IMAGE * mri_scalize( MRI_IMAGE *inim , int kind , float *sfac )
11 {
12    float gtop , fac , fimfac ;
13    MRI_IMAGE *outim ;
14 
15 ENTRY("mri_scalize") ;
16 
17    if( inim == NULL            ||
18        inim->kind != MRI_float ||
19       sfac == NULL             ||
20       !MRI_IS_INT_TYPE(kind)     ) RETURN(NULL) ;
21 
22    fac = *sfac ; if( fac < 0.0 ) fac = 0.0 ;
23 
24    gtop = MCW_vol_amax( inim->nvox,1,1,MRI_float,MRI_FLOAT_PTR(inim) ) ;
25    if( gtop == 0.0 ){
26      fimfac = fac ;
27    } else {
28      fimfac = gtop / MRI_TYPE_maxval[kind] ;
29      if( fimfac < fac ) fimfac = fac ;
30    }
31    outim = mri_new_conforming( inim , kind ) ;
32    if( fimfac > 0.0 )
33      EDIT_coerce_scale_type( inim->nvox , 1.0/fimfac ,
34                              MRI_float , MRI_FLOAT_PTR(inim) ,
35                              outim->kind , mri_data_pointer(outim) ) ;
36    *sfac = fimfac ; RETURN(outim) ;
37 }
38