Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
ppmtocmap.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "mrilib.h"
00008
00009 #define MAXCOL 255
00010 byte rgb[3*MAXCOL] ;
00011 int ncol = 0 ;
00012
00013 #define ASP(x,y) ( (x)[0]=(y)[0] , (x)[1]=(y)[1] , (x)[2]=(y)[2] )
00014 #define EQP(x,y) ( (x)[0]==(y)[0] && (x)[1]==(y)[1] && (x)[2]==(y)[2] )
00015
00016 int main( int argc , char * argv[] )
00017 {
00018 int makemap , iarg , ii,jj ;
00019 char * mapfile = NULL ;
00020 MRI_IMAGE * mapim = NULL , * ppmim = NULL , * qqim = NULL ;
00021 byte * ppmar , * mapar , * qqar ;
00022
00023 if( argc < 2 || strncmp(argv[1],"-help",5)==0 ){
00024 printf("Usage 1: Make a map of the colors in a ppm raw file\n"
00025 " ppmtocmap -make ppmfile\n"
00026 "\n"
00027 "Usage 2: Make a color mapped pgm file from a map and a raw ppm file\n"
00028 " ppmtocmap -map mapfile ppmfile\n"
00029 "\n"
00030 "Both usages write to stdout.\n"
00031 ) ;
00032 exit(0) ;
00033 }
00034
00035 iarg = 1 ;
00036 if( strncmp(argv[1],"-make",5) == 0 ){
00037 makemap = 1 ; iarg++ ;
00038 } else if( strncmp(argv[1],"-map",4) == 0 ){
00039 makemap = 0 ; iarg++ ;
00040 if( iarg >= argc ){ fprintf(stderr,"No file after -map\n") ; exit(1) ; }
00041 mapfile = argv[iarg++] ;
00042 mapim = mri_read_ppm( mapfile ) ;
00043 if( mapim == NULL ){fprintf(stderr,"Can't read -map file %s\n",mapfile);exit(1);}
00044 mapar = MRI_RGB_PTR(ppmim) ;
00045 } else {
00046 fprintf(stderr,"Illegal first switch: %s\n",argv[1]) ;
00047 exit(1) ;
00048 }
00049
00050 if( iarg >= argc ){ fprintf(stderr,"No input ppmfile?\n") ; exit(1) ; }
00051
00052 ppmim = mri_read_ppm( argv[iarg] ) ;
00053 if( ppmim == NULL ){fprintf(stderr,"Can't read ppmfile %s\n",argv[iarg]);exit(1);}
00054 ppmar = MRI_RGB_PTR(ppmim) ;
00055
00056 qqim = (makemap) ? ppmim : mapim ;
00057 qqar = MRI_RGB_PTR(qqim) ;
00058
00059 ASP( rgb , qqar ) ; ncol = 1 ;
00060 for( ii=1 ; ii < qqim->nvox ; ii++ ){
00061 for( jj=0 ; jj < ncol ; jj++ )
00062 if( EQP(rgb+3*jj,qqar+3*ii) ) break ;
00063
00064 if( jj == ncol ){
00065 ASP( rgb+3*ncol , qqar+3*ii ) ;
00066 ncol++ ;
00067 if( ncol > MAXCOL ){
00068 fprintf(stderr,"More than %d colors at pixel %d\n",MAXCOL,ii) ;
00069 exit(1) ;
00070 }
00071 }
00072 }
00073 fprintf(stderr,"found %d colors\n",ncol) ;
00074
00075 if( makemap ){
00076 printf("P6 %d 1 255\n" , ncol ) ;
00077 fwrite( rgb , 1 , 3*ncol , stdout ) ;
00078 exit(0) ;
00079 }
00080
00081 qqim = mri_new( ppmim->nx , ppmim->ny , MRI_byte ) ;
00082 qqar = MRI_BYTE_PTR(qqim) ;
00083
00084 for( ii=0 ; ii < ppmim->nvox ; ii++ ){
00085 for( jj=0 ; jj < ncol ; jj++ )
00086 if( EQP(rgb+3*jj,ppmar+3*ii) ) break ;
00087
00088 if( jj < ncol ) qqar[ii] = jj ;
00089 else { fprintf(stderr,"Unmatched color at pixel %d\n",ii) ; exit(1) ; }
00090 }
00091
00092 printf("P5 %d %d 255\n" , ppmim->nx , ppmim->ny ) ;
00093 fwrite( qqar , 1 , ppmim->nx * ppmim->ny , stdout ) ;
00094 exit(0) ;
00095 }