Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

plug_ttget.c

Go to the documentation of this file.
00001 #include "afni.h"
00002 
00003 #ifndef ALLOW_PLUGINS
00004 #  error "Plugins not properly set up -- see machdep.h"
00005 #endif
00006 
00007 /*------------------------------------------------------------------*/
00008 
00009 #define CORONAL_NUM 38
00010 static float coronal_yy[] = {
00011   -100, -95, -90, -85, -80, -75, -70, -65,
00012    -60, -55, -50, -45, -40, -35, -32, -28,
00013    -24, -20, -16, -12,  -8,  -4,   0,   4,
00014      8,  12,  16,  20,  24,  28,  32,  35,
00015     40,  45,  50,  55,  60,  65
00016 } ;
00017 static char * coronal_ff[] = {
00018  "tt_corm99.ppm.gz", "tt_corm95.ppm.gz", "tt_corm90.ppm.gz", "tt_corm85.ppm.gz",
00019  "tt_corm80.ppm.gz", "tt_corm75.ppm.gz", "tt_corm70.ppm.gz", "tt_corm65.ppm.gz",
00020  "tt_corm60.ppm.gz", "tt_corm55.ppm.gz", "tt_corm50.ppm.gz", "tt_corm45.ppm.gz",
00021  "tt_corm40.ppm.gz", "tt_corm35.ppm.gz", "tt_corm32.ppm.gz", "tt_corm28.ppm.gz",
00022  "tt_corm24.ppm.gz", "tt_corm20.ppm.gz", "tt_corm16.ppm.gz", "tt_corm12.ppm.gz",
00023  "tt_corm08.ppm.gz", "tt_corm04.ppm.gz", "tt_corp00.ppm.gz", "tt_corp04.ppm.gz",
00024  "tt_corp08.ppm.gz", "tt_corp12.ppm.gz", "tt_corp16.ppm.gz", "tt_corp20.ppm.gz",
00025  "tt_corp24.ppm.gz", "tt_corp28.ppm.gz", "tt_corp32.ppm.gz", "tt_corp35.ppm.gz",
00026  "tt_corp40.ppm.gz", "tt_corp45.ppm.gz", "tt_corp50.ppm.gz", "tt_corp55.ppm.gz",
00027  "tt_corp60.ppm.gz", "tt_corp65.ppm.gz"
00028 } ;
00029 
00030 /*------------------------------------------------------------------------------*/
00031 
00032 #define SAGITTAL_NUM 18
00033 static float sagittal_xx[] = {
00034     0,  3,  5,  9, 13, 17, 21, 25, 29, 33,
00035    37, 41, 43, 47, 51, 55, 59, 61
00036 } ;
00037 static char * sagittal_ff[] = {
00038  "tt_sag00g.ppm.gz", "tt_sag03.ppm.gz", "tt_sag05.ppm.gz", "tt_sag09.ppm.gz",
00039  "tt_sag13.ppm.gz", "tt_sag17.ppm.gz", "tt_sag21.ppm.gz", "tt_sag25.ppm.gz",
00040  "tt_sag29.ppm.gz", "tt_sag33.ppm.gz", "tt_sag37.ppm.gz", "tt_sag41.ppm.gz",
00041  "tt_sag43.ppm.gz", "tt_sag47.ppm.gz", "tt_sag51.ppm.gz", "tt_sag55.ppm.gz",
00042  "tt_sag59.ppm.gz", "tt_sag61.ppm.gz"
00043 } ;
00044 
00045 /*------------------------------------------------------------------------------*/
00046 
00047 #define AXIAL_NUM 27
00048 static float axial_zz[] = {
00049    -40, -36, -32, -28, -24, -20, -16, -12, -8, -4,
00050     -1,   1,   4,   8,  12,  16,  20,  24, 28, 32, 35,
00051     40,  45,  50,  55,  60,  65
00052 } ;
00053 static char * axial_ff[] = {
00054  "tt_horm40.ppm.gz", "tt_horm36.ppm.gz", "tt_horm32.ppm.gz", "tt_horm28.ppm.gz",
00055  "tt_horm24.ppm.gz", "tt_horm20.ppm.gz", "tt_horm16.ppm.gz", "tt_horm12.ppm.gz",
00056  "tt_horm08.ppm.gz", "tt_horm04.ppm.gz", "tt_horm01.ppm.gz", "tt_horp01.ppm.gz",
00057  "tt_horp04.ppm.gz", "tt_horp08.ppm.gz", "tt_horp12.ppm.gz", "tt_horp16.ppm.gz",
00058  "tt_horp20.ppm.gz", "tt_horp24.ppm.gz", "tt_horp28.ppm.gz", "tt_horp32.ppm.gz",
00059  "tt_horp35.ppm.gz", "tt_horp40.ppm.gz", "tt_horp45.ppm.gz", "tt_horp50.ppm.gz",
00060  "tt_horp55.ppm.gz", "tt_horp60.ppm.gz", "tt_horp65.ppm.gz"
00061 } ;
00062 
00063 #define TTAHOME "http://128.231.212.175/TTA/"
00064 
00065 /*------------------------------------------------------------------------------*/
00066 /*! (xx,yy,zz) in RAI (Dicom) coords; code is (0=axial,1=sag,2=cor).            */
00067 /*------------------------------------------------------------------------------*/
00068 
00069 static char * TTget_URL( int code, float xx, float yy, float zz )
00070 {
00071    static char nbuf[256] ;
00072    char *ttahome ;
00073    int ii,jj,kk ;
00074 
00075    ttahome = getenv("AFNI_TTAHOME"); if( ttahome == NULL ) ttahome = TTAHOME;
00076 
00077    switch( code ){
00078      case 1:                       /* sagittal */
00079        xx = fabs(xx) ;
00080        if( xx <= sagittal_xx[0] ){
00081          ii = 0 ;
00082        } else if( xx >= sagittal_xx[SAGITTAL_NUM-1] ){
00083          ii = SAGITTAL_NUM - 1 ;
00084        } else {
00085          for( ii=1 ; ii < SAGITTAL_NUM && xx > sagittal_xx[ii] ; ii++ ) ; /* nada */
00086          if( fabs(xx-sagittal_xx[ii-1]) < fabs(xx-sagittal_xx[ii]) ) ii-- ;
00087        }
00088        strcpy(nbuf,ttahome) ; strcat(nbuf,sagittal_ff[ii]) ;
00089      break ;
00090 
00091      case 2:                       /* coronal */
00092        yy = -yy ;
00093        if( yy <= coronal_yy[0] ){
00094           jj = 0 ;
00095        } else if( yy >= coronal_yy[CORONAL_NUM-1] ){
00096           jj = CORONAL_NUM - 1 ;
00097        } else {
00098           for( jj=1 ; jj < CORONAL_NUM && yy > coronal_yy[jj] ; jj++ ) ; /* nada */
00099           if( fabs(yy-coronal_yy[jj-1]) < fabs(yy-coronal_yy[jj]) ) jj-- ;
00100        }
00101        strcpy(nbuf,ttahome) ; strcat(nbuf,coronal_ff[jj]) ;
00102      break ;
00103 
00104      default:
00105      case 0:                       /* axial */
00106        if( zz <= axial_zz[0] ){
00107           kk = 0 ;
00108        } else if( zz >= axial_zz[AXIAL_NUM-1] ){
00109           kk = AXIAL_NUM - 1 ;
00110        } else {
00111           for( kk=1 ; kk < AXIAL_NUM && zz > axial_zz[kk] ; kk++ ) ; /* nada */
00112           if( fabs(zz-axial_zz[kk-1]) < fabs(zz-axial_zz[kk]) ) kk-- ;
00113        }
00114        strcpy(nbuf,ttahome) ; strcat(nbuf,axial_ff[kk]) ;
00115      break ;
00116    }
00117 
00118    return nbuf ;
00119 }
00120 
00121 /*------------------------------------------------------------------------------*/
00122 
00123 static MRI_IMAGE * TTget_ppm( char *url )
00124 {
00125    int ii,nn, ch,nch, nx,ny,maxval, id ;
00126    char buf[32] ;
00127    byte *ndata=NULL, *bar ;
00128    MRI_IMAGE *im ;
00129 
00130    nn = NI_read_URL( url, (char **)&ndata ) ;
00131 
00132    if( nn < 40960 || ndata == NULL || ndata[0] != 'P' || ndata[1] != '6' ){
00133       if( ndata != NULL ) free(ndata) ;
00134       return NULL ;
00135    }
00136 
00137    id = 2 ; ch = ndata[id] ;  /* start scan after "P6" */
00138 
00139 #define SKIPCOM                                                                      \
00140     {if(ch == '#') do{ch=ndata[++id];}while(id<nn-1 && ch != '\n');}
00141 
00142 #define NUMSCAN(var)                                                                 \
00143    { SKIPCOM ;                                                                       \
00144      while( id<nn-1 && !isdigit(ch) ){ch = ndata[++id]; SKIPCOM; }                   \
00145      for( nch=0 ; id<nn-1 && isdigit(ch) ; nch++,ch=ndata[++id] ) {buf[nch] = ch ;}  \
00146      buf[nch]='\0';                                                                  \
00147      var = strtol( buf, NULL, 10 ) ; }
00148 
00149     NUMSCAN(nx) ; if( nx <= 2 || id >= nn-1 ){ free(ndata); return NULL; }
00150     NUMSCAN(ny) ; if( ny <= 2 || id >= nn-1 ){ free(ndata); return NULL; }
00151     NUMSCAN(maxval);
00152     if( maxval <= 7 || maxval > 255 || id >= nn-1 ){ free(ndata); return NULL; }
00153 
00154     id++ ;
00155     if( nn-id < 3*nx*ny ){ free(ndata); return NULL; }
00156 
00157     im = mri_new( nx, ny, MRI_rgb ) ;
00158     bar = MRI_RGB_PTR(im) ;
00159     memcpy( bar, ndata+id, 3*nx*ny ) ;
00160     free(ndata) ;
00161 
00162     if( maxval < 255 ){
00163       float fac = 255.4/maxval ;
00164       for( ii=0 ; ii < 3*nx*ny ; ii++ ) bar[ii] = (byte)( bar[ii]*fac ) ;
00165     }
00166 
00167     return im ;
00168 }
00169 
00170 /*--------------------------------------------------------------------*/
00171 
00172 #include <sys/types.h>
00173 #include <sys/socket.h>
00174 #include <netinet/in.h>
00175 #include <netdb.h>
00176 #include <stdio.h>
00177 #include <arpa/inet.h>
00178 
00179 /*----------------------------------------------------------------
00180    Return the Internet address (in 'dot' format, as a string)
00181    given the name of the host.  If NULL is returned, some
00182    error occurrrrred.  The return string is malloc-ed and should
00183    be free-d someday.
00184 ------------------------------------------------------------------*/
00185 
00186 static char * xxx_name_to_inet( char * host )
00187 {
00188    struct hostent * hostp ;
00189    char * iname = NULL, * str ;
00190    int ll ;
00191 
00192    if( host == NULL || host[0] == '\0' ) return NULL ;
00193    hostp = gethostbyname(host) ; if( hostp == NULL ) return NULL ;
00194 
00195    str = inet_ntoa(*((struct in_addr *)(hostp->h_addr))) ;
00196    if( str == NULL || str[0] == '\0' ) return NULL ;
00197 
00198    ll = strlen(str) ; iname = malloc(ll+1) ; strcpy(iname,str) ;
00199    return iname ;
00200 }
00201 
00202 /*----------------------------------------------------------------*/
00203 
00204 static char * self_to_inet( void )
00205 {
00206    char hname[1048]="localhost" ;
00207    static char *ipad=NULL ;
00208    if( ipad == NULL ){ gethostname(hname,1048); ipad = xxx_name_to_inet(hname); }
00209    return ipad ;
00210 }
00211 
00212 /*----------------------------------------------------------------*/
00213 
00214 static int is_nih_host( void )
00215 {
00216    char *ipad = self_to_inet() ;
00217 
00218    if( ipad == NULL ) return 0 ;
00219 
00220    if( strstr(ipad,"128.231.") == ipad ) return 1 ;
00221    if( strstr(ipad,"137.187.") == ipad ) return 1 ;
00222 
00223 #if 1
00224    if( strstr(ipad,"130.14." ) == ipad ) return 1 ;
00225    if( strstr(ipad,"156.40." ) == ipad ) return 1 ;
00226    if( strstr(ipad,"165.112.") == ipad ) return 1 ;
00227    if( strstr(ipad,"157.98." ) == ipad ) return 1 ;
00228 #endif
00229 
00230 #if 1
00231    if( strstr(ipad,"129.43."      ) == ipad ) return 1 ;
00232    if( strstr(ipad,"199.249.158." ) == ipad ) return 1 ;
00233    if( strstr(ipad,"209.218.0."   ) == ipad ) return 1 ;
00234    if( strstr(ipad,"130.14."      ) == ipad ) return 1 ;
00235    if( strstr(ipad,"131.158.140." ) == ipad ) return 1 ;
00236    if( strstr(ipad,"150.148.11"   ) == ipad ) return 1 ;
00237    if( strstr(ipad,"150.148.21"   ) == ipad ) return 1 ;
00238 #endif
00239 
00240    return 0 ;
00241 }
00242 
00243 /*----------------------------------------------------------------*/
00244 
00245 static int can_get_testfile(void)
00246 {
00247    char *ttahome, *ndata=NULL, url[256]; int nn ;
00248 
00249    ttahome = getenv("AFNI_TTAHOME"); if( ttahome == NULL ) ttahome = TTAHOME;
00250    strcpy(url,ttahome) ; strcat(url,"testfile") ;
00251    nn = NI_read_URL( url, (char **)&ndata ) ;
00252    if( nn <= 0 || ndata == NULL ) return 0 ;
00253    if( ndata != NULL ) free(ndata) ;
00254    return 1 ;
00255 }
00256 
00257 /*-----------------------------------------------------------------*/
00258 
00259 #define NUM_ORIENT_STRINGS 4
00260 static char *orient_strings[4] = { "None", "Axial", "Sagittal", "Coronal" } ;
00261 static int orient = 0 ;
00262 
00263 static char * TTget_main( PLUGIN_interface * ) ;
00264 
00265 /*-----------------------------------------------------------------*/
00266 
00267 
00268 DEFINE_PLUGIN_PROTOTYPE
00269 
00270 PLUGIN_interface * PLUGIN_init( int ncall )
00271 {
00272    PLUGIN_interface * plint ;
00273 
00274 #if 1
00275    return NULL ;
00276 #endif
00277 
00278    if( ncall > 0 || !is_nih_host() || !can_get_testfile() ) return NULL ;
00279 
00280    plint = PLUTO_new_interface( "TT Atlas",
00281                                 "TT Atlas display",
00282                                 NULL,
00283                                 PLUGIN_CALL_VIA_MENU, TTget_main  ) ;
00284 
00285    PLUTO_add_option( plint, "Mode", "MODE", TRUE ) ;
00286 
00287    PLUTO_add_string( plint, "Orient", NUM_ORIENT_STRINGS, orient_strings, orient ) ;
00288 
00289    return plint ;
00290 }
00291 
00292 /*-----------------------------------------------------------------*/
00293 
00294 static int recv_key = -1 ;
00295 static Three_D_View *old_im3d = NULL ;
00296 static char old_url[256] = "\0" ;
00297 
00298 static void TTget_recv( int why, int np, int * ijk, void * junk ) ;
00299 
00300 /*-----------------------------------------------------------------*/
00301 
00302 static char * TTget_main( PLUGIN_interface *plint )
00303 {
00304    char *str ;
00305 
00306    PLUTO_next_option(plint) ;
00307    str    = PLUTO_get_string(plint) ;
00308    orient = PLUTO_string_index( str, NUM_ORIENT_STRINGS, orient_strings ) ;
00309 
00310    old_url[0] = '\0' ;
00311 
00312    if( plint->im3d != old_im3d || !IM3D_OPEN(plint->im3d) ){
00313      if( recv_key >= 0 ){
00314        AFNI_receive_control( old_im3d,recv_key,EVERYTHING_SHUTDOWN,NULL ) ;
00315        recv_key = -1 ;
00316      }
00317      old_im3d = plint->im3d ;
00318      if( !IM3D_OPEN(plint->im3d) )
00319        return "***************************************\n"
00320               "TTget_main: AFNI controller isn't open!\n"
00321               "***************************************"   ;
00322    }
00323 
00324    if( orient == 0 && recv_key >= 0 ){
00325      AFNI_receive_control( plint->im3d,recv_key,EVERYTHING_SHUTDOWN,NULL ) ;
00326      recv_key = -1 ;
00327    } else if( orient > 0 && recv_key < 0 ){
00328      recv_key = AFNI_receive_init( plint->im3d, RECEIVE_VIEWPOINT_MASK,
00329                                    TTget_recv, NULL , "TTget_recv"      ) ;
00330    }
00331 
00332    return NULL ;
00333 }
00334 
00335 /*-----------------------------------------------------------------*/
00336 
00337 static void *impop = NULL ;
00338 
00339 static void TTget_recv( int why, int np, int * ijk, void * junk )
00340 {
00341    if( orient == 0          ||
00342        !IM3D_OPEN(old_im3d) ||
00343        old_im3d->vinfo->view_type != VIEW_TALAIRACH_TYPE ) return ;
00344 
00345    switch( why ){
00346 
00347       case RECEIVE_VIEWPOINT:{ /*-- change of crosshair location --*/
00348         MRI_IMAGE *im ;
00349         char *url ;
00350 
00351         url = TTget_URL( orient-1, old_im3d->vinfo->xi,
00352                                     old_im3d->vinfo->yj, old_im3d->vinfo->zk ) ;
00353 
00354         if( strcmp(url,old_url) == 0 ) return ;
00355         strcpy(old_url,url) ;
00356 
00357         im = TTget_ppm( url ) ; if( im == NULL ) return ;
00358 
00359         impop = PLUTO_popup_image( impop, im ) ;
00360         mri_free(im) ;
00361       }
00362       break ;
00363 
00364    }
00365 
00366    return ;
00367 }
 

Powered by Plone

This site conforms to the following standards: