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 "mrilib.h"
8 #include <string.h>
9 
main(int argc,char * argv[])10 int main( int argc , char * argv[] )
11 {
12    MRI_IMAGE *imin , *imout ;
13    short *shin , *shout ;
14    int nopt=1 , nxim , nyim , ii , kindim , npix , thresh=0 , nout ;
15 
16    if( argc < 4 || strncmp(argv[1],"-help",4) == 0 ){
17       printf("Usage: imand [-thresh #] input_images ... output_image\n"
18              "* Only pixels nonzero in all input images\n"
19              "* (and above the threshold, if given) will be output.\n" ) ;
20       exit(0) ;
21    }
22 
23    machdep() ;
24 
25    if( strncmp(argv[1],"-thresh",4) == 0 ){
26       thresh = strtol( argv[2] , NULL , 0 ) ;
27       thresh = ABS(thresh) ;
28       nopt = 3 ;
29       if( argc < 5 ){fprintf(stderr,"not enough files!\n");exit(1);}
30    }
31 
32    imout = mri_read_just_one( argv[nopt++] ) ;
33    if( imout == NULL ) exit(1) ;
34    nxim   = imout->nx ;
35    nyim   = imout->ny ;
36    kindim = imout->kind ;
37    npix   = nxim * nyim ;
38    if( kindim != MRI_short || !MRI_IS_2D(imout) ){
39       fprintf(stderr,"imand currently only works for 2D short images!\n") ;
40       exit(1) ;
41    }
42    shout = mri_data_pointer( imout ) ;
43    for( ii=0 ; ii < npix ; ii++ )
44       if( ABS(shout[ii]) <= thresh ) shout[ii] = 0 ;
45 
46    while( nopt < argc-1 ){
47 
48       imin = mri_read_just_one( argv[nopt] ) ;
49       if( imin == NULL ) exit(1) ;
50       if( imin->nx!=nxim || imin->ny!=nyim || imin->kind!=kindim || !MRI_IS_2D(imin) ){
51          fprintf(stderr,
52                  "image %s doesn't conform to first image!\n",argv[nopt]) ;
53          exit(1) ;
54       }
55       shin = mri_data_pointer( imin ) ;
56 
57       for( ii=0 ; ii < npix ; ii++ ){
58          if( ABS(shin[ii]) <= thresh ){
59             shout[ii] = 0 ;
60           } else if( ((shout[ii] >  thresh) && (shin[ii] > shout[ii])) ||
61                      ((shout[ii] < -thresh) && (shin[ii] < shout[ii]))   ){
62             shout[ii] = shin[ii] ;
63           }
64       }
65 
66       mri_free( imin ) ;
67       nopt++ ;
68 
69    } /* end of while loop */
70 
71    nout = 0 ;
72    for( ii=0 ; ii < npix ; ii++ ) if( shout[ii] != 0 ) nout++ ;
73 
74    mri_write( argv[argc-1] , imout ) ;
75 
76    printf("number of nonzero pixels output = %d\n",nout) ;
77    exit(0) ;
78 }
79