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  

xvisual.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006    
00007 #define MAIN
00008 
00009 #include <X11/Intrinsic.h>
00010 #include <X11/StringDefs.h>
00011 #include <X11/Shell.h>
00012 #include <stdio.h>
00013 
00014 #include <Xm/XmAll.h>
00015 #include "mrilib.h"
00016 
00017 static char * vcl[] =  { "StaticGray"  , "GrayScale" , "StaticColor" ,
00018                          "PseudoColor" , "TrueColor" , "DirectColor"  } ;
00019 
00020 static XImage * xim    = NULL ;
00021 static int      xim_ww = 0 ;
00022 static int      xim_hh = 0 ;
00023 
00024 #undef USE_TRUECOLOR
00025 
00026 #define USE_PIXMAP
00027 #ifdef  USE_PIXMAP
00028 # define WANT_LOGO_BITMAP
00029 # include "logo.h"
00030 #endif
00031 
00032 XImage * rgb_to_XImage( Display * dis , XVisualInfo * vin , MRI_IMAGE * im ) ;
00033 
00034 void fred_CB( Widget w , XtPointer cd , XtPointer cb ){ exit(0); }
00035 
00036 void elvis_CB( Widget w , XtPointer cd , XtPointer cb )
00037 {
00038    static int needGC = 1 ;
00039    static  GC myGC ;
00040    XmDrawingAreaCallbackStruct * cbs = (XmDrawingAreaCallbackStruct *) cb ;
00041    XExposeEvent * ev = (XExposeEvent *) cbs->event ;
00042    Dimension nx , ny ;
00043    int ii , jj ;
00044 
00045    if( cbs->reason != XmCR_EXPOSE || ev->count > 0 ) return ;
00046 
00047    if( needGC ){
00048      XGCValues  gcv;
00049      gcv.function = GXcopy ;
00050      myGC  = XCreateGC( XtDisplay(w) , XtWindow(w) , GCFunction , &gcv ) ;
00051      needGC = 0 ;
00052    }
00053 
00054    XtVaGetValues( w , XmNwidth  , &nx , XmNheight , &ny , NULL ) ;
00055 
00056 #ifdef USE_TRUECOLOR
00057    ii = 0 ;
00058    do{
00059       jj = 0 ;
00060       do{
00061          XPutImage( XtDisplay(w),XtWindow(w),myGC,xim,0,0,ii,jj,xim_ww,xim_hh) ;
00062          jj += xim_hh + 4 ;
00063       } while( jj < ny ) ;
00064       ii += xim_ww ;
00065    } while( ii < nx ) ;
00066 #else
00067 # ifdef USE_PIXMAP
00068    ii = 0 ;
00069    do{ jj = 0 ;
00070        do{
00071           XCopyArea( XtDisplay(w),logo_pixmap , XtWindow(w),myGC ,
00072                      0,0,logo_width,logo_height , ii,jj ) ;
00073          jj += logo_height + 4 ;
00074       } while( jj < ny ) ;
00075       ii += logo_width ;
00076    } while( ii < nx ) ;
00077 # endif /* USE_PIXMAP */
00078 #endif /* USE_TRUECOLOR */
00079 
00080    return ;
00081 }
00082 
00083 int main( int argc , char * argv[] )
00084 {
00085         XtAppContext    app;            /* the application context */
00086         Widget          top;            /* toplevel widget */
00087         Display         *dpy;           /* display */
00088         Colormap        colormap;       /* created colormap */
00089         XVisualInfo     vinfo;          /* template for find visual */
00090         Visual          *vis ;          /* the Visual itself */
00091         XVisualInfo     *vinfo_list;    /* returned list of visuals */
00092         int             count;          /* number of matchs (only 1?) */
00093         int             vid , stat ;
00094         Widget          fred , fff ;
00095 
00096         top = XtVaAppInitialize( &app , "test" , NULL , 0 , &argc , argv , NULL , NULL ) ;
00097         dpy = XtDisplay (top);
00098 
00099 #ifndef USE_TRUECOLOR
00100         stat = XMatchVisualInfo( dpy,XScreenNumberOfScreen(XtScreen(top)),
00101                                  8,PseudoColor,&vinfo ) ;
00102         if( stat == 0 ){ printf("no 8 bit visual\n") ; exit(1) ; }
00103 #else
00104         vid = strtol( argv[1] , NULL , 0 ) ;
00105         vinfo.visualid = (VisualID) vid ;
00106         vinfo_list = XGetVisualInfo (dpy, VisualIDMask, &vinfo, &count);
00107         if( count == 0 || vinfo_list == NULL ){fprintf(stderr,"no match\n");exit(1);}
00108         vinfo = vinfo_list[0] ;
00109 #endif
00110         vid = vinfo.visualid ;
00111         vis = vinfo.visual ;
00112 
00113         colormap = XCreateColormap( dpy, RootWindowOfScreen(XtScreen(top)) ,
00114                                     vis , AllocNone ) ;
00115 
00116         XtVaSetValues( top ,
00117                           XtNborderColor , 0 ,
00118                           XtNbackground  , 0 ,
00119                           XtNdepth       , vinfo.depth ,
00120                           XtNcolormap    , colormap ,
00121                           XtNvisual      , vis ,
00122                        NULL ) ;
00123 
00124         fff = XtVaCreateWidget( "dialog" , xmFormWidgetClass , top ,
00125                                    XmNborderWidth , 0 ,
00126                                 NULL ) ;
00127 
00128 #ifndef LABEL_ARG
00129 #define LABEL_ARG(str) \
00130   XtVaTypedArg , XmNlabelString , XmRString , (str) , strlen(str)+1
00131 #endif
00132 
00133         fred = XtVaCreateManagedWidget( "dialog" , xmPushButtonWidgetClass , fff ,
00134                                           LABEL_ARG("Jumpback") ,
00135                                           XmNtopAttachment    , XmATTACH_FORM ,
00136                                           XmNleftAttachment   , XmATTACH_FORM ,
00137                                           XmNrightAttachment  , XmATTACH_FORM ,
00138                                          NULL ) ;
00139         XtAddCallback( fred , XmNactivateCallback , fred_CB , NULL ) ;
00140 
00141         fred = XtVaCreateManagedWidget( "dialog" , xmDrawingAreaWidgetClass , fff ,
00142                                           XmNtopAttachment    , XmATTACH_WIDGET ,
00143                                           XmNtopWidget        , fred ,
00144                                           XmNleftAttachment   , XmATTACH_FORM ,
00145                                           XmNrightAttachment  , XmATTACH_FORM ,
00146                                           XmNbottomAttachment , XmATTACH_FORM ,
00147                                         NULL ) ;
00148 
00149         XtAddCallback( fred , XmNexposeCallback , elvis_CB , NULL ) ;
00150 
00151 #ifdef USE_TRUECOLOR
00152         { MRI_IMAGE * im ;
00153           im = mri_read_ppm( "bob.ppm" ) ;
00154           xim = rgb_to_XImage( XtDisplay(top) , &vinfo , im ) ;
00155           xim_ww = im->nx ; xim_hh = im->ny ;
00156           mri_free(im) ;
00157         }
00158 #else
00159         xim_ww = xim_hh = 77 ;
00160 #endif
00161 
00162         XtVaSetValues( top ,
00163                          XmNwidth , xim_ww ,
00164                          XmNheight , xim_hh+40 ,
00165                        NULL ) ;
00166 
00167 #ifdef USE_PIXMAP
00168    {  Pixel bg_pix  , fg_pix  ;
00169 # define ICON_bg bg_pix
00170 # define ICON_fg fg_pix
00171 
00172       XtVaGetValues( fred ,
00173                        XmNforeground , &bg_pix ,  /* note reversal of roles here! */
00174                        XmNbackground , &fg_pix ,
00175                      NULL ) ;
00176 
00177       logo_pixmap = XCreatePixmapFromBitmapData(
00178                         XtDisplay(top) ,
00179                         RootWindowOfScreen(XtScreen(top)) ,
00180                         logo_bits , logo_width , logo_height ,
00181                         fg_pix , bg_pix ,
00182                         DefaultDepthOfScreen(XtScreen(top)) ) ;
00183 
00184       XtVaSetValues( top , XmNiconPixmap , logo_pixmap , NULL ) ;
00185    }
00186 #endif
00187 
00188 
00189         XtManageChild(fff) ;
00190         XtRealizeWidget(top);
00191         XtAppMainLoop(app);
00192 
00193         exit(0); /* never reached */
00194 }
00195 
00196 #ifdef USE_TRUECOLOR
00197 /*---------------------------------------------------------------------------
00198    Create an XImage from an RGB image.  Adapted from program "xv".
00199    The output of this can be XPutImage-d to a window.
00200 -----------------------------------------------------------------------------*/
00201 
00202 static int highbit(unsigned long ul) ;  /* prototype */
00203 
00204 XImage * rgb_to_XImage( Display * dis , XVisualInfo * vin , MRI_IMAGE * im )
00205 {
00206    unsigned long r, g, b, rmask, gmask, bmask ;
00207    int           rshift, gshift, bshift, bperpix, bperline, border, i,j ;
00208    int           wide, high ;
00209    byte         *imagedata, *lip, *ip, *pp ;
00210    XImage       *xim = NULL ;
00211    int          *xcol ;
00212 
00213    /* check inputs */
00214 
00215 #if defined(__cplusplus) || defined(c_plusplus)
00216    if( vin==NULL || vin->c_class!=TrueColor || im==NULL || im->kind!=MRI_rgb ){
00217 #else
00218    if( vin==NULL || vin->class!=TrueColor || im==NULL || im->kind!=MRI_rgb ){
00219 #endif
00220       fprintf(stderr,"\a\n*** ILLEGAL input to rgb_to_XImage\n") ;
00221       sleep(1) ; exit(1) ;
00222    }
00223 
00224    /* get color masks and shifts to put high bit of color byte into place */
00225 
00226    rmask = vin->red_mask   ; rshift = 7 - highbit(rmask) ;
00227    gmask = vin->green_mask ; gshift = 7 - highbit(gmask) ;
00228    bmask = vin->blue_mask  ; bshift = 7 - highbit(bmask) ;
00229 
00230    /* input image dimensions */
00231 
00232    wide = im->nx ;
00233    high = im->ny ;
00234 
00235    /* make output XImage */
00236 
00237    xim = XCreateImage( dis , vin->visual , vin->depth , ZPixmap, 0, NULL,
00238                        wide,  high, 32, 0) ;
00239 
00240    bperline = xim->bytes_per_line ;
00241    bperpix  = xim->bits_per_pixel ;
00242    border   = xim->byte_order ;
00243 
00244    if(bperpix != 8 && bperpix != 16 && bperpix != 24 && bperpix != 32){
00245       fprintf(stderr,"\a\n*** rgb_to_XImage: can't use %d-bit TrueColor\n",bperpix) ;
00246       sleep(1) ; exit(1) ;
00247    }
00248 
00249    /* make output image array */
00250 
00251    imagedata = (byte *) XtMalloc((size_t) (high * bperline)) ;
00252    xim->data = (char *) imagedata ;
00253 
00254    lip = imagedata ;       /* pointer to row of output image array */
00255    pp  = MRI_RGB_PTR(im) ; /* pointer to input image data */
00256 
00257    xcol = (int *) malloc(sizeof(int) * wide) ; /* row of output colors */
00258 
00259    /*-- loop over image --*/
00260 
00261    for (i=0; i<high; i++, lip+=bperline) {  /* loop over rows */
00262 
00263       for (j=0, ip=lip; j<wide; j++) {      /* load color for row #i */
00264 
00265         r = *pp++ ;  g = *pp++ ;  b = *pp++ ;  /* get input RGB byte values */
00266 
00267         /* shift each component to the correct position, and mask it off */
00268 
00269         r = (rshift<0) ? (r<<(-rshift)) : (r>>rshift) ; r = r & rmask ;
00270         g = (gshift<0) ? (g<<(-gshift)) : (g>>gshift) ; g = g & gmask ;
00271         b = (bshift<0) ? (b<<(-bshift)) : (b>>bshift) ; b = b & bmask ;
00272 
00273         xcol[j] = r | g | b ;  /* assemble color for this pixel */
00274       }
00275 
00276       /* now put all colors for row #i into output image array,
00277          allowing for most possible bytes/pixel and byte ordering stupidities */
00278 
00279       switch( bperpix ){
00280            case 32:
00281              if (border == MSBFirst)
00282                 for( j=0 ; j < wide ; j++ ){
00283                    *ip++ = (xcol[j]>>24) & 0xff ;
00284                    *ip++ = (xcol[j]>>16) & 0xff ;
00285                    *ip++ = (xcol[j]>>8)  & 0xff ;
00286                    *ip++ =  xcol[j]      & 0xff ;
00287                  }
00288              else
00289                 for( j=0 ; j < wide ; j++ ){
00290                    *ip++ =  xcol[j]      & 0xff ;
00291                    *ip++ = (xcol[j]>>8)  & 0xff ;
00292                    *ip++ = (xcol[j]>>16) & 0xff ;
00293                    *ip++ = (xcol[j]>>24) & 0xff ;
00294                  }
00295            break ;
00296 
00297            case 24:
00298              if (border == MSBFirst)
00299                 for( j=0 ; j < wide ; j++ ){
00300                   *ip++ = (xcol[j]>>16) & 0xff ;
00301                   *ip++ = (xcol[j]>>8)  & 0xff ;
00302                   *ip++ =  xcol[j]      & 0xff ;
00303                 }
00304              else
00305                 for( j=0 ; j < wide ; j++ ){
00306                   *ip++ =  xcol[j]      & 0xff ;
00307                   *ip++ = (xcol[j]>>8)  & 0xff ;
00308                   *ip++ = (xcol[j]>>16) & 0xff ;
00309                 }
00310            break ;
00311 
00312            case 16:
00313              if (border == MSBFirst)
00314                 for( j=0 ; j < wide ; j++ ){
00315                   *ip++ = (xcol[j]>>8)  & 0xff ;
00316                   *ip++ =  xcol[j]      & 0xff ;
00317                 }
00318              else
00319                 for( j=0 ; j < wide ; j++ ){
00320                   *ip++ =  xcol[j]      & 0xff ;
00321                   *ip++ = (xcol[j]>>8)  & 0xff ;
00322                 }
00323            break ;
00324 
00325            case 8:
00326                 for( j=0 ; j < wide ; j++ )
00327                    *ip++ = xcol[j] & 0xff ;
00328            break ;
00329          }
00330     } /* end of loop over rows */
00331 
00332    free(xcol) ;
00333    return xim ;
00334 }
00335 
00336 /*------------------------------------------------------------------------
00337   Returns position of highest set bit in 'ul' as an integer (0-31),
00338   or returns -1 if no bit is set.
00339 --------------------------------------------------------------------------*/
00340 
00341 static int highbit(unsigned long ul)
00342 {
00343   int i;  unsigned long hb;
00344 
00345   hb = 0x80;  hb = hb << 24;   /* hb = 0x80000000UL */
00346   for (i=31; ((ul & hb) == 0) && i>=0;  i--, ul<<=1);
00347   return i;
00348 }
00349 #endif
00350 
00351 /*----------  Fix a Linux stupidity  ------------------------------------*/
00352 
00353 #include "machdep.h"
00354 #ifdef NEED_XSETLOCALE
00355 #include <locale.h>
00356 char * _Xsetlocale( int category, const char * locale)
00357 { return setlocale(category,locale) ; }
00358 #endif
 

Powered by Plone

This site conforms to the following standards: