1 #include "mcw_malloc.h"
2 #include "cox_render.h"
3 
4 /*
5    -- I have no idea why I wrote this program
6    -- I suppose to test the 3D rendering functions in cox_render.c
7    -- git blame dates it to the year 2000, so that is plausible
8    -- In any case, this program is probably useless except for
9       the eventual biographer of AFNI.
10    -- RWCox
11 */
12 
main(int argc,char * argv[])13 int main( int argc , char * argv[] )
14 {
15    THD_3dim_dataset *dset ;
16    int iarg=1 ;
17    char *cc1="x",*cc2="y",*cc3="z" ;
18    float th1=0.0, th2=0.0, th3=0.0 ;
19    float thx,thy,thz ;
20    int   axx,ayy,azz ;
21    char *fname="testcox.ppm" , fn[128] ;
22    void * rhand ;
23    int bot=1 , ii , nim=0 ;
24    float omap[128] , bfac ;
25    MRI_IMAGE * im , * brim ;
26    int hbr[256] , nperc,ibot,itop,sum ;
27    byte * bar ;
28    double ctim ;
29    int imode=CREN_TWOSTEP ;
30    int pmode=CREN_SUM_VOX ;
31 
32    if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
33       printf("Usage: testcox [-rotate a b c] [-mip|-MIP] [-out f] [-bot b] [-nn|-ts|-li] dset\n") ;
34       exit(0) ;
35    }
36 
37 #ifdef USING_MCW_MALLOC
38    enable_mcw_malloc() ;
39 #endif
40 
41    while( iarg < argc && argv[iarg][0] == '-' ){
42 
43       if( strcmp(argv[iarg],"-MIP") == 0 ){
44         pmode = CREN_MIP_VOX ; iarg++ ; continue ;
45       }
46       if( strcmp(argv[iarg],"-mip") == 0 ){
47         pmode = CREN_MINIP_VOX ; iarg++ ; continue ;
48       }
49 
50       if( strcmp(argv[iarg],"-nn") == 0 ){
51          imode = CREN_NN ;
52          iarg++ ; continue ;
53       }
54 
55       if( strcmp(argv[iarg],"-ts") == 0 ){
56          imode = CREN_TWOSTEP ;
57          iarg++ ; continue ;
58       }
59 
60       if( strcmp(argv[iarg],"-li") == 0 ){
61          imode = CREN_LINEAR ;
62          iarg++ ; continue ;
63       }
64 
65       if( strcmp(argv[iarg],"-bot") == 0 ){
66          bot = strtod( argv[++iarg] , NULL ) ;
67          iarg++ ; continue ;
68       }
69 
70       if( strcmp(argv[iarg],"-rotate") == 0 ){
71          th1 = (PI/180.0) * strtod( argv[++iarg] , &cc1 ) ;
72          th2 = (PI/180.0) * strtod( argv[++iarg] , &cc2 ) ;
73          th3 = (PI/180.0) * strtod( argv[++iarg] , &cc3 ) ;
74 
75          iarg++ ; continue ;
76       }
77 
78       if( strcmp(argv[iarg],"-out") == 0 ){
79          fname = argv[++iarg] ;
80          iarg++ ; continue ;
81       }
82 
83       fprintf(stderr,"Illegal option: %s\n",argv[iarg]); exit(1);
84    }
85 
86    if( iarg >= argc ){fprintf(stderr,"No dataset?\n"); exit(1); }
87 
88    dset = THD_open_dataset( argv[iarg] ) ;
89    if( dset == NULL ){fprintf(stderr,"Can't open dataset!\n");exit(1);}
90    if( DSET_BRICK_TYPE(dset,0) != MRI_byte ){
91       fprintf(stderr,"Non-byte dataset input!\n");exit(1);
92    }
93    DSET_mallocize(dset) ; DSET_load(dset) ;
94    if( !DSET_LOADED(dset) ){
95       fprintf(stderr,"Can't load dataset!\n");exit(1);
96    }
97 
98    rhand = new_CREN_renderer() ;
99 
100 #if 0
101    THD_rotangle_user_to_dset( dset ,
102                               th1,*cc1  , th2,*cc2  , th3,*cc3 ,
103                               &thx,&axx , &thy,&ayy , &thz,&azz ) ;
104    CREN_set_viewpoint( rhand , axx,thx,ayy,thy,azz,thz ) ;
105 #else
106    CREN_set_angles( rhand , th1,th2,th3 ) ;
107 #endif
108 
109    for( ii=0 ; ii < 128 ; ii++ )
110       omap[ii] = (ii <= bot) ? 0.0
111                              : (ii-bot)/(127.0-bot) ;
112 
113    CREN_set_opamap( rhand , omap , 1.0 ) ;
114 
115    brim = DSET_BRICK(dset,0) ; bar = MRI_BYTE_PTR(brim) ;
116    mri_histobyte( brim , hbr ) ;
117    nperc = 0.02 * brim->nvox ;
118    for( sum=0,ibot=0   ; ibot < 128  && sum < nperc ; ibot++ ) sum += hbr[ibot] ;
119    for( sum=0,itop=255 ; itop > ibot && sum < nperc ; itop-- ) sum += hbr[itop] ;
120    if( ibot >= itop ){ ibot = 64 ; itop = 192 ; }
121    bfac = 127.5 / (itop-ibot) ;
122    for( ii=0 ; ii < brim->nvox ; ii++ )
123            if( bar[ii] <= ibot ) bar[ii] = 0 ;
124       else if( bar[ii] >= itop ) bar[ii] = 127 ;
125       else                       bar[ii] = bfac * (bar[ii]-ibot) ;
126 
127 
128    ctim = COX_cpu_time() ;
129 
130    CREN_set_databytes( rhand , brim->nx,brim->ny,brim->nz , bar ) ;
131    CREN_dset_axes( rhand , dset ) ;
132 
133    CREN_set_render_mode( rhand , pmode ) ;
134 
135    CREN_set_interp( rhand , imode ) ;
136 
137    for( th3=0 ; th3 < 360.0 ; th3+=5.0 ){
138       CREN_set_angles( rhand , th1,th2,(PI/180.0)*th3 ) ;
139       im = CREN_render( rhand, NULL ) ;  /* added NULL   2002.08.28 - rickr */
140       if( im == NULL ){
141         fprintf(stderr,"renderer fails!\n") ; exit(1) ;
142       }
143 
144       sprintf(fn,"tc%03d.jpg",(int)rint(th3)) ;
145       mri_write_pnm( fn, im ) ;
146       fprintf(stderr,"+++ Output to file %s\n",fn);
147       mri_free(im) ; nim++ ;
148    }
149    ctim = COX_cpu_time() - ctim ;
150    fprintf(stderr,"+++ Rendering CPU time = %g s = %g/im\n",ctim,ctim/nim) ;
151 
152    exit(0) ;
153 }
154