Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

thd_entropy16.c

Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*----- 02 Mar 2001 - RWCox - Used for AUTOGZIP decision -------------*/
00003 
00004 #include "mrilib.h"
00005 
00006 #undef SNUM
00007 #define SNUM 65536  /* one for every possible 16 bit pattern */
00008 
00009 static int * scount=NULL ;  /* holds count of unsigned shorts */
00010 static int   snum=0 ;       /* total number of unsigned shorts processed */
00011 
00012 /*-----------------------------------------------------------------------*/
00013 
00014 void ENTROPY_setup(void)
00015 {
00016    if( scount == NULL ) scount = (int *) malloc(sizeof(int)*SNUM) ;
00017    memset( scount , 0 , sizeof(int)*SNUM ) ;
00018    snum = 0 ;
00019 }
00020 
00021 /*-----------------------------------------------------------------------*/
00022 
00023 void ENTROPY_setdown(void)
00024 {
00025    if( scount != NULL ){ free(scount); scount = NULL; snum = 0; }
00026 }
00027 
00028 /*-----------------------------------------------------------------------*/
00029 
00030 void ENTROPY_accumulate( int nbytes , void * var )
00031 {
00032    int nn = nbytes/2 , ii ;
00033    unsigned short * sar = (unsigned short *) var ;
00034 
00035    if( scount == NULL ) ENTROPY_setup() ;
00036 
00037    for( ii=0 ; ii < nn ; ii++ ) scount[sar[ii]]++ ;
00038    snum += nn ;
00039 }
00040 
00041 /*-----------------------------------------------------------------------
00042   Value returned is in bits per 16 bit unsigned short
00043 -------------------------------------------------------------------------*/
00044 
00045 double ENTROPY_compute(void)
00046 {
00047    register int ii ;
00048    register double sum ;
00049 
00050    if( scount == NULL || snum == 0 ) return 0.0 ;
00051 
00052    sum = 0.0 ;
00053    for( ii=0 ; ii < SNUM ; ii++ )
00054       if( scount[ii] > 0 ) sum += scount[ii] * log((double)scount[ii]) ;
00055 
00056    sum = -(sum - snum*log((double)snum)) / ( log(2.0) * snum ) ;
00057    return sum ;
00058 }
00059 
00060 /*-----------------------------------------------------------------------*/
00061 
00062 double ENTROPY_dataset( THD_3dim_dataset * dset )
00063 {
00064    if( !ISVALID_DSET(dset) ) return(0.0) ;
00065    DSET_load(dset) ;
00066    if( !DSET_LOADED(dset) ) return(0.0) ;
00067    return ENTROPY_datablock( dset->dblk ) ;
00068 }
00069 
00070 /*-----------------------------------------------------------------------*/
00071 
00072 double ENTROPY_datablock( THD_datablock * blk )
00073 {
00074    int iv ;
00075    double sum ;
00076 
00077 ENTRY("ENTROPY_datablock") ;
00078 
00079    ENTROPY_setup() ;
00080 
00081    for( iv=0 ; iv < blk->nvals ; iv++ )
00082       ENTROPY_accumulate( DBLK_BRICK_BYTES(blk,iv) , DBLK_ARRAY(blk,iv) ) ;
00083 
00084    sum = ENTROPY_compute() ;
00085    ENTROPY_setdown() ;
00086    RETURN(sum) ;
00087 }
 

Powered by Plone

This site conforms to the following standards: