home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / graphics / 8892 < prev    next >
Encoding:
Text File  |  1992-08-16  |  17.0 KB  |  446 lines

  1. Xref: sparky comp.graphics:8892 comp.graphics.visualization:1219 alt.graphics.pixutils:1957
  2. Path: sparky!uunet!usc!news
  3. From: merlin@neuro.usc.edu (merlin)
  4. Newsgroups: comp.graphics,comp.graphics.visualization,alt.graphics.pixutils
  5. Subject: Quick & Dirty SUN X11 Based Viewer for Chapel Hill 3D MRI of Human Head
  6. Date: 17 Aug 1992 02:46:11 -0700
  7. Organization: University of Southern California, Los Angeles, CA
  8. Lines: 432
  9. Sender: merlin@neuro.usc.edu (merlin)
  10. Message-ID: <l8utb3INN7qb@neuro.usc.edu>
  11. References: x
  12. NNTP-Posting-Host: neuro.usc.edu
  13.  
  14. /***
  15.  
  16. purpose:  SUN X11 viewer for omicron.cs.unc.edu:/pub/softlab/CHVRTD/volI/3dhead
  17.  
  18. status:   quick & dirty first attempt at interactive X11 based image viewing tool
  19.  
  20.           compulsive programmers are invited to add their own favorite features
  21.           -- scroll bars indicating current position with single slice and multi
  22.           slice advance arbitrary image positioning along the bars would be nice
  23.  
  24. data:     the 14 MByte 3D MRI of the human head may be obtained by anonymous ftp:
  25.  
  26.           ftp -i omicron.cs.unc.edu
  27.           username:  anonymous
  28.           password:  yourname@yoursite.yourcampus.edu
  29.           ftp> cd pub/softlab/CHVRTD
  30.           ftp> binary
  31.           ftp> get 3dhead
  32.           ftp> quit
  33.  
  34. compile:  cc -o vhead vhead.c -L/usr/public/X11/lib -lX11
  35.  
  36. run:      setenv DISPLAY yourhost:0.0; foo9
  37.  
  38.           this program simultaneously displays parasagital, coronal, and horizontal
  39.           MRI slices from a human head -- within any one of these three images the
  40.           left and right mouse buttons cause the display to increment one image at
  41.           a time while the middle mouse button will rapidly advance to the position
  42.           marked by the position of the cursor along a vertical line in each slice. 
  43.  
  44. author:   alexander-james annala
  45.  
  46.           principal investigator
  47.           neuroscience image analysis network
  48.  
  49.           -- and --
  50.  
  51.           ph.d. graduate student            
  52.           neuroscience program
  53.  
  54.           hedco neuroscience bldg, room 534a
  55.           university of southern california
  56.           los angeles, ca 90089-2520
  57.  
  58. request:  if you enhance this code i would appreciate an updated copy.  thanks, aj
  59.  
  60. ***/
  61.  
  62. #include <X11/Xlib.h>
  63. #include <X11/Xutil.h>
  64. #include <X11/Xos.h>
  65. #include <stdio.h>
  66.  
  67. #define min(a,b) (((a) < (b)) ? (a) : (b))
  68.  
  69. char *display_name = NULL;
  70. Display *display;
  71. int screen;
  72. Colormap colormap;
  73. XColor colors[256], fg_color, bg_color;
  74. Window window;
  75. unsigned int width = 1166, height = 512;
  76. unsigned int imwidth = 256, imheight = 256;
  77. int imx = 0, imy = 0;
  78. int x = 0, y = 0;
  79. unsigned int border_width = 4;
  80. XEvent report;
  81. GC gc;
  82. XFontStruct *font_info;
  83. char *fontname = "9x15";
  84. unsigned long valuemask = 0;
  85. XGCValues values;
  86. Visual *visual=NULL;
  87. int i, j, k, k1, k2, k3;
  88. int ncolors=256;
  89. int isize=16;
  90. FILE *f;
  91. unsigned char *data;
  92. unsigned short *datas;
  93. int a, tmp;
  94. Pixmap pic;
  95. XImage *image;
  96. char *infilename = "3dhead"; 
  97. unsigned char *ptr;
  98. XSetWindowAttributes setwinattr;
  99.  
  100. main(argc, argv)
  101.   int argc;
  102.   char *argv[];
  103. {
  104.   k1=54;
  105.   k2=128;
  106.   k3=128;
  107.   for (a=1; (a<argc) && (*argv[a]=='-'); a++)
  108.     if (!strcmp("-in", argv[a]))
  109.       infilename=argv[++a];
  110.     else if (!strcmp("-w", argv[a]))
  111.       imwidth=atoi(argv[++a]);
  112.     else if (!strcmp("-h", argv[a]))
  113.       imheight=atoi(argv[++a]);
  114.  
  115.   if ((display=XOpenDisplay(display_name))==NULL)
  116.     {
  117.       fprintf(stderr, "cannot connect to server %s\n",
  118.         XDisplayName(display_name));
  119.       exit(-1);
  120.     }
  121.   screen = DefaultScreen(display);
  122.   visual = XDefaultVisual(display, screen);
  123.   window = XCreateSimpleWindow(display, RootWindow(display,screen),
  124.              x, y, width, height, border_width,
  125.              BlackPixel(display,screen),
  126.              WhitePixel(display,screen));
  127.   valuemask = CWBackingStore;
  128.   setwinattr.backing_store = NotUseful;
  129.   XChangeWindowAttributes(display, window, valuemask, &setwinattr);
  130.  
  131.   XMapWindow(display, window);
  132.  
  133.   XSelectInput(display, window, ExposureMask|KeyPressMask|ButtonPressMask|
  134.                                 StructureNotifyMask);
  135.  
  136.   if ((font_info=XLoadQueryFont(display, fontname))==NULL)
  137.     {
  138.       fprintf(stderr,"cannot load font %s\n", fontname);
  139.       exit(-1);
  140.     }
  141.  
  142.   for (i=0; i<ncolors; i++)
  143.     {
  144.       colors[i].pixel = (u_long)(i);
  145.       colors[i].red   = (u_short)(i<<8);
  146.       colors[i].green = (u_short)(i<<8);
  147.       colors[i].blue  = (u_short)(i<<8);
  148.       colors[i].flags = DoRed | DoGreen | DoBlue ;
  149.     }
  150.  
  151.   colormap=XCreateColormap(display, window, visual, AllocAll);
  152.   XStoreColors(display, colormap, colors, ncolors);
  153.   XSetWindowColormap(display, window, colormap);
  154.  
  155.   gc = XCreateGC(display, window, 0, &values);
  156.   XSetForeground(display, gc, WhitePixel(display, screen));
  157.   XSetBackground(display, gc, BlackPixel(display, screen));
  158.  
  159.   /************************************************************************/
  160.   f = fopen(infilename,"r");
  161.  
  162.   if ((datas=(unsigned short *)malloc(imheight*imwidth*109*2))==NULL)
  163.     {
  164.       fprintf(stderr,"malloc error 1\n");
  165.       exit(1);
  166.     }
  167.   if (fread(datas, imwidth*imheight*109*2, 1, f) != 1)
  168.     {
  169.       fprintf(stderr,"read error -- maybe short image\n");
  170.       exit(1);
  171.     }
  172.   if ((data=(unsigned char *)malloc(imheight*imwidth*4))==NULL)
  173.     {
  174.       fprintf(stderr,"malloc error 2\n");
  175.       exit(1);
  176.     }
  177.   for (i=0; i<(imheight*imwidth*109); i++)
  178.     {
  179.       ptr = (unsigned char *) (datas+i);
  180.       tmp = *(ptr);
  181.       *ptr = *(ptr+1);
  182.       *(ptr+1) = tmp;
  183.       *(datas+i) = (*(datas+i)) >> 3;
  184.       
  185.     }
  186.  
  187.   pic = XCreatePixmap(display, window, (imwidth*2), (imheight*2), 8);
  188.   image=XCreateImage(display,visual,8,ZPixmap,0,data,(imwidth*2),(imheight*2),8,(imwidth*2));
  189.   image->byte_order=MSBFirst;
  190.   image->bitmap_bit_order=MSBFirst;
  191.   values.function=GXcopy;
  192.   gc = XCreateGC(display, pic, GCFunction, &values);
  193.   XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));
  194.   /************************************************************************/
  195.   k=0;
  196.   while (1)
  197.     {
  198.       XNextEvent(display, &report);
  199.       switch(report.type)
  200.         {
  201.           case Expose:
  202.           {
  203.                   for (j=0; j<(imheight); j++)
  204.                     for (i=0; i<(imwidth); i++)
  205.                       { 
  206.                         tmp = min(255, *(datas+(k1<<16)+(j*imwidth)+i));
  207.                 *(data+(((j*4)*imwidth)+(i*2))) = tmp;
  208.                 *(data+(((j*4)*imwidth)+(i*2)+1)) = tmp;
  209.                 *(data+((((j*4)+2)*imwidth)+(i*2))) = tmp;
  210.                 *(data+((((j*4)+2)*imwidth)+(i*2)+1)) = tmp;
  211.                       }
  212.           XPutImage(display, window, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));
  213.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  214.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  215.  
  216.                   for (j=0; j<(imheight); j++)
  217.                     for (i=0; i<(109); i++)
  218.                       {
  219.                         tmp = min(255, *(datas+(i<<16)+(j*imwidth)+k2));
  220.                 *(data+(((j*4)*imwidth)+(i*3))) = tmp;
  221.                 *(data+(((j*4)*imwidth)+(i*3)+1)) = tmp;
  222.                 *(data+(((j*4)*imwidth)+(i*3)+2)) = tmp;
  223.                 *(data+((((j*4)+2)*imwidth)+(i*3))) = tmp;
  224.                 *(data+((((j*4)+2)*imwidth)+(i*3)+1)) = tmp;
  225.                 *(data+((((j*4)+2)*imwidth)+(i*3)+2)) = tmp;
  226.                       }
  227.           XPutImage(display, window, gc, image, 0, 0, 512, 0, (109*3), (imheight*2));
  228.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  229.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  230.  
  231.                   for (j=0; j<(imheight); j++)
  232.                     for (i=0; i<(109); i++)
  233.                       {
  234.                         tmp = min(255, *(datas+(i<<16)+(k3*imwidth)+j));
  235.                 *(data+(((j*4)*imwidth)+(i*3))) = tmp;
  236.                 *(data+(((j*4)*imwidth)+(i*3)+1)) = tmp;
  237.                 *(data+(((j*4)*imwidth)+(i*3)+2)) = tmp;
  238.                 *(data+((((j*4)+2)*imwidth)+(i*3))) = tmp;
  239.                 *(data+((((j*4)+2)*imwidth)+(i*3)+1)) = tmp;
  240.                 *(data+((((j*4)+2)*imwidth)+(i*3)+2)) = tmp;
  241.                       }
  242.           XPutImage(display, window, gc, image, 0, 0, 839, 0, (109*3), (imheight*2));
  243.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  244.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  245.  
  246.           }
  247.             break;
  248.           case ButtonPress:
  249.             {
  250.             switch(report.xbutton.button)
  251.               {
  252.                 case Button1:
  253.           {
  254.                   if (report.xbutton.x < 512) 
  255.           {
  256.                   if (k1 < 108)
  257.                     k1 = k1 + 1;
  258.                   for (j=0; j<(imheight); j++)
  259.                     for (i=0; i<(imwidth); i++)
  260.                       {
  261.                         tmp = min(255, *(datas+(k1<<16)+(j*imwidth)+i));
  262.                 *(data+(((j*4)*imwidth)+(i*2))) = tmp;
  263.                 *(data+(((j*4)*imwidth)+(i*2)+1)) = tmp;
  264.                 *(data+((((j*4)+2)*imwidth)+(i*2))) = tmp;
  265.                 *(data+((((j*4)+2)*imwidth)+(i*2)+1)) = tmp;
  266.                       }
  267.           XPutImage(display, window, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));
  268.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  269.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  270.           }
  271.                   if ( (report.xbutton.x > 512) && (report.xbutton.x < 839) )
  272.           {
  273.                   if (k2 < 255)
  274.                     k2 = k2 + 1;
  275.                   for (j=0; j<(imheight); j++)
  276.                     for (i=0; i<(109); i++)
  277.                       {
  278.                         tmp = min(255, *(datas+(i<<16)+(j*imwidth)+k2));
  279.                 *(data+(((j*4)*imwidth)+(i*3))) = tmp;
  280.                 *(data+(((j*4)*imwidth)+(i*3)+1)) = tmp;
  281.                 *(data+(((j*4)*imwidth)+(i*3)+2)) = tmp;
  282.                 *(data+((((j*4)+2)*imwidth)+(i*3))) = tmp;
  283.                 *(data+((((j*4)+2)*imwidth)+(i*3)+1)) = tmp;
  284.                 *(data+((((j*4)+2)*imwidth)+(i*3)+2)) = tmp;
  285.                       }
  286.           XPutImage(display, window, gc, image, 0, 0, 512, 0, (109*3), (imheight*2));
  287.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  288.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  289.           }
  290.                   if ( report.xbutton.x > 840 )
  291.           {
  292.                   if (k3 < 255)
  293.                     k3 = k3 + 1;
  294.                   for (j=0; j<(imheight); j++)
  295.                     for (i=0; i<(109); i++)
  296.                       {
  297.                         tmp = min(255, *(datas+(i<<16)+(k3*imwidth)+j));
  298.                 *(data+(((j*4)*imwidth)+(i*3))) = tmp;
  299.                 *(data+(((j*4)*imwidth)+(i*3)+1)) = tmp;
  300.                 *(data+(((j*4)*imwidth)+(i*3)+2)) = tmp;
  301.                 *(data+((((j*4)+2)*imwidth)+(i*3))) = tmp;
  302.                 *(data+((((j*4)+2)*imwidth)+(i*3)+1)) = tmp;
  303.                 *(data+((((j*4)+2)*imwidth)+(i*3)+2)) = tmp;
  304.                       }
  305.           XPutImage(display, window, gc, image, 0, 0, 839, 0, (109*3), (imheight*2));
  306.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  307.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  308.           }
  309.           }
  310.                   break;
  311.                 case Button2:
  312.           {
  313.                   if (report.xbutton.x < 512) 
  314.           {
  315.                   k1 = report.xbutton.y * 109 / height;
  316.                   for (j=0; j<(imheight); j++)
  317.                     for (i=0; i<(imwidth); i++)
  318.                       {
  319.                         tmp = min(255, *(datas+(k1<<16)+(j*imwidth)+i));
  320.                 *(data+(((j*4)*imwidth)+(i*2))) = tmp;
  321.                 *(data+(((j*4)*imwidth)+(i*2)+1)) = tmp;
  322.                 *(data+((((j*4)+2)*imwidth)+(i*2))) = tmp;
  323.                 *(data+((((j*4)+2)*imwidth)+(i*2)+1)) = tmp;
  324.                       }
  325.           XPutImage(display, window, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));
  326.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  327.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  328.           }
  329.                   if ( (report.xbutton.x > 512) && (report.xbutton.x < 839) )
  330.           {
  331.                   k2 = report.xbutton.y * 256 / height;
  332.                   for (j=0; j<(imheight); j++)
  333.                     for (i=0; i<(109); i++)
  334.                       {
  335.                         tmp = min(255, *(datas+(i<<16)+(j*imwidth)+k2));
  336.                 *(data+(((j*4)*imwidth)+(i*3))) = tmp;
  337.                 *(data+(((j*4)*imwidth)+(i*3)+1)) = tmp;
  338.                 *(data+(((j*4)*imwidth)+(i*3)+2)) = tmp;
  339.                 *(data+((((j*4)+2)*imwidth)+(i*3))) = tmp;
  340.                 *(data+((((j*4)+2)*imwidth)+(i*3)+1)) = tmp;
  341.                 *(data+((((j*4)+2)*imwidth)+(i*3)+2)) = tmp;
  342.                       }
  343.           XPutImage(display, window, gc, image, 0, 0, 512, 0, (109*3), (imheight*2));
  344.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  345.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  346.           }
  347.                   if ( report.xbutton.x > 840 )
  348.           {
  349.                   k3 = report.xbutton.y * 256 / height;
  350.                   for (j=0; j<(imheight); j++)
  351.                     for (i=0; i<(109); i++)
  352.                       {
  353.                         tmp = min(255, *(datas+(i<<16)+(k3*imwidth)+j));
  354.                 *(data+(((j*4)*imwidth)+(i*3))) = tmp;
  355.                 *(data+(((j*4)*imwidth)+(i*3)+1)) = tmp;
  356.                 *(data+(((j*4)*imwidth)+(i*3)+2)) = tmp;
  357.                 *(data+((((j*4)+2)*imwidth)+(i*3))) = tmp;
  358.                 *(data+((((j*4)+2)*imwidth)+(i*3)+1)) = tmp;
  359.                 *(data+((((j*4)+2)*imwidth)+(i*3)+2)) = tmp;
  360.                       }
  361.           XPutImage(display, window, gc, image, 0, 0, 839, 0, (109*3), (imheight*2));
  362.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  363.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  364.           }
  365.           }
  366.                   break;
  367.                 case Button3:
  368.           {
  369.                   if (report.xbutton.x < 512) 
  370.           {
  371.                   if (k1 > 0)
  372.                     k1 = k1 - 1;
  373.                   for (j=0; j<(imheight); j++)
  374.                     for (i=0; i<(imwidth); i++)
  375.                       {
  376.                         tmp = min(255, *(datas+(k1<<16)+(j*imwidth)+i));
  377.                 *(data+(((j*4)*imwidth)+(i*2))) = tmp;
  378.                 *(data+(((j*4)*imwidth)+(i*2)+1)) = tmp;
  379.                 *(data+((((j*4)+2)*imwidth)+(i*2))) = tmp;
  380.                 *(data+((((j*4)+2)*imwidth)+(i*2)+1)) = tmp;
  381.                       }
  382.           XPutImage(display, window, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));
  383.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  384.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  385.           }
  386.                   if ( (report.xbutton.x > 512) && (report.xbutton.x < 839) )
  387.           {
  388.                   if (k2 > 0)
  389.                     k2 = k2 - 1;
  390.                   for (j=0; j<(imheight); j++)
  391.                     for (i=0; i<(109); i++)
  392.                       {
  393.                         tmp = min(255, *(datas+(i<<16)+(j*imwidth)+k2));
  394.                 *(data+(((j*4)*imwidth)+(i*3))) = tmp;
  395.                 *(data+(((j*4)*imwidth)+(i*3)+1)) = tmp;
  396.                 *(data+(((j*4)*imwidth)+(i*3)+2)) = tmp;
  397.                 *(data+((((j*4)+2)*imwidth)+(i*3))) = tmp;
  398.                 *(data+((((j*4)+2)*imwidth)+(i*3)+1)) = tmp;
  399.                 *(data+((((j*4)+2)*imwidth)+(i*3)+2)) = tmp;
  400.                       }
  401.           XPutImage(display, window, gc, image, 0, 0, 512, 0, (109*3), (imheight*2));
  402.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  403.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  404.           }
  405.                   if ( report.xbutton.x > 840 )
  406.           {
  407.                   if (k3 > 0)
  408.                     k3 = k3 - 1;
  409.                   for (j=0; j<(imheight); j++)
  410.                     for (i=0; i<(109); i++)
  411.                       {
  412.                         tmp = min(255, *(datas+(i<<16)+(k3*imwidth)+j));
  413.                 *(data+(((j*4)*imwidth)+(i*3))) = tmp;
  414.                 *(data+(((j*4)*imwidth)+(i*3)+1)) = tmp;
  415.                 *(data+(((j*4)*imwidth)+(i*3)+2)) = tmp;
  416.                 *(data+((((j*4)+2)*imwidth)+(i*3))) = tmp;
  417.                 *(data+((((j*4)+2)*imwidth)+(i*3)+1)) = tmp;
  418.                 *(data+((((j*4)+2)*imwidth)+(i*3)+2)) = tmp;
  419.                       }
  420.           XPutImage(display, window, gc, image, 0, 0, 839, 0, (109*3), (imheight*2));
  421.                   /*** XPutImage(display, pic, gc, image, 0, 0, 0, 0, (imwidth*2), (imheight*2));  ***/
  422.                   /*** XCopyArea(display, pic, window, gc, 0, 0, (imwidth*2), (imheight*2), 0, 0); ***/
  423.           }
  424.           }
  425.                   break;
  426.         default:
  427.                   break;
  428.           }
  429.         }
  430.             break;
  431.           case ConfigureNotify:
  432.             width = report.xconfigure.width;
  433.             height = report.xconfigure.height;
  434.             break;
  435.           case KeyPress:
  436.             XUnloadFont(display, font_info->fid);
  437.             XFreeGC(display,gc);
  438.             XCloseDisplay(display);
  439.             exit(1);
  440.           default:
  441.             break;
  442.     }
  443.     }
  444. }
  445.  
  446.