00001 #include "mrilib.h"
00002 #include "coxplot.h"
00003 #include "display.h"
00004
00005 #define TSGRAY_SEPARATE_YSCALE (1<<0)
00006 #define TSGRAY_FLIP_XY (1<<1)
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 MEM_plotdata * PLOT_tsgray( int npt , int nts , int ymask , float **y )
00020 {
00021 MEM_plotdata * mp ;
00022 float ybot,ytop , yfac , dx,dy , val ;
00023 int ii,jj , flipxy ;
00024 char str[32] ;
00025 int sepscl ;
00026
00027 if( npt < 2 || nts < 1 || y == NULL ) return NULL ;
00028
00029
00030
00031 ybot = ytop = y[0][0] ;
00032 for( jj=0 ; jj < nts ; jj++ ){
00033 for( ii=0 ; ii < npt ; ii++ ){
00034 val = y[jj][ii] ;
00035 if( ybot > val ) ybot = val ;
00036 else if( ytop < val ) ytop = val ;
00037 }
00038 }
00039 if( ybot >= ytop ) return NULL ;
00040 yfac = 1.0/(ytop-ybot) ;
00041
00042 dx = 1.0/npt ;
00043 dy = 1.0/nts ;
00044
00045 create_memplot_surely( "Gplot" , 1.0 ) ;
00046 set_color_memplot( 0.0 , 0.0 , 0.0 ) ;
00047 set_thick_memplot( 0.0 ) ;
00048
00049 flipxy = (ymask & TSGRAY_FLIP_XY) != 0 ;
00050 sepscl = (ymask & TSGRAY_SEPARATE_YSCALE) != 0 ;
00051
00052 for( jj=0 ; jj < nts ; jj++ ){
00053
00054 if( sepscl ){
00055 ybot = ytop = y[jj][0] ;
00056 for( ii=1 ; ii < npt ; ii++ ){
00057 val = y[jj][ii] ;
00058 if( ybot > val ) ybot = val ;
00059 else if( ytop < val ) ytop = val ;
00060 }
00061 if( ybot >= ytop ) yfac = 1.0 ;
00062 else yfac = 1.0/(ytop-ybot) ;
00063 }
00064
00065 for( ii=0 ; ii < npt ; ii++ ){
00066 val = yfac*(ytop-y[jj][ii]) ;
00067 set_color_memplot( val,val,val ) ;
00068 if( flipxy )
00069 plotrect_memplot( ii*dx,jj*dy , (ii+1)*dx,(jj+1)*dy ) ;
00070 else
00071 plotrect_memplot( jj*dy,1.0-ii*dx , (jj+1)*dy,1.0-(ii+1)*dy ) ;
00072 }
00073 }
00074
00075 set_color_memplot( 0.0 , 0.0 , 0.0 ) ;
00076 mp = get_active_memplot() ;
00077 return mp ;
00078 }
00079
00080
00081
00082 #define DEFAULT_NCOLOVR 20
00083
00084 static char * INIT_colovr[DEFAULT_NCOLOVR] = {
00085 "#ffff00" , "#ffcc00" , "#ff9900" , "#ff6900" , "#ff4400" , "#ff0000" ,
00086 "#0000ff" , "#0044ff" , "#0069ff" , "#0099ff" , "#00ccff" , "#00ffff" ,
00087 "green" , "limegreen" , "violet" , "hotpink" ,
00088 "white" , "#dddddd" , "#bbbbbb" , "black"
00089 } ;
00090
00091 static char * INIT_labovr[DEFAULT_NCOLOVR] = {
00092 "yellow" , "yell-oran" , "oran-yell" , "orange" , "oran-red" , "red" ,
00093 "dk-blue", "blue" , "lt-blue1" , "lt-blue2" , "blue-cyan", "cyan" ,
00094 "green" , "limegreen" , "violet" , "hotpink" ,
00095 "white" , "gry-dd" , "gry-bb" , "black"
00096 } ;
00097
00098 void startup_timeout_CB( XtPointer client_data , XtIntervalId * id ) ;
00099
00100 static MCW_DC * dc ;
00101 static int npt , nts , ymask=0 ;
00102 static float **yar ;
00103
00104
00105
00106 int main( int argc , char * argv[] )
00107 {
00108 int iarg ;
00109 int install=0 , ignore=0 , use=0 , jj ;
00110 char * tsfile , * cpt ;
00111 char dname[THD_MAX_NAME] , subv[THD_MAX_NAME] ;
00112 MRI_IMAGE * flim ;
00113 float *far ;
00114 XtAppContext app ;
00115 Widget shell ;
00116 int out_ps=0 ;
00117
00118
00119
00120 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
00121 printf("Usage: 1dgrayplot [options] tsfile\n"
00122 "Graphs the columns of a *.1D type time series file to the screen,\n"
00123 "sort of like 1dplot, but in grayscale.\n"
00124 "\n"
00125 "Options:\n"
00126 " -install = Install a new X11 colormap (for X11 PseudoColor)\n"
00127 " -ignore nn = Skip first 'nn' rows in the input file\n"
00128 " [default = 0]\n"
00129 " -flip = Plot x and y axes interchanged.\n"
00130 " [default: data columns plotted DOWN the screen]\n"
00131 " -sep = Separate scales for each column.\n"
00132 " -use mm = Plot 'mm' points\n"
00133 " [default: all of them]\n"
00134 " -ps = Don't draw plot in a window; instead, write it\n"
00135 " to stdout in PostScript format.\n"
00136 " N.B.: If you view this result in 'gv', you should\n"
00137 " turn 'anti-alias' off, and switch to\n"
00138 " landscape mode.\n"
00139
00140 "\n"
00141 TS_HELP_STRING
00142 ) ;
00143 exit(0) ;
00144 }
00145
00146 machdep() ;
00147
00148
00149
00150 for( jj=1 ; jj < argc ; jj++ )
00151 if( strcmp(argv[jj],"-ps") == 0 ){ out_ps = 1; break; }
00152
00153
00154
00155 if( !out_ps ){
00156 shell = XtVaAppInitialize(
00157 &app , "AFNI" , NULL , 0 , &argc , argv , NULL , NULL ) ;
00158 if( shell == NULL ){
00159 fprintf(stderr,"** Cannot initialize X11!\n") ; exit(1) ;
00160 }
00161 }
00162
00163 cpt = my_getenv("TMPDIR") ;
00164
00165
00166
00167 iarg = 1 ;
00168 while( iarg < argc && argv[iarg][0] == '-' ){
00169
00170 if( strcmp(argv[iarg],"-ps") == 0 ){
00171 iarg++ ; continue ;
00172 }
00173
00174 if( strcmp(argv[iarg],"-flip") == 0 ){
00175 ymask |= TSGRAY_FLIP_XY ; iarg++ ; continue ;
00176 }
00177
00178 if( strcmp(argv[iarg],"-sep") == 0 ){
00179 ymask |= TSGRAY_SEPARATE_YSCALE ; iarg++ ; continue ;
00180 }
00181
00182 if( strcmp(argv[iarg],"-install") == 0 ){
00183 install++ ; iarg++ ; continue ;
00184 }
00185
00186 if( strcmp(argv[iarg],"-ignore") == 0 ){
00187 ignore = strtod( argv[++iarg] , NULL ) ;
00188 if( ignore < 0 ){fprintf(stderr,"** Illegal -ignore value!\n");exit(1);}
00189 iarg++ ; continue ;
00190 }
00191
00192 if( strcmp(argv[iarg],"-use") == 0 ){
00193 use = strtod( argv[++iarg] , NULL ) ;
00194 if( use < 2 ){fprintf(stderr,"** Illegal -use value!\n");exit(1);}
00195 iarg++ ; continue ;
00196 }
00197
00198 fprintf(stderr,"** Unknown option: %s\n",argv[iarg]) ; exit(1) ;
00199 }
00200
00201 if( iarg >= argc ){
00202 fprintf(stderr,"** No tsfile on command line!\n") ; exit(1) ;
00203 }
00204
00205 if( !out_ps )
00206 dc = MCW_new_DC( shell , 16 ,
00207 DEFAULT_NCOLOVR , INIT_colovr , INIT_labovr ,
00208 1.0 , install ) ;
00209
00210 tsfile = argv[iarg] ;
00211 flim = mri_read_1D( tsfile ) ;
00212 if( flim == NULL ){
00213 fprintf(stderr,"** Can't read input file %s\n",tsfile) ;
00214 exit(1);
00215 }
00216 if( ignore >= flim->nx-1 ) ignore = 0 ;
00217
00218 if( use == 0 || use > flim->nx-ignore ) use = flim->nx-ignore ;
00219
00220 far = MRI_FLOAT_PTR(flim) ;
00221 yar = (float **) malloc( sizeof(float *) * flim->ny ) ;
00222 for( jj=0 ; jj < flim->ny ; jj++ )
00223 yar[jj] = far + (jj*flim->nx + ignore) ;
00224
00225 npt = use ;
00226 nts = flim->ny ;
00227
00228
00229
00230 if( !out_ps ){
00231 (void) XtAppAddTimeOut( app , 123 , startup_timeout_CB , NULL ) ;
00232 XtAppMainLoop(app) ;
00233 }
00234
00235
00236
00237 { MEM_plotdata *mp ;
00238 mp = PLOT_tsgray( npt , nts , ymask , yar ) ;
00239 memplot_to_postscript( "-" , mp ) ;
00240 }
00241 exit(0) ;
00242 }
00243
00244
00245
00246
00247
00248 void killfunc(void * fred){ exit(0) ; }
00249
00250
00251 void startup_timeout_CB( XtPointer client_data , XtIntervalId * id )
00252 {
00253 MEM_plotdata * mp ;
00254
00255 mp = PLOT_tsgray( npt , nts , ymask , yar ) ;
00256
00257 if( mp != NULL )
00258 (void) memplot_to_topshell( dc->display , mp , killfunc ) ;
00259
00260 return ;
00261 }