00001
00002
00003
00004
00005
00006
00007 #undef MAIN
00008 #include "afni.h"
00009
00010
00011
00012 int label_in_PALTAB( PBAR_palette_table * pt , char * lab )
00013 {
00014 int ii ;
00015 if( pt == NULL || PALTAB_NUM(pt) == 0 || lab == NULL || lab[0] == '\0' )
00016 return -1 ;
00017
00018 for( ii=0 ; ii < PALTAB_NUM(pt) ; ii++ )
00019 if( strcmp( PALTAB_ARR_LABEL(pt,ii) , lab ) == 0 ) return ii ;
00020
00021 return -1 ;
00022 }
00023
00024
00025
00026
00027
00028 #define ISTARRED(s) ( (s)[0]=='*' && (s)[1]=='*' && (s)[2]=='*' )
00029
00030 #define EOLSKIP \
00031 do{ for( ; fptr[0] != '\n' && fptr[0] != '\0' ; fptr++ ) ; \
00032 if( fptr[0] == '\0' ){ free(fbuf) ; EXRETURN ; } \
00033 fptr++ ; } while(0)
00034
00035 #define GETSSS \
00036 do{ int nu=0,qq; \
00037 if( fptr-fbuf >= nbuf || fptr[0] == '\0' ){ free(fbuf); EXRETURN; } \
00038 str[0]='\0'; qq=sscanf(fptr,"%127s%n",str,&nu); nused+=nu;fptr+=nu; \
00039 if( str[0]=='\0' || qq==0 || nu==0 ){ free(fbuf); EXRETURN; } \
00040 } while(0)
00041
00042 #define GETSTR \
00043 do{ GETSSS ; \
00044 while(str[0]=='!' || (str[0]=='/' && str[1]=='/') || \
00045 (str[0]=='#' && str[1]=='\0') ){EOLSKIP; GETSSS;} \
00046 } while(0)
00047
00048 #define GETEQN \
00049 do{ GETSTR ; if(ISTARRED(str)) goto SkipSection ; \
00050 strcpy(left,str) ; \
00051 GETSTR ; if(ISTARRED(str)) goto SkipSection ; \
00052 strcpy(middle,str) ; \
00053 GETSTR ; if(ISTARRED(str)) goto SkipSection ; \
00054 strcpy(right,str) ; } while(0)
00055
00056 #define NSBUF 256
00057
00058 void AFNI_process_setup( char * fname , int mode , MCW_DC * dc )
00059 {
00060 int nbuf , nused , ii ;
00061 char * fbuf , * fptr ;
00062 char str[NSBUF] , left[NSBUF] , middle[NSBUF] , right[NSBUF] ;
00063
00064 ENTRY("AFNI_process_setup") ;
00065
00066 fbuf = AFNI_suck_file( fname ) ; if( fbuf == NULL ) EXRETURN ;
00067 nbuf = strlen(fbuf) ; if( nbuf == 0 ) EXRETURN ;
00068
00069 fptr = fbuf ; nused = 0 ;
00070
00071
00072
00073 str[0] = '\0' ;
00074
00075 if(PRINT_TRACING)
00076 { char str[256] ;
00077 sprintf(str,"Reading AFNI setup file = %s (%d bytes)",fname,nbuf) ;
00078 STATUS(str);}
00079
00080 while( nused < nbuf ){
00081
00082
00083
00084
00085 SkipSection: while( ! ISTARRED(str) ){ GETSTR; }
00086
00087
00088
00089 if( mode == SETUP_ENVIRON_MODE && strcmp(str,"***ENVIRONMENT") != 0 ){
00090 GETSTR ;
00091 goto SkipSection ;
00092 }
00093
00094
00095
00096
00097 if( strcmp(str,"***COLORS") == 0 ){
00098 char label[NSBUF] , defn[NSBUF] ;
00099
00100 STATUS("enter ***COLORS") ;
00101
00102 while(1){
00103 GETEQN ;
00104
00105 if(PRINT_TRACING)
00106 { char str[256] ;
00107 sprintf(str,"GETEQN: %s %s %s",left,middle,right) ; STATUS(str);}
00108
00109
00110
00111 if( strcmp(left,"none")==0 || strcmp(right,"none")==0 ) continue ;
00112 if( !THD_filename_pure(left) ) continue ;
00113
00114 if( mode == SETUP_INIT_MODE ){
00115 if( INIT_ncolovr < MAX_NCOLOVR ){
00116 ii = INIT_ncolovr++ ;
00117 INIT_labovr[ii] = XtNewString(left) ;
00118 INIT_colovr[ii] = XtNewString(right) ;
00119
00120 if(PRINT_TRACING)
00121 { char str[256] ;
00122 sprintf(str,"setup into #%d",ii) ; STATUS(str);}
00123
00124 } else {
00125 fprintf(stderr,"\nIn setup file %s, color table overflow!\n",fname);
00126 goto SkipSection ;
00127 }
00128 } else if( mode == SETUP_LATER_MODE ){
00129 ii = DC_add_overlay_color( dc , right , left ) ;
00130
00131 if(PRINT_TRACING)
00132 { char str[256] ;
00133 sprintf(str,"'new' color index returned as #%d",ii) ; STATUS(str);}
00134
00135 if( ii < 0 )
00136 fprintf(stderr,"\nIn setup file %s, unknown color %s\n",fname,right);
00137 else
00138 OVC_mostest( dc->ovc ) ;
00139 }
00140 }
00141 continue ;
00142 }
00143
00144
00145
00146
00147 if( strcmp(str,"***PALETTES") == 0 ){
00148 char label[NSBUF] = "NoThing" , ccc , * cpt ;
00149 PBAR_palette_array * ppar=NULL ;
00150 PBAR_palette ** ppp ;
00151 PBAR_palette * ppnew ;
00152 int npane , pmode , icol=0 , jj ;
00153 float val ;
00154
00155 STATUS("enter ***PALETTES") ;
00156
00157 if( GPT == NULL ){
00158 STATUS("create initial palettes") ;
00159 INIT_PALTAB(GPT) ;
00160 INIT_PALARR(ppar,"NoThing") ;
00161 ADDTO_PALTAB(GPT,ppar) ;
00162 }
00163
00164
00165
00166 while(1){
00167 GETSTR ; if( ISTARRED(str) ) goto SkipSection ;
00168 if( fptr-fbuf >= nbuf ){ free(fbuf) ; EXRETURN ; }
00169
00170 if( str[0] != '[' ){
00171 strcpy(label,str) ;
00172 if( !THD_filename_ok(label) ){
00173 fprintf(stderr,"\nIn setup file %s, bad palette label: %s.\n",
00174 fname,label) ;
00175 free(fbuf) ; EXRETURN ;
00176 }
00177
00178 if(PRINT_TRACING)
00179 { char str[256] ;
00180 sprintf(str,"found palette label=%s. [len=%d label[0]=%d]",
00181 label,(int)strlen(label),(int)label[0]); STATUS(str);
00182 sprintf(str,"nbuf=%d fptr-fbuf=%d",nbuf,fptr-fbuf); STATUS(str);}
00183
00184 ii = label_in_PALTAB( GPT , label ) ;
00185 if( ii < 0 ){
00186 STATUS("making a new palette array") ;
00187 INIT_PALARR(ppar,label) ;
00188 ADDTO_PALTAB(GPT,ppar) ;
00189 } else {
00190
00191 if(PRINT_TRACING)
00192 { char str[256] ;
00193 sprintf(str,"matches old palette array #%d",ii) ; STATUS(str);}
00194
00195 ppar = PALTAB_ARR(GPT,ii) ;
00196 }
00197 GETSTR ; if( ISTARRED(str) ) goto SkipSection ;
00198 }
00199
00200 if(PRINT_TRACING)
00201 { char str[256] ;
00202 sprintf(str,"GPT now has %d arrays",PALTAB_NUM(GPT)) ; STATUS(str);}
00203
00204 if( str[0] != '[' ){
00205 fprintf(stderr,"\nIn setup file %s, expected palette '[n]' here: %s\n",
00206 fname , str ) ;
00207 break ;
00208 }
00209
00210
00211 ii = sscanf( str , "[%d%c" , &npane , &ccc ) ;
00212 if( ii < 2 ){
00213 fprintf(stderr,"\nIn setup file %s, can't interpret palette %s\n",
00214 fname , str ) ;
00215 break ;
00216 } else if( npane < NPANE_MIN || npane > NPANE_MAX ){
00217 fprintf(stderr,"\nIn setup file %s, illegal palette count %s\n",
00218 fname , str ) ;
00219 break ;
00220 }
00221
00222 pmode = (ccc == '+') ? 1 : 0 ;
00223 ppp = (pmode==0) ? (ppar->psgn + npane)
00224 : (ppar->ppos + npane) ;
00225
00226 if(PRINT_TRACING)
00227 { char str[256] ;
00228 sprintf(str,"palette definition: npane=%d pmode=%d",npane,pmode) ;
00229 STATUS(str); }
00230
00231 ppnew = XtNew(PBAR_palette) ;
00232 ppnew->npane = npane ;
00233 ppnew->mode = pmode ;
00234 for( ii=0 ; ii < npane ; ii++ ){
00235 ppnew->val[ii] = PAL_FIGNORE ; ppnew->col[ii] = PAL_IIGNORE ;
00236 }
00237
00238
00239
00240 for( ii=0 ; ii < npane ; ii++ ){
00241 GETEQN ;
00242
00243 if(PRINT_TRACING)
00244 { char str[256] ;
00245 sprintf(str,"GETEQN: %s %s %s",left,middle,right) ; STATUS(str);}
00246
00247 val = strtod(left,&cpt) ;
00248 if( val == 0.0 && *cpt != '\0' ) val = PAL_FIGNORE ;
00249
00250 if( mode == SETUP_INIT_MODE ){
00251 if( strcmp(right,"none") == 0 ){
00252 icol = 0 ;
00253 } else {
00254 for( jj=0 ; jj < INIT_ncolovr ; jj++ )
00255 if( strcmp(right,INIT_labovr[jj]) == 0 ) break ;
00256 icol = (jj < INIT_ncolovr) ? jj+1 : PAL_IIGNORE ;
00257 }
00258 } else if( mode == SETUP_LATER_MODE ){
00259 icol = DC_find_overlay_color( dc , right ) ;
00260 if( icol < 0 ) icol = PAL_IIGNORE ;
00261
00262 if(PRINT_TRACING)
00263 { char str[256] ;
00264 sprintf(str," DC_find_overlay_color(%s) returns %d\n",right,icol) ;
00265 STATUS(str) ; }
00266
00267 }
00268 ppnew->val[ii] = val ; ppnew->col[ii] = icol ;
00269
00270 if(PRINT_TRACING)
00271 { char str[256] ;
00272 sprintf(str,"new palette entry #%d: val=%f col=%d",ii,val,icol) ;
00273 STATUS(str);}
00274
00275 }
00276
00277 ii = check_PBAR_palette( ppnew ) ;
00278 if( ii < 0 ){
00279 fprintf(stderr,"\nIn setup file %s, palette '%s [%d%s' is illegal\n",
00280 fname,label,npane, (pmode==0)?"]":"+]" ) ;
00281 myXtFree(ppnew) ;
00282 } else {
00283 myXtFree(*ppp) ;
00284 *ppp = ppnew ;
00285
00286 STATUS("stored new palette") ;
00287 }
00288 }
00289
00290 continue ;
00291 }
00292
00293
00294
00295
00296 if( strcmp(str,"***ENVIRONMENT") == 0 ){
00297 char *enveqn ; int nl , nr ;
00298
00299 if( mode != SETUP_ENVIRON_MODE ){ GETSTR ; goto SkipSection ; }
00300
00301 STATUS("enter ***ENVIRONMENT") ;
00302
00303 while(1){
00304 GETEQN ;
00305
00306 if(PRINT_TRACING)
00307 { char str[256] ;
00308 sprintf(str,"GETEQN: %s %s %s",left,middle,right) ; STATUS(str);}
00309
00310 if( !THD_filename_pure(left) ) continue ;
00311
00312 nl = strlen(left) ; nr = strlen(right) ;
00313 enveqn = (char *) malloc(nl+nr+4) ;
00314 strcpy(enveqn,left) ; strcat(enveqn,"=") ; strcat(enveqn,right) ;
00315 putenv(enveqn) ;
00316 }
00317
00318 continue ;
00319 }
00320
00321
00322
00323 if( strcmp(str,"***END") == 0 ) break ;
00324
00325
00326
00327 #if 0
00328 fprintf(stderr,"\nIn setup file %s, unknown section: %s\n",
00329 fname , str ) ;
00330 break ;
00331 #else
00332 GETSTR ; goto SkipSection ;
00333 #endif
00334
00335 }
00336
00337 free(fbuf) ; EXRETURN ;
00338 }
00339
00340
00341
00342 int check_PBAR_palette( PBAR_palette * pp )
00343 {
00344 int ii , nn ;
00345
00346 ENTRY("check_PBAR_palette") ;
00347
00348 if( pp == NULL ) RETURN(-1) ;
00349 if( pp->npane < NPANE_MIN || pp->npane > NPANE_MAX ) RETURN(-1) ;
00350 if( pp->mode < 0 || pp->mode >= PANE_MAXMODE ) RETURN(-1) ;
00351
00352
00353
00354 nn = 0 ;
00355 for( ii=0 ; ii < pp->npane ; ii++ )
00356 if( pp->val[ii] == PAL_FIGNORE ) nn++ ;
00357 if( nn > 0 && nn != pp->npane ) RETURN(-1) ;
00358
00359
00360
00361 if( nn == 0 ){
00362 if( pp->val[0] <= 0.0 ) RETURN(-1) ;
00363 for( ii=1 ; ii < pp->npane ; ii++ )
00364 if( pp->val[ii] >= pp->val[ii-1] ) RETURN(-1) ;
00365
00366 if( pp->mode == 1 && pp->val[pp->npane-1] < 0.0 ) RETURN(-1) ;
00367 }
00368
00369 RETURN(1) ;
00370 }
00371
00372
00373 char * dump_PBAR_palette_table( int verb )
00374 {
00375 int ii , jj , nn , nsss,nuuu , nbuf , kk ;
00376 char * sss ;
00377 static char buf[2048] ;
00378 char s1[32] , s2[32] ;
00379 PBAR_palette * pp ;
00380 MCW_DC * dc = GLOBAL_library.dc ;
00381
00382 ENTRY("dump_PBAR_palette_table") ;
00383
00384 nsss = 256 ; sss = (char *) malloc(sizeof(char) * nsss) ;
00385 sss[0] = '\0' ; nuuu = 0 ;
00386
00387 if( verb ){
00388 sprintf(sss,"Overlay Color Table: \n") ; nuuu = strlen(sss) ;
00389 for( kk=1 ; kk < dc->ovc->ncol_ov ; kk++ ){
00390 sprintf(buf," %s = %s\n" , dc->ovc->label_ov[kk] , dc->ovc->name_ov[kk] ) ;
00391 nbuf = strlen(buf) ;
00392 if( nbuf+nuuu+2 > nsss ){
00393 nsss = nbuf+nuuu+128 ;
00394 sss = (char *) realloc( sss , sizeof(char) * nsss ) ;
00395 }
00396 strcat(sss,buf) ; nuuu = strlen(sss) ;
00397 }
00398 }
00399
00400 if( GPT == NULL || PALTAB_NUM(GPT) == 0 ){
00401 sprintf(buf,"\nPalette Table: *** EMPTY ***\n") ;
00402 nbuf = strlen(buf) ;
00403 if( nbuf+nuuu+2 > nsss ){
00404 nsss = nbuf+nuuu+128 ;
00405 sss = (char *) realloc( sss , sizeof(char) * nsss ) ;
00406 }
00407 strcat(sss,buf) ; nuuu = strlen(sss) ;
00408 RETURN(sss) ;
00409 }
00410
00411 sprintf(buf,"\nPalette Table:\n") ;
00412 nbuf = strlen(buf) ;
00413 if( nbuf+nuuu+2 > nsss ){
00414 nsss = nbuf+nuuu+128 ;
00415 sss = (char *) realloc( sss , sizeof(char) * nsss ) ;
00416 }
00417 strcat(sss,buf) ; nuuu = strlen(sss) ;
00418
00419 for( ii=0 ; ii < PALTAB_NUM(GPT) ; ii++ ){
00420 if( PALTAB_ARR(GPT,ii) == NULL ){
00421 sprintf(buf,"#%2d: *** EMPTY PALETTE ***\n",ii) ;
00422 } else {
00423 sprintf(buf,"#%2d: %s has" ,
00424 ii,PALTAB_ARR_LABEL(GPT,ii) ); nbuf = strlen(buf) ;
00425 nn = 0 ;
00426 for( jj=NPANE_MIN ; jj <= NPANE_MAX ; jj++ ){
00427 if( (pp=PALTAB_ARR_PSGN(GPT,ii,jj)) != NULL ){
00428 if( verb ){ sprintf(buf+nbuf,"\n") ; nbuf = strlen(buf) ; }
00429 sprintf(buf+nbuf," [%d]",jj) ; nn++ ; nbuf = strlen(buf) ;
00430 if( verb ){
00431 for( kk=0 ; kk < jj ; kk++ ){
00432 sprintf(buf+nbuf,"\n %s -> %f",
00433 dc->ovc->label_ov[pp->col[kk]] , pp->val[kk] ) ;
00434 nbuf = strlen(buf) ;
00435 }
00436 } else {
00437 if(nn%10 == 0){sprintf(buf+nbuf,"\n "); nbuf=strlen(buf);}
00438 }
00439 }
00440 if( (pp=PALTAB_ARR_PPOS(GPT,ii,jj)) != NULL ){
00441 if( verb ){ sprintf(buf+nbuf,"\n") ; nbuf = strlen(buf) ; }
00442 sprintf(buf+nbuf," [%d+]",jj) ; nn++ ; nbuf = strlen(buf) ;
00443 if( verb ){
00444 for( kk=0 ; kk < jj ; kk++ ){
00445 if( pp->col[kk] >= 0 ) strcpy(s1,dc->ovc->label_ov[pp->col[kk]]) ;
00446 else strcpy(s1,"IGNORE") ;
00447
00448 if( pp->val[kk] != PAL_FIGNORE ) sprintf(s2,"%f",pp->val[kk]) ;
00449 else strcpy(s2,"IGNORE") ;
00450
00451 sprintf(buf+nbuf,"\n %s -> %s", s1,s2 ) ;
00452 nbuf = strlen(buf) ;
00453 }
00454 } else {
00455 if(nn%10 == 0){sprintf(buf+nbuf,"\n "); nbuf=strlen(buf);}
00456 }
00457 }
00458 }
00459 if( nn == 0) sprintf(buf+nbuf," nothing \n") ;
00460 else if( verb || nn%10 != 0) sprintf(buf+nbuf,"\n") ;
00461 }
00462 nbuf = strlen(buf) ;
00463 if( nbuf+nuuu+2 > nsss ){
00464 nsss = nbuf+nuuu+128 ;
00465 sss = (char *) realloc( sss , sizeof(char) * nsss ) ;
00466 }
00467 strcat(sss,buf) ; nuuu = strlen(sss) ;
00468 }
00469 RETURN(sss) ;
00470 }
00471
00472
00473
00474 void load_PBAR_palette_array( MCW_pbar *pbar, PBAR_palette_array *par, int fixim )
00475 {
00476 int ii , jj , jm , nn ;
00477 PBAR_palette *pp ;
00478
00479 ENTRY("load_PBAR_palette_array") ;
00480
00481 if( pbar == NULL || par == NULL ) EXRETURN ;
00482
00483 nn = 0 ;
00484 for( jj=NPANE_MIN ; jj <= NPANE_MAX ; jj++ ){
00485 pp = par->psgn[jj] ; jm = 0 ;
00486 if( pp != NULL ){
00487 if( pp->val[0] != PAL_FIGNORE ){
00488 for( ii=0 ; ii < jj ; ii++ )
00489 pbar->pval_save[jj][ii][jm] = pp->val[ii] ;
00490 pbar->pval_save[jj][jj][jm] = - pp->val[0] ;
00491 }
00492
00493 for( ii=0 ; ii < jj ; ii++ )
00494 if( pp->col[ii] >= 0 && pp->col[ii] < pbar->dc->ovc->ncol_ov )
00495 pbar->ovin_save[jj][ii][jm] = pp->col[ii] ;
00496
00497 nn++ ;
00498 }
00499
00500 pp = par->ppos[jj] ; jm = 1 ;
00501 if( pp != NULL ){
00502 if( pp->val[0] != PAL_FIGNORE ){
00503 for( ii=0 ; ii < jj ; ii++ )
00504 pbar->pval_save[jj][ii][jm] = pp->val[ii] ;
00505 pbar->pval_save[jj][jj][jm] = 0.0 ;
00506 }
00507
00508 for( ii=0 ; ii < jj ; ii++ )
00509 if( pp->col[ii] >= 0 && pp->col[ii] < pbar->dc->ovc->ncol_ov )
00510 pbar->ovin_save[jj][ii][jm] = pp->col[ii] ;
00511
00512 nn++ ;
00513 }
00514 }
00515
00516 if( nn > 0 && !pbar->bigmode ){
00517 Three_D_View * im3d = (Three_D_View *) pbar->parent ;
00518 if( fixim ){ HIDE_SCALE(im3d) ; }
00519 alter_MCW_pbar( pbar , 0 , NULL ) ;
00520 if( fixim ){ FIX_SCALE_SIZE(im3d) ; }
00521 }
00522 EXRETURN ;
00523 }
00524
00525
00526
00527 char * AFNI_palette_label_CB( MCW_arrowval * av , XtPointer cd )
00528 {
00529 static char blab[32] ;
00530
00531 if( av->ival >= 0 && av->ival < PALTAB_NUM(GPT) )
00532 sprintf(blab,"%-.14s",PALTAB_ARR_LABEL(GPT,av->ival)) ;
00533 else
00534 strcpy(blab,"???") ;
00535
00536 return blab ;
00537 }
00538
00539
00540
00541
00542
00543 void AFNI_pbar_EV( Widget w , XtPointer cd ,
00544 XEvent *ev , Boolean *continue_to_dispatch )
00545 {
00546 Three_D_View *im3d = (Three_D_View *) cd ;
00547
00548 ENTRY("AFNI_pbar_EV") ;
00549
00550 if( ! IM3D_OPEN(im3d) ) EXRETURN ;
00551
00552 switch( ev->type ){
00553 case ButtonPress:{
00554 XButtonEvent *event = (XButtonEvent *) ev ;
00555 im3d->vwid->butx = event->x_root ;
00556 im3d->vwid->buty = event->y_root ;
00557 event->button = Button3 ;
00558 XmMenuPosition( im3d->vwid->func->pbar_menu , event ) ;
00559 XtManageChild ( im3d->vwid->func->pbar_menu ) ;
00560 }
00561 break ;
00562 }
00563
00564 EXRETURN ;
00565 }
00566
00567
00568
00569
00570
00571 void AFNI_pbar_CB( Widget w , XtPointer cd , XtPointer cbs )
00572 {
00573 Three_D_View *im3d = (Three_D_View *) cd ;
00574 MCW_pbar *pbar ;
00575 int npane , jm , ii ;
00576 double pmax , pmin ;
00577 float pval[NPANE_MAX+1] ;
00578
00579 ENTRY("AFNI_pbar_CB") ;
00580
00581 if( ! IM3D_OPEN(im3d) ) EXRETURN ;
00582
00583 pbar = im3d->vwid->func->inten_pbar ;
00584 npane = pbar->num_panes ;
00585 jm = pbar->mode ;
00586 pmax = pbar->pval_save[npane][0][jm] ;
00587 pmin = pbar->pval_save[npane][npane][jm] ;
00588
00589
00590
00591 if( w == im3d->vwid->func->pbar_equalize_pb ){
00592
00593 if( pbar->bigmode ){ BEEPIT; EXRETURN; }
00594
00595 for( ii=0 ; ii <= npane ; ii++ )
00596 pval[ii] = pmax - ii * (pmax-pmin)/npane ;
00597
00598 HIDE_SCALE(im3d) ;
00599 alter_MCW_pbar( pbar , 0 , pval ) ;
00600 FIX_SCALE_SIZE(im3d) ;
00601 }
00602
00603
00604
00605 else if( w == im3d->vwid->func->pbar_settop_pb ){
00606 MCW_choose_integer( im3d->vwid->func->options_label ,
00607 "Pbar Top" , 0 , 99999 , 1 ,
00608 AFNI_set_pbar_top_CB , cd ) ;
00609 }
00610
00611
00612
00613 else if( w == im3d->vwid->func->pbar_readin_pb ){
00614 XmString xstr ;
00615
00616 AFNI_make_file_dialog( im3d ) ;
00617 XtAddCallback( im3d->vwid->file_sbox , XmNokCallback ,
00618 AFNI_finalize_read_palette_CB , cd ) ;
00619 XtAddCallback( im3d->vwid->file_sbox , XmNcancelCallback ,
00620 AFNI_finalize_read_palette_CB , cd ) ;
00621 XtAddCallback( im3d->vwid->file_sbox , XmNhelpCallback ,
00622 AFNI_finalize_read_palette_CB , cd ) ;
00623 im3d->vwid->file_cb = AFNI_finalize_read_palette_CB ;
00624 im3d->vwid->file_cd = cd ;
00625 XtVaSetValues( im3d->vwid->file_dialog,
00626 XmNtitle,
00627 (pbar->bigmode) ? "AFNI: Read colorscale"
00628 : "AFNI: Read Palette" ,
00629 NULL ) ;
00630
00631 xstr = XmStringCreateLtoR( "*.pal" , XmFONTLIST_DEFAULT_TAG ) ;
00632 XtVaSetValues( im3d->vwid->file_sbox , XmNpattern , xstr , NULL ) ;
00633 XmStringFree(xstr) ;
00634
00635 XtPopup( im3d->vwid->file_dialog , XtGrabNone ) ;
00636 RWC_visibilize_widget( im3d->vwid->file_dialog ) ;
00637 }
00638
00639
00640
00641 else if( w == im3d->vwid->func->pbar_writeout_pb ){
00642 MCW_choose_string( im3d->vwid->func->options_label ,
00643 (pbar->bigmode) ? "Colorscale Name" : "Palette Name" ,
00644 NULL , AFNI_finalize_write_palette_CB , cd ) ;
00645 }
00646
00647
00648
00649 else if( w == im3d->vwid->func->pbar_showtable_pb ){
00650 char * dum = dump_PBAR_palette_table(1) ;
00651 new_MCW_textwin( im3d->vwid->func->options_label, dum, TEXT_READONLY ) ;
00652 free(dum) ;
00653 }
00654
00655
00656
00657 else if( w == im3d->vwid->func->pbar_saveim_pb ){
00658 MCW_choose_string( im3d->vwid->func->options_label ,
00659 "PPM file prefix" , NULL ,
00660 AFNI_finalize_saveim_CB , cd ) ;
00661 }
00662
00663
00664
00665 else if( w == im3d->vwid->func->pbar_environment_pb &&
00666 w != NULL ){
00667
00668 AFNI_misc_CB( im3d->vwid->dmode->misc_environ_pb ,
00669 (XtPointer) im3d , (XtPointer) NULL ) ;
00670 }
00671
00672
00673
00674
00675 EXRETURN ;
00676 }
00677
00678
00679
00680 void AFNI_palette_av_CB( MCW_arrowval * av , XtPointer cd )
00681 {
00682 Three_D_View * im3d = (Three_D_View *) cd ;
00683
00684 ENTRY("AFNI_palette_av_CB") ;
00685
00686 if( ! IM3D_VALID(im3d) || GPT == NULL ) EXRETURN ;
00687 if( av->ival < 0 || av->ival >= PALTAB_NUM(GPT) ) EXRETURN ;
00688
00689 load_PBAR_palette_array( im3d->vwid->func->inten_pbar ,
00690 PALTAB_ARR(GPT,av->ival) , 1 ) ;
00691
00692 if( im3d->vinfo->func_visible )
00693 AFNI_redisplay_func( im3d ) ;
00694
00695 EXRETURN ;
00696 }
00697
00698
00699
00700 void AFNI_finalize_read_palette_CB( Widget w, XtPointer cd, XtPointer cb )
00701 {
00702 Three_D_View * im3d = (Three_D_View *) cd ;
00703 XmFileSelectionBoxCallbackStruct * cbs = (XmFileSelectionBoxCallbackStruct *) cb ;
00704 char * dum ;
00705
00706 ENTRY("AFNI_finalize_read_palette_CB") ;
00707
00708 switch( cbs->reason ){
00709
00710
00711
00712 case XmCR_CANCEL:
00713 RWC_XtPopdown( im3d->vwid->file_dialog ) ;
00714 break ;
00715
00716
00717
00718 case XmCR_OK:{
00719 char * text = NULL ;
00720 int ii , npal1 , npal2 ;
00721 Three_D_View * qq3d ;
00722 XmStringGetLtoR( cbs->value , XmFONTLIST_DEFAULT_TAG , &text ) ;
00723 if( text != NULL ){
00724 if( THD_is_file(text) && !THD_is_directory(text) ){
00725
00726 if( im3d->vwid->func->inten_pbar->bigmode ){
00727 char *cmd = AFNI_suck_file( text ) ;
00728 ii = PBAR_define_bigmap(cmd); free(cmd);
00729 if( ii == 0 )
00730 RWC_XtPopdown( im3d->vwid->file_dialog ) ;
00731 else
00732 (void) MCW_popup_message( w ,
00733 "******************************\n"
00734 "** Can't use the colorscale **\n"
00735 "** file you selected! **\n"
00736 "******************************"
00737 , MCW_USER_KILL | MCW_TIMER_KILL ) ;
00738 } else {
00739
00740 npal1 = (GPT == NULL) ? 0 : PALTAB_NUM(GPT) ;
00741
00742 AFNI_process_setup( text , SETUP_LATER_MODE , im3d->dc ) ;
00743
00744 npal2 = (GPT == NULL) ? 0 : PALTAB_NUM(GPT) ;
00745
00746 if( npal2 > npal1 ){
00747 for( ii=0 ; ii < MAX_CONTROLLERS ; ii++ ){
00748 qq3d = GLOBAL_library.controllers[ii] ;
00749 if( IM3D_VALID(qq3d) ){
00750 refit_MCW_optmenu( qq3d->vwid->func->pbar_palette_av ,
00751 0 ,
00752 npal2-1 ,
00753 0 ,
00754 0 ,
00755 AFNI_palette_label_CB ,
00756 NULL
00757 ) ;
00758
00759
00760 XtManageChild( qq3d->vwid->func->pbar_palette_av->wrowcol ) ;
00761
00762
00763
00764 if( qq3d == im3d ){
00765 AV_assign_ival( qq3d->vwid->func->pbar_palette_av , PALTAB_NUM(GPT)-1 ) ;
00766 AFNI_palette_av_CB( qq3d->vwid->func->pbar_palette_av , im3d ) ;
00767 }
00768 }
00769 }
00770 }
00771
00772 dum = dump_PBAR_palette_table(0) ;
00773 (void) MCW_popup_message( im3d->vwid->func->options_label ,
00774 dum , MCW_USER_KILL | MCW_TIMER_KILL ) ;
00775 free(dum) ;
00776
00777 RWC_XtPopdown( im3d->vwid->file_dialog ) ;
00778 }
00779 } else {
00780 (void) MCW_popup_message( w ,
00781 "****************************\n"
00782 "** Can't open the palette **\n"
00783 "** file you selected! **\n"
00784 "****************************"
00785 , MCW_USER_KILL | MCW_TIMER_KILL ) ;
00786 BEEPIT ;
00787 }
00788 XtFree(text) ;
00789 }
00790 }
00791 break ;
00792
00793 case XmCR_HELP:
00794 (void) MCW_popup_message( w ,
00795 "To read in a palette file, use the\n"
00796 "Directories and Files selectors,\n"
00797 "and the Filter entry and button,\n"
00798 "to get the 'Selection' box correct;\n"
00799 "that is, 'Selection' should be the\n"
00800 "the name of the file you want to read.\n"
00801 "Then press 'Set'.\n\n"
00802 "N.B.: To use the new palette(s), you\n"
00803 " must use the 'Set Pal' chooser."
00804 , MCW_USER_KILL ) ;
00805 break ;
00806 }
00807 EXRETURN ;
00808 }
00809
00810
00811
00812 void AFNI_set_pbar_top_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs * cbs )
00813 {
00814 Three_D_View * im3d = (Three_D_View *) cd ;
00815 MCW_pbar * pbar ;
00816 float pval[NPANE_MAX+1] ;
00817 double pmin,pmax , fac ;
00818 int ii ;
00819
00820 ENTRY("AFNI_set_pbar_top_CB") ;
00821
00822 if( ! IM3D_OPEN(im3d) ) EXRETURN ;
00823
00824 pmax = cbs->fval ; if( pmax <= 0.0 ){ BEEPIT; EXRETURN; }
00825 pbar = im3d->vwid->func->inten_pbar ;
00826
00827 HIDE_SCALE(im3d) ;
00828 if( pbar->bigmode ){
00829 pbar->bigset = 0 ;
00830 pmin = (pbar->mode) ? 0.0 : -pmax ;
00831 PBAR_set_bigmode( pbar , 1 , pmin,pmax ) ;
00832 AFNI_inten_pbar_CB( pbar , im3d , 0 ) ;
00833 POPUP_cursorize( pbar->panew ) ;
00834 } else {
00835 fac = pmax / pbar->pval[0] ;
00836 for( ii=0 ; ii <= pbar->num_panes ; ii++ )
00837 pval[ii] = fac * pbar->pval[ii] ;
00838 alter_MCW_pbar( pbar , 0 , pval ) ;
00839 NORMAL_cursorize( pbar->panew ) ;
00840 }
00841 FIX_SCALE_SIZE(im3d) ;
00842
00843 EXRETURN ;
00844 }
00845
00846
00847
00848 void AFNI_finalize_write_palette_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs * cbs )
00849 {
00850 Three_D_View * im3d = (Three_D_View *) cd ;
00851 int ll , ii , jj ;
00852 char * fname , * ptr ;
00853 FILE * fp ;
00854 MCW_pbar * pbar ;
00855 int jm , npane , novu , ovu[NPANE_MAX] ;
00856 int * ovin ;
00857 float * pval ;
00858
00859 ENTRY("AFNI_finalize_write_palette_CB") ;
00860
00861 if( ! IM3D_OPEN(im3d) || cbs->reason != mcwCR_string ||
00862 cbs->cval == NULL || (ll=strlen(cbs->cval)) == 0 ){BEEPIT; EXRETURN;}
00863
00864 fname = (char *) malloc( sizeof(char) * (ll+8) ) ;
00865 strcpy( fname , cbs->cval ) ;
00866
00867 if( ll > 240 || ! THD_filename_ok(fname) ){free(fname); BEEPIT; EXRETURN;}
00868
00869 ptr = strstr(fname,".pal") ;
00870 if( ptr == NULL || ptr[4] != '0' ){ strcat(fname,".pal"); ll += 4; }
00871 fp = fopen( fname , "a" ) ;
00872 if( fp == NULL ){
00873 char buf[512] ;
00874 sprintf(buf,"Can't open file\n %s\nfor writing!",fname) ;
00875 (void) MCW_popup_message( im3d->vwid->func->options_label ,
00876 buf , MCW_USER_KILL | MCW_TIMER_KILL ) ;
00877 BEEPIT ; free(fname) ; EXRETURN ;
00878 } else {
00879 char buf[512] ;
00880 sprintf(buf,"\nWriting current palette to file\n %s\n",fname) ;
00881 (void) MCW_popup_message( im3d->vwid->func->options_label ,
00882 buf , MCW_USER_KILL | MCW_TIMER_KILL ) ;
00883 }
00884
00885 pbar = im3d->vwid->func->inten_pbar ;
00886 npane = pbar->num_panes ;
00887 jm = pbar->mode ;
00888 ovin = pbar->ov_index ;
00889 pval = pbar->pval ;
00890
00891
00892
00893 if( pbar->bigmode ){
00894 fprintf(fp,"%s\n",pbar->bigname) ;
00895 for( ii=0 ; ii < NPANE_BIG ; ii++ )
00896 fprintf(fp,"#%02x%02x%02x\n",
00897 (unsigned int)pbar->bigcolor[ii].r ,
00898 (unsigned int)pbar->bigcolor[ii].g ,
00899 (unsigned int)pbar->bigcolor[ii].b ) ;
00900 } else {
00901
00902
00903
00904 novu = 1 ; ovu[0] = ovin[0] ;
00905 for( ii=1 ; ii < npane ; ii++ ){
00906 for( jj=0 ; jj < novu ; jj++ )
00907 if( ovin[ii] == ovu[jj] ) break ;
00908
00909 if( jj == novu )
00910 ovu[novu++] = ovin[ii] ;
00911 }
00912
00913
00914
00915 fprintf( fp , "\n***COLORS\n" ) ;
00916 for( ii=0 ; ii < novu ; ii++ ){
00917 if( ovu[ii] > 0 )
00918 fprintf( fp , " %s = %s\n" ,
00919 im3d->dc->ovc->label_ov[ovu[ii]] ,
00920 im3d->dc->ovc->name_ov[ovu[ii]] ) ;
00921 }
00922
00923 fname[ll-4] = '\0' ;
00924 fprintf( fp , "\n***PALETTES %s [%d%s\n" ,
00925 fname , npane , (jm==0) ? "]" : "+]" ) ;
00926
00927 for( ii=0 ; ii < npane ; ii++ )
00928 fprintf( fp , " %f -> %s\n" ,
00929 pval[ii] , im3d->dc->ovc->label_ov[ovin[ii]] ) ;
00930 }
00931
00932 POPDOWN_string_chooser; fclose(fp); free(fname); EXRETURN;
00933 }
00934
00935
00936
00937 void AFNI_finalize_saveim_CB( Widget wcaller, XtPointer cd, MCW_choose_cbs * cbs )
00938 {
00939 Three_D_View * im3d = (Three_D_View *) cd ;
00940 char * fname , * ptr ;
00941 int ll , nx=20 , ny=256 ;
00942 MRI_IMAGE * im ;
00943
00944 ENTRY("AFNI_finalize_saveim_CB") ;
00945
00946 if( ! IM3D_OPEN(im3d) || cbs->reason != mcwCR_string ||
00947 cbs->cval == NULL || (ll=strlen(cbs->cval)) == 0 ){BEEPIT; EXRETURN;}
00948
00949 fname = (char *) malloc( sizeof(char) * (ll+8) ) ;
00950 strcpy( fname , cbs->cval ) ;
00951
00952 if( ll > 240 || ! THD_filename_ok(fname) ){free(fname); BEEPIT; EXRETURN;}
00953
00954 ptr = strstr(fname,".ppm") ;
00955 if( ptr == NULL ) ptr = strstr(fname,".pnm") ;
00956 if( ptr == NULL ) ptr = strstr(fname,".jpg") ;
00957 if( ptr == NULL ) strcat(fname,".ppm") ;
00958
00959 fprintf(stderr,"Writing palette image to %s\n",fname) ;
00960
00961 ptr = getenv( "AFNI_PBAR_IMXY" );
00962 if( ptr != NULL ){
00963 ll = sscanf( ptr , "%dx%d" , &nx , &ny ) ;
00964 if( ll < 2 || nx < 1 || ny < 32 ){ nx=20; ny=256; }
00965 }
00966
00967 im = MCW_pbar_to_mri( im3d->vwid->func->inten_pbar , nx,ny ) ;
00968 mri_write_pnm( fname , im ) ;
00969
00970 POPDOWN_string_chooser; mri_free(im); free(fname); EXRETURN;
00971 }
00972
00973
00974
00975 void AFNI_palette_tran_CB( MCW_arrowval * av , XtPointer cd )
00976 {
00977 Three_D_View * im3d = (Three_D_View *) cd ;
00978
00979 ENTRY("AFNI_palette_tran_CB") ;
00980
00981 if( !IM3D_VALID(im3d) ) EXRETURN ;
00982
00983 if( av == im3d->vwid->func->pbar_transform0D_av ){
00984
00985 im3d->vwid->func->pbar_transform0D_index = av->ival ;
00986 if( av->ival == 0 )
00987 im3d->vwid->func->pbar_transform0D_func = NULL ;
00988 else {
00989 im3d->vwid->func->pbar_transform0D_func =
00990 GLOBAL_library.registered_0D.funcs[av->ival-1];
00991 if( GLOBAL_library.registered_0D.func_init[av->ival-1] != NULL )
00992 GLOBAL_library.registered_0D.func_init[av->ival-1]() ;
00993 }
00994
00995 } else if( av == im3d->vwid->func->pbar_transform2D_av ){
00996
00997 im3d->vwid->func->pbar_transform2D_index = av->ival ;
00998 if( av->ival == 0 )
00999 im3d->vwid->func->pbar_transform2D_func = NULL ;
01000 else{
01001 im3d->vwid->func->pbar_transform2D_func =
01002 GLOBAL_library.registered_2D.funcs[av->ival-1];
01003 if( GLOBAL_library.registered_2D.func_init[av->ival-1] != NULL )
01004 GLOBAL_library.registered_2D.func_init[av->ival-1]() ;
01005 }
01006 }
01007
01008 if( im3d->vinfo->func_visible )
01009 AFNI_redisplay_func( im3d ) ;
01010
01011 EXRETURN ;
01012 }