Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

toxx.c

Go to the documentation of this file.
00001 #include "mrilib.h"
00002 #include <string.h>
00003 
00004 #define MMAX 82   /* max # colors */
00005 #define NOUT 61   /* max # chars per output line */
00006 
00007 static byte rmap[MMAX], gmap[MMAX], bmap[MMAX] ;          /* color map */
00008 
00009 static char alpha[MMAX] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"    /* color codes */
00010                           "abcdefghijklmnopqrstuvwxyz"
00011                           ",<.>/?;:'[{]}|=+-_)(*&^%$#@!`~" ;
00012 
00013 static char num[10] = "0123456789" ;                      /* number codes */
00014 
00015 int main( int argc , char * argv[] )
00016 {
00017    MRI_IMAGE *im ;
00018    byte *bp , rr,bb,gg ;
00019    int ii,jj,nmap,kk , nn , qq , nlin=0 ;
00020    char out[NOUT+1] , zout[NOUT+1] , cc,nc , *nam ;
00021 
00022    if( argc < 2 ){
00023      printf("Usage: toxx NAME input.ppm > output.xx\n"
00024             "Probably get the input by\n"
00025             "  ppmquant xx image.ppm > input.ppm\n"
00026             "where 'xx' is the number of colors to use.\n"
00027             "Max value of 'xx' is %d.\n" , MMAX
00028            ) ;
00029      exit(0);
00030    }
00031 
00032    nam = argv[1] ;
00033    im = mri_read( argv[2] ) ;
00034    if( im == NULL ){
00035      fprintf(stderr,"** Can't read file %s\n",argv[2]); exit(1);
00036    }
00037    switch( im->kind ){
00038      case MRI_rgb:
00039        fprintf(stderr,"++ Input is color %dx%d\n",im->nx,im->ny) ;
00040      break ;
00041      case MRI_byte:{
00042        MRI_IMAGE *qim = mri_to_rgb(im) ;
00043        mri_free(im) ; im = qim ;
00044        fprintf(stderr,"++ Input is grayscale %dx%d\n",im->nx,im->ny) ;
00045      }
00046      break ;
00047      default:
00048        fprintf(stderr,"** Input %s is wrong kind of image!\n",argv[2]); exit(1);
00049    }
00050    bp = MRI_RGB_PTR(im) ;
00051 
00052    /* build color map (MMAX or fewer entries allowed) */
00053 
00054    rmap[0] = bp[0] ; gmap[0] = bp[1] ; bmap[0] = bp[2] ;
00055 
00056    for( ii=nmap=1 ; ii < im->nvox ; ii++ ){
00057      rr = bp[3*ii] ; gg = bp[3*ii+1] ; bb = bp[3*ii+2] ;
00058      for( kk=0 ; kk < nmap ; kk++ ){
00059        if( rr==rmap[kk] && gg==gmap[kk] && bb==bmap[kk] ) break ;
00060      }
00061      if( kk == nmap ){  /* new color */
00062        if( nmap == MMAX ){
00063          fprintf(stderr,"** Too many colors in input %s\n",argv[2]); exit(1);
00064        }
00065        rmap[nmap] = rr ; gmap[nmap] = gg ; bmap[nmap] = bb ;
00066        nmap++ ;
00067      }
00068    }
00069 
00070    fprintf(stderr,"++ Input has %d distinct colors\n",nmap) ;
00071 
00072    nn = 0 ; qq = 0 ;
00073    printf( "#define NX_%s %d\n",nam,im->nx) ;
00074    printf( "#define NY_%s %d\n",nam,im->ny) ;
00075    printf( "#define NC_%s %d\n",nam,nmap  ) ;
00076 
00077    printf( "static byte RMAP_%s[] = {\n " , nam ) ;
00078    for( kk=0 ; kk < nmap ; kk++ )
00079      printf("%d%s",(int)rmap[kk] , (kk==nmap-1) ? "};\n" : "," ) ;
00080 
00081    printf( "static byte GMAP_%s[] = {\n " , nam ) ;
00082    for( kk=0 ; kk < nmap ; kk++ )
00083      printf("%d%s",(int)gmap[kk] , (kk==nmap-1) ? "};\n" : "," ) ;
00084 
00085    printf( "static byte BMAP_%s[] = {\n " , nam ) ;
00086    for( kk=0 ; kk < nmap ; kk++ )
00087      printf("%d%s",(int)bmap[kk] , (kk==nmap-1) ? "};\n" : "," ) ;
00088 
00089    printf( "static char *BAR_%s[] = {\n" , nam ) ;
00090    for( ii=0 ; ii < im->nvox ; ii++ ){
00091       rr = bp[3*ii] ; gg = bp[3*ii+1] ; bb = bp[3*ii+2] ;
00092       for( kk=0 ; kk < nmap ; kk++ ){
00093         if( rr==rmap[kk] && gg==gmap[kk] && bb==bmap[kk] ) break ;
00094       }
00095 
00096       out[nn++] = alpha[kk] ;
00097 
00098       if( nn == NOUT && ii < im->nvox-1 ){  /* output line is full; RLE it */
00099         out[nn] = '\0' ;
00100         cc = out[0] ; qq = 1 ; kk = 0 ;
00101         for( jj=1 ; jj <= nn ; jj++ ){
00102           nc = out[jj] ;
00103           if( nc == cc ){  /* same character */
00104             if( qq == 9 ){ zout[kk++] = num[qq] ; zout[kk++] = cc ; qq = 0 ; }
00105             qq++ ;
00106           } else {         /* new character */
00107             if( qq == 1 ){
00108               zout[kk++] = cc ; qq = 1 ;
00109             } else if( qq == 2 ){
00110               zout[kk++] = cc ; zout[kk++] = cc ; qq = 1 ;
00111             } else {
00112               zout[kk++] = num[qq] ; zout[kk++] = cc ; qq = 1 ;
00113             }
00114           }
00115           cc = nc ;
00116         }
00117         zout[kk] = '\0' ;
00118         printf("   \"%s\",\n",zout) ; nlin++ ; nn = 0 ;
00119       }
00120    }
00121 
00122    /* put the last line out (no RLE here) */
00123 
00124    out[nn] = '\0' ;
00125    printf("   \"%s\"\n};\n",out) ; nlin++ ;
00126    printf("#define NLINE_%s %d\n",nam,nlin) ;
00127    exit(0) ;
00128 }
 

Powered by Plone

This site conforms to the following standards: