Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
imand.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006    
00007 #include "mrilib.h"
00008 #include <string.h>
00009 
00010 #define ABS(x)  ( ((x)>=0) ? (x) : (-(x)) )
00011 
00012 int main( int argc , char * argv[] )
00013 {
00014    MRI_IMAGE *imin , *imout ;
00015    short *shin , *shout ;
00016    int nopt=1 , nxim , nyim , ii , kindim , npix , thresh=0 , nout ;
00017 
00018    if( argc < 4 || strncmp(argv[1],"-help",4) == 0 ){
00019       fprintf(stderr,"Usage: imand [-thresh #] input_images ... output_image\n"
00020                      "* Only pixels nonzero in all input images\n"
00021                      "* (and above the threshold, if given) will be output.\n" ) ;
00022       exit(0) ;
00023    }
00024 
00025    machdep() ;
00026 
00027    if( strncmp(argv[1],"-thresh",4) == 0 ){
00028       thresh = strtol( argv[2] , NULL , 0 ) ;
00029       thresh = ABS(thresh) ;
00030       nopt = 3 ;
00031       if( argc < 5 ){fprintf(stderr,"not enough files!\n");exit(1);}
00032    }
00033 
00034    imout = mri_read_just_one( argv[nopt++] ) ;
00035    if( imout == NULL ) exit(1) ;
00036    nxim   = imout->nx ;
00037    nyim   = imout->ny ;
00038    kindim = imout->kind ;
00039    npix   = nxim * nyim ;
00040    if( kindim != MRI_short || !MRI_IS_2D(imout) ){
00041       fprintf(stderr,"imand currently only works for 2D short images!\n") ;
00042       exit(1) ;
00043    }
00044    shout = mri_data_pointer( imout ) ;
00045    for( ii=0 ; ii < npix ; ii++ )
00046       if( ABS(shout[ii]) <= thresh ) shout[ii] = 0 ;
00047 
00048    while( nopt < argc-1 ){
00049 
00050       imin = mri_read_just_one( argv[nopt] ) ;
00051       if( imin == NULL ) exit(1) ;
00052       if( imin->nx!=nxim || imin->ny!=nyim || imin->kind!=kindim || !MRI_IS_2D(imin) ){
00053          fprintf(stderr,
00054                  "image %s doesn't conform to first image!\n",argv[nopt]) ;
00055          exit(1) ;
00056       }
00057       shin = mri_data_pointer( imin ) ;
00058 
00059       for( ii=0 ; ii < npix ; ii++ ){
00060          if( ABS(shin[ii]) <= thresh ){
00061             shout[ii] = 0 ;
00062           } else if( ((shout[ii] >  thresh) && (shin[ii] > shout[ii])) ||
00063                      ((shout[ii] < -thresh) && (shin[ii] < shout[ii]))   ){
00064             shout[ii] = shin[ii] ;
00065           }
00066       }
00067 
00068       mri_free( imin ) ;
00069       nopt++ ;
00070 
00071    } 
00072 
00073    nout = 0 ;
00074    for( ii=0 ; ii < npix ; ii++ ) if( shout[ii] != 0 ) nout++ ;
00075 
00076    mri_write( argv[argc-1] , imout ) ;
00077 
00078    printf("number of nonzero pixels output = %d\n",nout) ;
00079    exit(0) ;
00080 }