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 
main(int argc,char * argv[])9 int main( int argc , char *argv[] )
10 {
11    MRI_IMAGE *im , *imb ;
12    int narg ;
13    float perc=0.0 ;
14 
15    if( argc < 2 || strncmp(argv[1],"-help",2) == 0 ){
16       printf( "Converts an image to raw pgm format.\n") ;
17       printf( "Results go to stdout and should be redirected.\n");
18       printf( "Usage:   mritopgm [-pp] input_image\n" ) ;
19       printf( "Example: mritopgm fred.001 | ppmtogif > fred.001.gif\n") ;
20       printf( "\n"
21               "  The '-pp' option expresses a clipping percentage.\n"
22               "  That is, if this option is given, the pp%%-brightest\n"
23               "  pixel is mapped to white; all above it are also white,\n"
24               "  and all below are mapped linearly down to black.\n"
25               "  The default is that pp=100; that is, the brightest\n"
26               "  pixel is white.  A useful operation for many MR images is\n"
27               "    mritopgm -99 fred.001 | ppmtogif > fred.001.gif\n"
28               "  This will clip off the top 1%% of voxels, which are often\n"
29               "  super-bright due to arterial inflow effects, etc.\n"
30             ) ;
31       exit(0) ;
32    }
33 
34    machdep() ;
35 
36    narg = 1 ;
37    if( argv[narg][0] == '-' ){
38      perc = 0.01 * strtod(argv[narg]+1,NULL) ;
39      if( perc < 0.01 || perc > 1.00 ){
40        fprintf(stderr,"** Illegal percentage: %s\n",argv[narg]+1) ;
41      }
42      narg++ ;
43    }
44    im = mri_read_just_one( argv[narg] ) ;
45    if( im == NULL ) exit(1) ;
46 
47    if( im->kind != MRI_float ){
48      imb = mri_to_float(im) ; mri_free(im) ; im = imb ;
49    }
50 
51    if( perc > 0.0 ){
52      float val = mri_quantile( im , perc ) ;
53      float *ar = mri_data_pointer(im) ;
54      int ii ;
55      for( ii=0 ; ii < im->nvox ; ii++ ) if( ar[ii] > val ) ar[ii] = val ;
56    }
57 
58    imb = mri_to_byte( im ) ; mri_free( im ) ;
59 
60    printf( "P5 %d %d 255\n" , imb->nx , imb->ny ) ;
61    fwrite( MRI_BYTE_PTR(imb) , imb->pixel_size , imb->nx * imb->ny , stdout ) ;
62 
63    exit(0) ;
64 }
65