Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
ppmtoargb.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 Amax 0
00011 #define Asolid 1
00012 #define Ainten 2
00013
00014 int main( int argc , char * argv[] )
00015 {
00016 MRI_IMARR * ppmar ;
00017 MRI_IMAGE * rim , * gim , * bim ;
00018 byte * rby , * gby , * bby , * argb ;
00019 byte aa,rr,gg,bb ;
00020
00021 int Askip=0 , Rskip=0 , Gskip=0 , Bskip=0 ;
00022 int Aoff =0 , Roff =1 , Goff =2 , Boff =3 ;
00023 int Acalc = Amax ;
00024 int nx , ny , nz , nper=4 , iarg, ii,jj,kk , npix ;
00025
00026
00027
00028 if( argc < 2 || strncmp(argv[1],"-help",4) == 0 ){
00029 printf(
00030 "Usage: ppmtoargb [options] ppm_files\n"
00031 "-- Takes as input a bunch of raw PPM files and writes them\n"
00032 "-- out as a 3D brick of ARGB byte values, where\n"
00033 "-- A = max(r,g,b).\n"
00034 "-- Output is written to stdout and should be redirected!\n"
00035 "-- Dimensions of output brick are written to stderr.\n"
00036 "\n"
00037 "Options are:\n"
00038 " -ARGB = write out data in ARGB order [default].\n"
00039 " -ABGR = write out data in ABGR order [good for NCSA].\n"
00040 " -???? = up to four letters to specify output byte order;\n"
00041 " a missing letter means don't write that channel\n"
00042 " (e.g., -RGB means write R then G then B, and no A).\n"
00043 " In all cases, bytes for each voxel are written contiguously.\n"
00044 " -solid = Flag for alternate calculation of A, using\n"
00045 " A = r if (r==g && g==b) else A = 255 (max).\n"
00046 " -inten = Flag for alternate calculation of A, using\n"
00047 " A = .299*r + .587*g + .114*b (total intensity).\n"
00048 ) ;
00049 exit(0) ;
00050 }
00051
00052
00053
00054 iarg = 1 ;
00055 while( iarg < argc && argv[iarg][0] == '-' ){
00056
00057 if( strncmp(argv[iarg],"-solid",5) == 0 ){
00058 Acalc = Asolid ;
00059 iarg++ ; continue ;
00060 }
00061
00062 if( strncmp(argv[iarg],"-inten",5) == 0 ){
00063 Acalc = Ainten ;
00064 iarg++ ; continue ;
00065 }
00066
00067 if( argv[iarg][1] == 'A' || argv[iarg][1] == 'R' ||
00068 argv[iarg][1] == 'G' || argv[iarg][1] == 'B' ){
00069
00070 char * ch ;
00071
00072 nper = strlen(argv[iarg]) - 1 ;
00073 if( nper < 1 || nper > 4 ){
00074 fprintf(stderr,"Illegal order option %s\n",argv[iarg]) ; exit(-1) ; }
00075
00076 ch = strchr( argv[iarg] , 'A' ) ;
00077 if( ch == NULL ){ Askip = 1 ; }
00078 else { Askip = 0 ; Aoff = (ch - argv[iarg]) - 1 ; }
00079
00080 ch = strchr( argv[iarg] , 'B' ) ;
00081 if( ch == NULL ){ Bskip = 1 ; }
00082 else { Bskip = 0 ; Boff = (ch - argv[iarg]) - 1 ; }
00083
00084 ch = strchr( argv[iarg] , 'R' ) ;
00085 if( ch == NULL ){ Rskip = 1 ; }
00086 else { Rskip = 0 ; Roff = (ch - argv[iarg]) - 1 ; }
00087
00088 ch = strchr( argv[iarg] , 'G' ) ;
00089 if( ch == NULL ){ Gskip = 1 ; }
00090 else { Gskip = 0 ; Goff = (ch - argv[iarg]) - 1 ; }
00091
00092 if( nper + Askip + Bskip + Gskip + Rskip != 4 ){
00093 fprintf(stderr,"Illegal order option %s\n",argv[iarg]) ; exit(-1) ; }
00094
00095 #if 0
00096 fprintf(stderr,
00097 "Askip = %d Aoff = %d\n"
00098 "Bskip = %d Boff = %d\n"
00099 "Gskip = %d Goff = %d\n"
00100 "Rskip = %d Roff = %d\n" ,
00101 Askip,Aoff, Bskip,Boff, Gskip,Goff, Rskip,Roff ) ;
00102 #endif
00103
00104 iarg++ ; continue ;
00105 }
00106
00107
00108
00109 fprintf(stderr,"Unknown option %s\n",argv[iarg]) ; exit(-1) ;
00110 }
00111
00112
00113
00114 nz = argc - iarg ;
00115 if( nz < 1 ){
00116 fprintf(stderr,"No input images on command line!\n") ; exit(-1) ;
00117 }
00118
00119 for( kk=0 ; kk < nz ; kk++ ){
00120
00121 ppmar = mri_read_ppm3( argv[kk+iarg] ) ;
00122 if( ppmar == NULL || ppmar->num != 3 ){
00123 fprintf(stderr,"Cannot read input image file %s\n",argv[kk+iarg]) ;
00124 exit(-1) ;
00125 }
00126
00127 rim = ppmar->imarr[0] ; rby = mri_data_pointer( rim ) ;
00128 gim = ppmar->imarr[1] ; gby = mri_data_pointer( gim ) ;
00129 bim = ppmar->imarr[2] ; bby = mri_data_pointer( bim ) ;
00130
00131 FREE_IMARR(ppmar) ;
00132
00133 if( kk == 0 ){
00134 int kbytes ;
00135
00136 nx = rim->nx ; ny = rim->ny ; npix = nx*ny ;
00137
00138 argb = (byte *) malloc( sizeof(byte) * nper * nx * ny ) ;
00139 if( argb == NULL ){
00140 fprintf(stderr,"Cannot malloc workspace in ppmtoargb!\n") ;
00141 exit(-1) ;
00142 }
00143
00144 kbytes = 0.001 * nx * ny * nz * nper ;
00145 fprintf(stderr,
00146 "Output brick is %d x %d x %d (%d bytes per voxel) = %d Kbytes ",
00147 nx,ny,nz,nper , kbytes) ;
00148 fflush(stderr) ;
00149
00150 } else if( rim->nx != nx || rim->ny != ny ){
00151 fprintf(stderr,"Image mismatch in input image file %s\n",argv[kk+iarg]) ;
00152 exit(-1) ;
00153 }
00154
00155 jj = 0 ;
00156 for( ii=0 ; ii < npix ; ii++ ){
00157 rr = rby[ii] ; gg = gby[ii] ; bb = bby[ii] ;
00158
00159 if( !Rskip ) argb[jj+Roff] = rr ;
00160 if( !Gskip ) argb[jj+Goff] = gg ;
00161 if( !Bskip ) argb[jj+Boff] = bb ;
00162
00163 if( !Askip ){
00164 switch( Acalc ){
00165 default:
00166 case Amax:
00167 aa = MAX(rr,gg) ; aa = MAX(aa,bb) ;
00168 break ;
00169
00170 case Asolid:
00171 aa = (rr==gg && gg==bb) ? (rr) : (255) ;
00172 break ;
00173
00174 case Ainten:
00175 aa = (byte) (0.299*rr + 0.587*gg + 0.114*bb) ;
00176 break ;
00177 }
00178
00179 argb[jj+Aoff] = aa ;
00180 }
00181
00182 jj += nper ;
00183 }
00184
00185 fwrite( argb , sizeof(byte) , npix*nper , stdout ) ;
00186
00187 mri_free(rim) ; mri_free(gim) ; mri_free(bim) ;
00188
00189 if( kk%10 == 5 ) { fprintf(stderr,".") ; fflush(stderr) ; }
00190 }
00191
00192 fprintf(stderr,"\n") ;
00193 exit(0) ;
00194 }