00001 #include <Xm/XmAll.h>
00002
00003 #include "mrilib.h"
00004 #include "imseq.h"
00005
00006 static XtAppContext MAIN_app ;
00007 static MCW_DC * MAIN_dc ;
00008 static Widget MAIN_shell=NULL ;
00009 static Widget MAIN_rc ;
00010 static Widget MAGN_scale , PHASE_scale ;
00011
00012 #define FIX_SCALE_SIZE_PROBLEM
00013
00014 static int P_swide = 512 ;
00015 #ifdef FIX_SCALE_SIZE_PROBLEM
00016 # define FIX_SCALE_SIZE \
00017 do{ XtVaSetValues(MAGN_scale ,XmNwidth,P_swide,NULL) ; \
00018 XtVaSetValues(PHASE_scale,XmNwidth,P_swide,NULL) ; } while(0)
00019 #else
00020 # define FIX_SCALE_SIZE
00021 #endif
00022
00023
00024
00025 MRI_IMAGE * mri_fft2D( MRI_IMAGE * im , int mode )
00026 {
00027 MRI_IMAGE * cxim , * outim ;
00028 int nx,ny , nxup,nyup , ii,jj ;
00029 complex * cxar , * outar , * cpt , * opt ;
00030 float fac ;
00031
00032 if( im == NULL ) return NULL ;
00033
00034
00035
00036 cxim = mri_to_complex(im) ;
00037 cxar = MRI_COMPLEX_PTR(cxim) ;
00038
00039
00040
00041 nx = cxim->nx ; nxup = csfft_nextup_one35(nx) ;
00042 ny = cxim->ny ; nyup = csfft_nextup_one35(ny) ;
00043
00044
00045
00046 outim = mri_new( nxup , nyup , MRI_complex ) ;
00047 outar = MRI_COMPLEX_PTR(outim) ;
00048
00049
00050
00051 opt = outar ;
00052 cpt = cxar ;
00053 for( jj=0 ; jj < ny ; jj++ ){
00054 for( ii=0 ; ii < nx ; ii++ ){opt->r=cpt->r; opt->i=cpt->i; opt++; cpt++;}
00055 for( ; ii < nxup ; ii++ ){opt->r=opt->i=0.0; opt++;}
00056 }
00057 for( ; jj < nyup ; jj++ ){opt->r=opt->i=0.0; opt++;}
00058
00059 mri_free(cxim) ;
00060
00061
00062
00063 for( jj=0 ; jj < ny ; jj++ )
00064 csfft_cox( mode , nxup , outar+jj*nxup ) ;
00065
00066
00067
00068 cxar = (complex *) malloc(sizeof(complex)*nyup) ;
00069
00070 for( ii=0 ; ii < nxup ; ii++ ){
00071 for( jj=0 ; jj < nyup ; jj++ ) cxar[jj] = outar[ii+jj*nxup] ;
00072 csfft_cox( mode , nyup , cxar ) ;
00073 for( jj=0 ; jj < nyup ; jj++ ) outar[ii+jj*nxup] = cxar[jj] ;
00074 }
00075
00076 fac = sqrt(1.0/(nxup*nyup)) ;
00077 for( ii=0 ; ii < nxup*nyup ; ii++ ){
00078 outar[ii].r *= fac ; outar[ii].i *= fac ;
00079 }
00080
00081 free(cxar) ; return outim ;
00082 }
00083
00084
00085
00086 MRI_IMAGE * cx_scramble( MRI_IMAGE * ima , MRI_IMAGE * imb ,
00087 float alpha , float beta )
00088 {
00089 int ii , npix ;
00090 double r1,r2 , t1,t2 , rr,tt ;
00091 complex * ar , * br , * cr ;
00092 double aa,aa1 , bb,bb1 ;
00093 MRI_IMAGE * imc ;
00094
00095 if( ima == NULL || ima->kind != MRI_complex ||
00096 imb == NULL || imb->kind != MRI_complex ||
00097 ima->nx != imb->nx || ima->ny != imb->ny ||
00098 alpha < 0.0 || alpha > 1.0 ||
00099 beta < 0.0 || beta > 1.0 ) return NULL ;
00100
00101 npix = ima->nvox ;
00102 ar = MRI_COMPLEX_PTR(ima) ;
00103 br = MRI_COMPLEX_PTR(imb) ;
00104 imc = mri_new_conforming( ima , MRI_complex ) ;
00105 cr = MRI_COMPLEX_PTR(imc) ;
00106
00107 aa = alpha ; aa1 = 1.0 - aa ;
00108 bb = beta ; bb1 = 1.0 - bb ;
00109
00110 for( ii=0 ; ii < npix ; ii++ ){
00111 r1 = CABS(ar[ii]) ; r2 = CABS(br[ii]) ; rr = pow(r1,aa)*pow(r2,aa1) ;
00112 t1 = CARG(ar[ii]) ; t2 = CARG(br[ii]) ; tt = t1-t2 ;
00113 if( tt < -PI ) t2 -= 2.0*PI ;
00114 else if( tt > PI ) t2 += 2.0*PI ;
00115 tt = bb*t1 + bb1*t2 ;
00116 cr[ii].r = rr * cos(tt) ; cr[ii].i = rr * sin(tt) ;
00117 }
00118
00119 return imc ;
00120 }
00121
00122
00123
00124 MRI_IMAGE * mri_scramble( MRI_IMAGE * ima , MRI_IMAGE * imb ,
00125 float alpha , float beta )
00126 {
00127 MRI_IMAGE * cxa, * cxb, * cxc ;
00128 int nx,ny , nxup,nyup ;
00129
00130 if( ima == NULL || imb == NULL ||
00131 ima->nx != imb->nx || ima->ny != imb->ny ||
00132 alpha < 0.0 || alpha > 1.0 ||
00133 beta < 0.0 || beta > 1.0 ) return NULL ;
00134
00135 cxa = mri_fft2D( ima , -1 ) ;
00136 cxb = mri_fft2D( imb , -1 ) ;
00137 cxc = cx_scramble( cxa,cxb,alpha,beta ) ;
00138 mri_free(cxa) ; mri_free(cxb) ;
00139 cxa = mri_fft2D( cxc , 1 ) ;
00140 mri_free(cxc) ;
00141 cxb = mri_to_mri( ima->kind , cxa ) ;
00142 mri_free(cxa) ;
00143
00144 if( cxb->nx > ima->nx || cxb->ny > ima->ny ){
00145 cxc = mri_cut_2D( cxb , 0,ima->nx-1,0,ima->ny-1 ) ;
00146 mri_free(cxb) ; cxb = cxc ;
00147 }
00148
00149 return cxb ;
00150 }
00151
00152
00153 typedef struct {
00154 MCW_imseq * seq ;
00155 MRI_IMAGE * im ;
00156 } PLUGIN_impopper ;
00157
00158 static void * P_handle = NULL ;
00159
00160 #define PLUTO_popup_open(hh) \
00161 ( (hh) != NULL && ISQ_REALZ(((PLUGIN_impopper *)(hh))->seq) )
00162
00163
00164
00165
00166
00167
00168
00169 void PLUGIN_seq_send_CB( MCW_imseq * seq , XtPointer handle , ISQ_cbs * cbs )
00170 {
00171 PLUGIN_impopper * imp = (PLUGIN_impopper *) handle ;
00172
00173 if( imp == NULL ) return ;
00174
00175 switch( cbs->reason ){
00176
00177 case isqCR_destroy:{
00178 XtFree((char*)imp->seq->status) ;
00179 XtFree((char*)imp->seq) ; imp->seq = NULL ;
00180 mri_free( imp->im ) ; imp->im = NULL ;
00181 }
00182 break ;
00183 }
00184 return ;
00185 }
00186
00187
00188
00189
00190
00191
00192 XtPointer PLUGIN_imseq_getim( int n , int type , XtPointer handle )
00193 {
00194 PLUGIN_impopper * imp = (PLUGIN_impopper *) handle ;
00195
00196 if( imp == NULL ) return(NULL) ;
00197
00198
00199
00200 if( type == isqCR_getstatus ){
00201 MCW_imseq_status * stat = XtNew( MCW_imseq_status ) ;
00202 stat->num_total = 1 ;
00203 stat->num_series = 1 ;
00204 stat->send_CB = PLUGIN_seq_send_CB ;
00205 stat->parent = (XtPointer) imp ;
00206 stat->aux = NULL ;
00207
00208 stat->transforms0D = NULL ;
00209 stat->transforms2D = NULL ;
00210 stat->slice_proj = NULL ;
00211
00212 return((XtPointer) stat) ;
00213 }
00214
00215
00216
00217 if( type == isqCR_getoverlay ) return(NULL) ;
00218
00219
00220
00221
00222 if( type == isqCR_getimage || type == isqCR_getqimage ){
00223 MRI_IMAGE * im = NULL ;
00224 if( imp->im != NULL ) im = mri_to_mri( imp->im->kind , imp->im ) ;
00225 return((XtPointer) im) ;
00226 }
00227
00228 return(NULL) ;
00229 }
00230
00231
00232
00233
00234
00235 static void * PH_popup_image( void * handle , MRI_IMAGE * im )
00236 {
00237 PLUGIN_impopper * imp = (PLUGIN_impopper *) handle ;
00238
00239
00240
00241 if( im == NULL ){
00242 if( imp != NULL )
00243 drive_MCW_imseq( imp->seq , isqDR_destroy , NULL ) ;
00244
00245 return ((void *) imp) ;
00246 }
00247
00248
00249
00250 if( imp == NULL ){
00251 imp = XtNew(PLUGIN_impopper) ;
00252 imp->seq = NULL ; imp->im = NULL ;
00253 }
00254
00255
00256
00257 mri_free( imp->im ) ;
00258 imp->im = mri_to_mri( im->kind , im ) ;
00259
00260
00261
00262 if( imp->seq == NULL ){
00263 imp->seq = open_MCW_imseq( MAIN_dc ,
00264 PLUGIN_imseq_getim , (XtPointer) imp ) ;
00265
00266 drive_MCW_imseq( imp->seq , isqDR_realize, NULL ) ;
00267 drive_MCW_imseq( imp->seq , isqDR_onoffwid , (XtPointer) isqDR_offwid ) ;
00268
00269 XtVaSetValues( imp->seq->wtop ,
00270 XmNmwmDecorations , MWM_DECOR_BORDER | MWM_DECOR_TITLE | MWM_DECOR_MENU ,
00271 XmNmwmFunctions , MWM_FUNC_MOVE | MWM_FUNC_CLOSE ,
00272 XmNtitle , "Xphace Images" ,
00273 NULL ) ;
00274
00275 }
00276
00277 drive_MCW_imseq( imp->seq , isqDR_clearstat , NULL ) ;
00278 drive_MCW_imseq( imp->seq , isqDR_reimage , (XtPointer) 0 ) ;
00279
00280 return ((void *) imp) ;
00281 }
00282
00283
00284
00285 static char * FALLback[] =
00286 { "AFNI*fontList: 9x15bold=charset1" ,
00287 "AFNI*background: gray40" ,
00288 "AFNI*menu*background: gray40" ,
00289 "AFNI*borderColor: gray40" ,
00290 "AFNI*foreground: yellow" ,
00291 "AFNI*borderWidth: 0" ,
00292 "AFNI*troughColor: green" ,
00293 "AFNI*XmLabel.translations: #override<Btn2Down>:" ,
00294 NULL } ;
00295
00296 #ifndef LABEL_ARG
00297 #define LABEL_ARG(str) \
00298 XtVaTypedArg , XmNlabelString , XmRString , (str) , strlen(str)+1
00299 #endif
00300
00301 void PH_scale_CB(Widget,XtPointer,XtPointer) ;
00302 void PH_redraw(void) ;
00303 int PH_loadim(char *) ;
00304 void PH_startup_timeout_CB( XtPointer client_data , XtIntervalId * id ) ;
00305
00306
00307
00308 int main( int argc , char * argv[] )
00309 {
00310 Widget rc , lab ;
00311 int ww , uu ;
00312
00313 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
00314 printf("Usage: Xphace im1 [im2]\n"
00315 "Image mergerizing.\n"
00316 "Image files are in PGM format.\n") ; exit(0) ;
00317 }
00318
00319 mainENTRY("Xphace main") ; machdep() ;
00320
00321 ww = PH_loadim( argv[1] ) ;
00322 if( ww < 0 ) exit(1) ;
00323 if( argc > 2 ){
00324 ww = PH_loadim( argv[2] ) ;
00325 if( ww < 0 ) exit(1) ;
00326 } else {
00327 PH_loadim( "noise=1" ) ;
00328 }
00329
00330 MAIN_shell = XtVaAppInitialize( &MAIN_app , "AFNI" , NULL , 0 ,
00331 &argc , argv , FALLback , NULL ) ;
00332
00333 if( MAIN_shell == NULL ){
00334 fprintf(stderr,"\n*** Cannot initialize X11 ***\n") ; exit(1) ;
00335 }
00336
00337 MAIN_dc = MCW_new_DC( MAIN_shell , 32 , 0 , NULL,NULL , 1.0 , 0 ) ;
00338
00339 XtVaSetValues( XmGetXmDisplay(XtDisplay(MAIN_shell)) ,
00340 XmNdragInitiatorProtocolStyle , XmDRAG_NONE ,
00341 XmNdragReceiverProtocolStyle , XmDRAG_NONE ,
00342 NULL ) ;
00343
00344 MAIN_rc = XtVaCreateWidget( "AFNI" , xmRowColumnWidgetClass , MAIN_shell ,
00345 XmNpacking , XmPACK_TIGHT ,
00346 XmNorientation , XmVERTICAL ,
00347 XmNtraversalOn , False ,
00348 NULL ) ;
00349
00350
00351
00352 rc = XtVaCreateWidget( "AFNI" , xmRowColumnWidgetClass , MAIN_rc ,
00353 XmNpacking , XmPACK_TIGHT ,
00354 XmNorientation , XmHORIZONTAL ,
00355 XmNtraversalOn , False ,
00356 NULL ) ;
00357
00358 lab = XtVaCreateManagedWidget( "AFNI" , xmLabelWidgetClass , rc ,
00359 LABEL_ARG( "Magn. " ) ,
00360 XmNmarginHeight, 0 ,
00361 XmNmarginWidth , 0 ,
00362 NULL ) ;
00363
00364 MAGN_scale = XtVaCreateManagedWidget( "AFNI" , xmScaleWidgetClass , rc ,
00365 XmNminimum , 0 ,
00366 XmNmaximum , 100 ,
00367 XmNvalue , 0 ,
00368 XmNwidth , P_swide ,
00369 XmNshowValue , True ,
00370 XmNscaleMultiple , 10 ,
00371 XmNorientation , XmHORIZONTAL ,
00372 XmNtraversalOn , False ,
00373 NULL ) ;
00374
00375 XtAddCallback( MAGN_scale , XmNvalueChangedCallback , PH_scale_CB , NULL ) ;
00376 XtManageChild( rc ) ;
00377
00378
00379
00380 rc = XtVaCreateWidget( "AFNI" , xmRowColumnWidgetClass , MAIN_rc ,
00381 XmNpacking , XmPACK_TIGHT ,
00382 XmNorientation , XmHORIZONTAL ,
00383 XmNtraversalOn , False ,
00384 NULL ) ;
00385
00386 lab = XtVaCreateManagedWidget( "AFNI" , xmLabelWidgetClass , rc ,
00387 LABEL_ARG( "Phase " ) ,
00388 XmNmarginHeight, 0 ,
00389 XmNmarginWidth , 0 ,
00390 NULL ) ;
00391
00392 PHASE_scale = XtVaCreateManagedWidget( "AFNI" , xmScaleWidgetClass , rc ,
00393 XmNminimum , 0 ,
00394 XmNmaximum , 100 ,
00395 XmNvalue , 0 ,
00396 XmNwidth , P_swide ,
00397 XmNshowValue , True ,
00398 XmNscaleMultiple , 10 ,
00399 XmNorientation , XmHORIZONTAL ,
00400 XmNtraversalOn , False ,
00401 NULL ) ;
00402
00403 XtAddCallback( PHASE_scale , XmNvalueChangedCallback , PH_scale_CB , NULL ) ;
00404 XtManageChild( rc ) ;
00405
00406
00407
00408 XtManageChild( MAIN_rc ) ;
00409 XtRealizeWidget( MAIN_shell ) ;
00410
00411 #if 0
00412 XtVaSetValues( MAIN_rc , XmNwidth , P_swide , NULL ) ;
00413 XtVaSetValues( MAGN_scale , XmNwidth , P_swide , NULL ) ;
00414 XtVaSetValues( PHASE_scale , XmNwidth , P_swide , NULL ) ;
00415 #endif
00416
00417 XtVaSetValues( MAIN_shell ,
00418 XmNmwmDecorations , MWM_DECOR_BORDER | MWM_DECOR_TITLE | MWM_DECOR_MENU ,
00419 XmNmwmFunctions , MWM_FUNC_MOVE | MWM_FUNC_CLOSE ,
00420 XmNtitle , "Xphace Controls" ,
00421 NULL ) ;
00422
00423 (void) XtAppAddTimeOut( MAIN_app , 1234 , PH_startup_timeout_CB , NULL ) ;
00424 XtAppMainLoop( MAIN_app ) ;
00425 exit(0) ;
00426 }
00427
00428
00429
00430 void PH_startup_timeout_CB( XtPointer client_data , XtIntervalId * id )
00431 {
00432 PH_redraw() ; return ;
00433 }
00434
00435
00436
00437 static MRI_IMAGE * P_ima=NULL , * P_imb=NULL ;
00438 static float P_alpha=0.0 , P_beta=0.0 ;
00439
00440 #define CONST 1
00441 #define NOISE 2
00442
00443 static int P_ima_null=CONST , P_imb_null=CONST ;
00444 static float P_ima_val =0.0 , P_imb_val =0.0 ;
00445
00446 static int P_nx=256 , P_ny=256 ;
00447
00448 #define Mfree(im) do{mri_free(im);im=NULL;}while(0)
00449
00450 MRI_IMAGE * PH_fakeim( int , int , int , float ) ;
00451
00452 static int doA = 1 ;
00453
00454 int PH_loadim( char * str )
00455 {
00456 MRI_IMAGE * im=NULL ;
00457 int nxa,nxb , nya,nyb ;
00458
00459 if( str[0] == '\0' || strncmp(str,"black",5) == 0 ){
00460 if( doA ){ P_ima_null = CONST ; P_ima_val = 0.0 ; Mfree(P_ima) ; }
00461 else { P_imb_null = CONST ; P_imb_val = 0.0 ; Mfree(P_imb) ; }
00462 } else if( strncmp(str,"const=",6) == 0 ){
00463 float val=0.0 ;
00464 sscanf(str+6,"%f",&val) ;
00465 if( doA ){ P_ima_null = CONST ; P_ima_val = val ; Mfree(P_ima) ; }
00466 else { P_imb_null = CONST ; P_imb_val = val ; Mfree(P_imb) ; }
00467 } else if( strncmp(str,"noise=",6) == 0 ){
00468 float val=0.0 ;
00469 sscanf(str+6,"%f",&val) ;
00470 if( doA ){ P_ima_null = NOISE ; P_ima_val = val ; Mfree(P_ima) ; }
00471 else { P_imb_null = NOISE ; P_imb_val = val ; Mfree(P_imb) ; }
00472 } else {
00473 float top ; MRI_IMAGE * qim ;
00474 im = mri_read( str ) ;
00475 if( im == NULL ) return -1;
00476 top = mri_maxabs(im) ;
00477 if( top > 0.0 ) top = 1.0 / top ;
00478 qim = mri_scale_to_float( top , im ) ;
00479 Mfree(im) ; im = qim ;
00480 if( doA ){ Mfree(P_ima) ; P_ima = im ; }
00481 else { Mfree(P_imb) ; P_imb = im ; }
00482 }
00483
00484 nxa = (P_ima != NULL) ? P_ima->nx : 0 ;
00485 nxb = (P_imb != NULL) ? P_imb->nx : 0 ;
00486 nya = (P_ima != NULL) ? P_ima->ny : 0 ;
00487 nyb = (P_imb != NULL) ? P_imb->ny : 0 ;
00488
00489 P_nx = MAX(nxa,nxb) ; if( P_nx == 0 ) P_nx = 256 ;
00490 P_ny = MAX(nya,nyb) ; if( P_ny == 0 ) P_ny = 256 ;
00491
00492 if( nxa > 0 && (nxa != P_nx || nya != P_ny) ){
00493 im = mri_resize(P_ima,P_nx,P_ny) ; Mfree(P_ima) ; P_ima = im ;
00494 }
00495
00496 if( nxb > 0 && (nxb != P_nx || nyb != P_ny) ){
00497 im = mri_resize(P_imb,P_nx,P_ny) ; Mfree(P_imb) ; P_imb = im ;
00498 }
00499
00500 if( P_ima == NULL ) P_ima = PH_fakeim( P_nx,P_ny , P_ima_null,P_ima_val ) ;
00501 if( P_imb == NULL ) P_imb = PH_fakeim( P_nx,P_ny , P_imb_null,P_imb_val ) ;
00502
00503 doA = 0 ; return 0 ;
00504 }
00505
00506 MRI_IMAGE * PH_fakeim( int nx , int ny , int code , float val )
00507 {
00508 MRI_IMAGE * im ;
00509 float * far ;
00510 int ii , nvox ;
00511
00512 im = mri_new( nx , ny , MRI_float ) ;
00513 far = MRI_FLOAT_PTR(im) ;
00514 nvox = im->nvox ;
00515
00516 #if 0
00517 fprintf(stderr,"PH_fakeim: code=%d val=%f\n",code,val) ;
00518 #endif
00519
00520 switch( code ){
00521
00522 default:
00523 case CONST:
00524 for( ii=0 ; ii < nvox ; ii++ ) far[ii] = val ;
00525 break ;
00526
00527 case NOISE:
00528 for( ii=0 ; ii < nvox ; ii++ ) far[ii] = val * drand48() ;
00529 break ;
00530 }
00531
00532 return im ;
00533 }
00534
00535 void PH_scale_CB( Widget w , XtPointer cd , XtPointer cb )
00536 {
00537 XmScaleCallbackStruct * cbs = (XmScaleCallbackStruct *) cb ;
00538 float val = 0.01 * cbs->value ;
00539
00540 FIX_SCALE_SIZE ;
00541
00542 if( w == MAGN_scale ) P_alpha = val ;
00543 else if( w == PHASE_scale ) P_beta = val ;
00544
00545 PH_redraw() ;
00546
00547 { char *eee = getenv("XPHACE_SNAP") ;
00548 if( eee != NULL && toupper(*eee) == 'Y' ){
00549 PLUGIN_impopper *imp = (PLUGIN_impopper *) P_handle ;
00550 #if 0
00551 fprintf(stderr,"XID = %x\n",(unsigned int)XtWindow(imp->seq->wimage)) ;
00552 #endif
00553 ISQ_snapshot( imp->seq->wimage ) ;
00554 }}
00555 }
00556
00557 void PH_redraw(void)
00558 {
00559 MRI_IMAGE * imc , * im3 ;
00560 MRI_IMARR * imar ;
00561 float fgap = 0.0 ;
00562
00563 if( P_ima == NULL || P_imb == NULL ) return ;
00564
00565 #if 0
00566 fprintf(stderr,"PH_redraw: A:nx=%d ny=%d B:nx=%d ny=%d\n",
00567 P_ima->nx,P_ima->ny , P_imb->nx,P_imb->ny ) ;
00568 fprintf(stderr,"PH_redraw: calling mri_scramble\n") ;
00569 #endif
00570
00571 imc = mri_scramble( P_ima,P_imb , 1.0-P_alpha,1.0-P_beta ) ;
00572
00573 #if 0
00574 if(imc==NULL)fprintf(stderr," - returned NULL!\n") ;
00575 #endif
00576
00577 INIT_IMARR(imar) ;
00578 ADDTO_IMARR(imar,P_ima); ADDTO_IMARR(imar,imc); ADDTO_IMARR(imar,P_imb);
00579
00580 #if 0
00581 fprintf(stderr,"PH_redraw: calling mri_cat2D\n") ;
00582 if(imc!=NULL)fprintf(stderr," C:nx=%d ny=%d\n", imc->nx,imc->ny ) ;
00583 #endif
00584
00585 im3 = mri_cat2D( 3,1 , 3 , &fgap , imar ) ;
00586 FREE_IMARR(imar) ; mri_free(imc) ;
00587
00588 #if 0
00589 if(im3==NULL)fprintf(stderr," - returned NULL!\n") ;
00590 fprintf(stderr,"PH_redraw: calling PH_popup_image\n") ;
00591 #endif
00592
00593 P_handle = PH_popup_image( P_handle , im3 ) ;
00594
00595 mri_free(im3) ; return ;
00596 }