Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
mritopgm.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006    
00007 #include "mrilib.h"
00008 
00009 int main( int argc , char *argv[] )
00010 {
00011    MRI_IMAGE *im , *imb ;
00012    int narg ;
00013    float perc=0.0 ;
00014 
00015    if( argc < 2 || strncmp(argv[1],"-help",2) == 0 ){
00016       printf( "Converts an image to raw pgm format.\n") ;
00017       printf( "Results go to stdout and should be redirected.\n");
00018       printf( "Usage:   mritopgm [-pp] input_image\n" ) ;
00019       printf( "Example: mritopgm fred.001 | ppmtogif > fred.001.gif\n") ;
00020       printf( "\n"
00021               "  The '-pp' option expresses a clipping percentage.\n"
00022               "  That is, if this option is given, the pp%%-brightest\n"
00023               "  pixel is mapped to white; all above it are also white,\n"
00024               "  and all below are mapped linearly down to black.\n"
00025               "  The default is that pp=100; that is, the brightest\n"
00026               "  pixel is white.  A useful operation for many MR images is\n"
00027               "    mritopgm -99 fred.001 | ppmtogif > fred.001.gif\n"
00028               "  This will clip off the top 1%% of voxels, which are often\n"
00029               "  super-bright due to arterial inflow effects, etc.\n"
00030             ) ;
00031       exit(0) ;
00032    }
00033 
00034    machdep() ;
00035 
00036    narg = 1 ;
00037    if( argv[narg][0] == '-' ){
00038      perc = 0.01 * strtod(argv[narg]+1,NULL) ;
00039      if( perc < 0.01 || perc > 1.00 ){
00040        fprintf(stderr,"** Illegal percentage: %s\n",argv[narg]+1) ;
00041      }
00042      narg++ ;
00043    }
00044    im   = mri_read_just_one( argv[narg] ) ;
00045    if( im == NULL ) exit(1) ;
00046 
00047    if( perc > 0.0 ){
00048      float val = mri_quantile( im , perc ) ;
00049      int ii ;
00050      switch( im->kind ){
00051         case MRI_short:{
00052           short *ar = mri_data_pointer(im) ;
00053           for( ii=0 ; ii < im->nvox ; ii++ ) if( ar[ii] > val ) ar[ii] = val ;
00054         }
00055         break ;
00056         case MRI_byte:{
00057           byte *ar = mri_data_pointer(im) ;
00058           for( ii=0 ; ii < im->nvox ; ii++ ) if( ar[ii] > val ) ar[ii] = val ;
00059         }
00060         break ;
00061         case MRI_float:{
00062           float *ar = mri_data_pointer(im) ;
00063           for( ii=0 ; ii < im->nvox ; ii++ ) if( ar[ii] > val ) ar[ii] = val ;
00064         }
00065         break ;
00066         default:
00067           fprintf(stderr,"** Don't know how to percent clip this type of image!\n") ;
00068         break ;
00069      }
00070    }
00071 
00072    imb = mri_to_byte( im ) ;
00073    mri_free( im ) ;
00074 
00075    printf( "P5 %d %d 255\n" , imb->nx , imb->ny ) ;
00076    fwrite( imb->im.byte_data , imb->pixel_size , imb->nx * imb->ny , stdout ) ;
00077 
00078    exit(0) ;
00079 }