1 #include "mrilib.h"
2
3 #define NBUF 16384
4
main(int argc,char * argv[])5 int main( int argc , char * argv[] )
6 {
7 char buf[NBUF] ;
8 int nn ; double ee,pp , cutper=0.0 ;
9
10 if( argc > 1 ){
11 if( strcmp(argv[1],"-help") == 0 ){
12 printf("Usage: ent16 [-%%nn]\n"
13 "Computes an estimate of the entropy of stdin.\n"
14 "If the flag '-%%75' is given (e.g.), then the\n"
15 " exit status is 1 only if the input could be\n"
16 " compressed at least 75%%, otherwise the exit\n"
17 " status is 0. Legal values of 'nn' are 1..99.\n"
18 "In any case, the entropy and compression estimates\n"
19 " are printed to stdout, even if no '-%%nn' flag is.\n"
20 " given.\n"
21 "\n"
22 "METHOD: entropy is estimated by building a histogram\n"
23 " of all 16 bit words in the input, then summing\n"
24 " over -p[i]*log2(p[i]), i=0..65535. Compression\n"
25 " estimate seems to work pretty good for gzip -1\n"
26 " in most cases of binary image data.\n"
27 "\n"
28 "SAMPLE USAGE (csh syntax):\n"
29 " ent16 -%%75 < fred+orig.BRIK\n"
30 " if( $status == 1 ) gzip -1v fred+orig.BRIK\n"
31 ) ;
32 exit(0) ;
33
34 } else if( strncmp(argv[1],"-%",2) == 0 ){
35 cutper = strtod( argv[1]+2 , NULL ) ;
36 if( cutper < 1.0 || cutper > 99.0 ) cutper = 0.0 ;
37
38 } else {
39 fprintf(stderr,"++ ent16: Unknown option %s ignored!\n",argv[1]) ;
40 }
41 }
42
43 machdep() ;
44
45 /*--------------------------*/
46
47 ENTROPY_setup() ;
48 do{
49 nn = fread( buf , 1 , NBUF , stdin ) ;
50 if( nn <= 0 ) break ;
51 ENTROPY_accumulate( nn , buf ) ;
52 } while(1) ;
53
54 /*--------------------------*/
55
56 ee = ENTROPY_compute() ; ENTROPY_setdown() ;
57 pp = 100.0*(1.0-ee/16.0) ;
58
59 printf("entropy=%6.3f bits/word compression=%5.2f%%\n",
60 ee , pp ) ;
61
62 if( cutper > 0.0 && pp >= cutper ) exit(1) ;
63 exit(0) ;
64 }
65