home *** CD-ROM | disk | FTP | other *** search
/ vis-ftp.cs.umass.edu / vis-ftp.cs.umass.edu.tar / vis-ftp.cs.umass.edu / pub / Software / universal_plane_file_format / XShowPlane.c < prev   
C/C++ Source or Header  |  1992-01-29  |  11KB  |  400 lines

  1. /*
  2.  * ------------------------------------------------------------------
  3.  * XShowPlane.c - C code for displaying an LLVS plane.
  4.  * Created by Robert Heller on Fri Sep 13 13:01:17 1991
  5.  * ------------------------------------------------------------------
  6.  * Modification History:
  7.  * ------------------------------------------------------------------
  8.  * Contents:
  9.  *    OpenXDisplay        Opens the X display
  10.  *    CreateDisplayWindow    Create an X window for displaying images
  11.  *                and other stuff on.  Creates both an X
  12.  *                window and a background pixmap for it.
  13.  *    ShowPlane        Display an LLVS image on a X drawable
  14.  *                (drawable == window or pixmap)
  15.  *    
  16.  * ------------------------------------------------------------------
  17.  *  
  18.  * Copyright (c) 1991 by The University of Massachusetts
  19.  *     All Rights Reserved
  20.  * 
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <malloc.h>
  25. #include <X11/Xlib.h>
  26. #include <llvs_per_plane.h>
  27. #include <displaystructs.h>
  28.  
  29. /*
  30.  * OpenXDisplay() - open a connection to the X display server
  31.  *            returns a pointer to a Display object.
  32.  *
  33.  *    Arguments:  none.  (Uses DISPLAY environment variable)
  34.  *
  35.  *    Returns:    Display pointer (from XOpenDisplay()).
  36.  */
  37.  
  38. Display *OpenXDisplay()
  39. {
  40.     char *getenv();
  41.  
  42.     return(XOpenDisplay(getenv("DISPLAY")));
  43. }
  44.  
  45. /*
  46.  * Create an X window and a pixmap for its background (binding the
  47.  * pixmap as its background).
  48.  *
  49.  *    Arguments:    disp - Display* from OpenXDisplay(),
  50.  *            width, height - ints - size of display
  51.  *            title - char* - window banner string
  52.  */
  53.  
  54. WindowAndPixmap CreateDisplayWindow(disp,width,height,title)
  55. Display *disp;
  56. int width, height;
  57. char *title;
  58. {
  59.     WindowAndPixmap result;
  60.     Window root;
  61.     XWindowAttributes windattrs;
  62.     XSetWindowAttributes setwindattrs;
  63.     int screen_id;
  64.     Visual visual;
  65.  
  66.  
  67.     result = (WindowAndPixmap) malloc(sizeof(windowAndPixmap));
  68.     if (result == NULL) return(NULL);
  69.  
  70.     screen_id = DefaultScreen(disp);
  71.     visual.visualid = CopyFromParent;
  72.  
  73.     setwindattrs.backing_store = Always;
  74.     setwindattrs.event_mask = ButtonPressMask;
  75.     setwindattrs.bit_gravity = NorthWestGravity;
  76.     setwindattrs.win_gravity = NorthWestGravity;
  77.     
  78.     result->window = XCreateWindow(disp,RootWindow(disp,screen_id),
  79.                    300,200,width,height, 0,
  80.                    8, InputOutput,
  81.                    &visual,
  82.                    CWBitGravity|CWWinGravity|
  83.                    CWBackingStore|CWEventMask, &setwindattrs);
  84.     XStoreName(disp,result->window,title);
  85.     result->background = XCreatePixmap(disp,result->window,width,height,
  86.                        8);    
  87.     XSetWindowBackgroundPixmap(disp,result->window,result->background);
  88.     XGetWindowAttributes(disp,result->window,&windattrs);
  89.     result->colormap = windattrs.colormap;
  90.     XMapRaised(disp,result->window);
  91.     XSync(disp,False);
  92.     return(result);
  93. }
  94.  
  95. get_drawable_size(disp,drawable,width,height,depth)
  96. Display *disp;
  97. Drawable drawable;
  98. unsigned int *width, *height, *depth;
  99. {
  100.     Window root_return;
  101.     int x_return, y_return;
  102.     unsigned int bw_return;
  103.  
  104.     return(XGetGeometry(disp,drawable,&root_return,&x_return,&y_return,
  105.                 width,height,&bw_return,depth));
  106. }
  107.  
  108. ShowPlane(disp,drawable,plane,plane_info,limits,
  109.       autoscale,absolute,negative,minval,maxval,number_grey,colormap)
  110. Display *disp;
  111. Drawable drawable;
  112. PLANE *plane;
  113. PLANE_INFO *plane_info;
  114. LIMITS *limits;
  115. int autoscale,negative,absolute,number_grey;
  116. double minval,maxval;
  117. Colormap colormap;
  118. {
  119.     register XImage *ximage;
  120.     Screen *screen;
  121.     register unsigned char *imdata;
  122.     int i, ipixel;
  123.     int runlevel;
  124.     int deltapllevel;
  125.     int roff,coff;
  126.     int xim_y_coord, xim_x_coord;
  127.     int crow,ccol;
  128.     int byte, ipixrow, ipixcol;
  129.     double intensity_scale,value_range,pixel,scaled_pixel,fback;
  130.     unsigned int dw_width, dw_height, dw_depth;
  131.     unsigned int im_width, im_height;
  132.     int pixwidth, pixheight;
  133.     XColor color;
  134.     unsigned long int *pixmap;
  135.     double grey_level;
  136.     GC gc;
  137.  
  138.     if (autoscale) plane_min_max(plane,plane_info,limits,&minval,&maxval);
  139.     
  140.     runlevel = limits->level;
  141.  
  142.     if (plane_info->datatype == FLOAT) fback = plane_info->background.flonum;
  143.     else fback = plane_info->background.fixnum;
  144.  
  145.     deltapllevel = runlevel - plane_info->level;
  146.     value_range = maxval - minval;
  147.     intensity_scale = ((double) (number_grey - 1)) / value_range;
  148.  
  149.     get_drawable_size(disp,drawable,&dw_width,&dw_height,&dw_depth);
  150.     if (dw_depth != 8) {
  151.         printf("Display is not an 8-bit display!\n");
  152.         return(0);
  153.     }
  154.  
  155.     {int absecol, absscol, abserow, abssrow;
  156.      int absdrow, absdcol;
  157.  
  158.      if (limits->deltacol < 0) absdcol = -limits->deltacol;
  159.      else absdcol = limits->deltacol;
  160.      if (limits->deltarow < 0) absdrow = -limits->deltarow;
  161.      else absdrow = limits->deltarow;
  162.  
  163.      TRANSLEVEL(absecol,limits->endcol,deltapllevel,
  164.             plane_info->column_location);
  165.      TRANSLEVEL(absscol,limits->startcol,deltapllevel,
  166.             plane_info->column_location);
  167.      TRANSLEVEL(abserow,limits->endrow,deltapllevel,
  168.             plane_info->row_location);
  169.      TRANSLEVEL(abssrow,limits->startrow,deltapllevel,
  170.             plane_info->row_location);
  171.      im_width = (absecol - absscol) / absdcol;
  172.      im_height = (abserow - abssrow) / absdrow;
  173.     }
  174.  
  175.     if (im_width > dw_width || im_height > dw_height) {
  176.         printf("Display window too small for image!\n");
  177.         return(0);
  178.     }
  179.     pixmap = (unsigned long int *) calloc(number_grey,
  180.                           sizeof(unsigned long int));
  181.     if (pixmap == NULL) {
  182.         printf("Couldn't allocate pixmap\n");
  183.         return(0);
  184.     }
  185.     
  186.     for (ipixel = 0; ipixel < number_grey; ipixel++) {
  187.         grey_level = (ipixel / ((double) number_grey)) * WHITE_VALUE;
  188.         color.blue = color.green = color.red = grey_level;
  189.         color.flags = DoRed | DoGreen | DoBlue;
  190.         XAllocColor(disp,colormap,&color);
  191.         pixmap[ipixel] = color.pixel;
  192.     }
  193.         
  194.     pixwidth = dw_width / im_width;
  195.     pixheight = dw_height / im_height;
  196.     
  197.     imdata = (unsigned char *) calloc(dw_width * dw_height,
  198.                       sizeof(unsigned char));
  199.     if (imdata == NULL) {
  200.         printf("Acclocation failure!\n");
  201.         free(pixmap);
  202.         return(0);
  203.     }
  204.     screen = XDefaultScreenOfDisplay(disp);
  205.     ximage = XCreateImage(disp,XDefaultVisualOfScreen(screen),dw_depth,
  206.                   ZPixmap,0,imdata,dw_width,dw_height,8,0);
  207.  
  208.  
  209.     for (crow = limits->startrow;
  210.          crow <= limits->endrow;
  211.          crow += limits->deltarow) {
  212.  
  213.         TRANSLEVEL(roff,crow,deltapllevel,plane_info->row_location);
  214.         xim_y_coord = (crow - limits->startrow) * pixheight;
  215.  
  216.         for (ccol = limits->startcol;
  217.              ccol <= limits->endcol;
  218.              ccol += limits->deltacol) {
  219.  
  220.             TRANSLEVEL(coff,ccol,deltapllevel,plane_info->column_location);
  221.             xim_x_coord = (ccol - limits->startcol) * pixwidth;
  222.     
  223.             GET_PIXEL(pixel,fback,plane,roff,coff,
  224.                         (*plane_info));
  225.             if (absolute) pixel = fabs(pixel);
  226.         /* scale pixel.  out of range values get black or white.
  227.            otherwise linear mapping */
  228.             if (intensity_scale > 0.0) {
  229.             /* positive slope case */
  230.             if (pixel < minval)
  231.                 scaled_pixel = 0.0;
  232.             else
  233.                 if (pixel > maxval)
  234.                 scaled_pixel = number_grey-1;
  235.                 else
  236.                 scaled_pixel =(pixel - minval) *
  237.                     intensity_scale;
  238.             } else {    /* negative slope case */
  239.             if (pixel < maxval)
  240.                 scaled_pixel = number_grey-1;
  241.             else
  242.                 if (pixel > minval)
  243.                 scaled_pixel = 0.0;
  244.                 else
  245.                 scaled_pixel =(pixel - minval) *
  246.                     intensity_scale;
  247.             }
  248.             byte = scaled_pixel + 0.5;
  249.             if (negative) byte = (number_grey-1) - byte;
  250.             for (ipixrow = 0; ipixrow < pixheight; ipixrow++) {
  251.                 for (ipixcol = 0; ipixcol < pixwidth; ipixcol++) {
  252.                 XPutPixel(ximage,xim_x_coord+ipixcol,
  253.                       xim_y_coord+ipixrow,pixmap[byte]);
  254.             }
  255.             }
  256.              }
  257.     }
  258.     gc = XCreateGC(disp,drawable,0L,NULL);
  259.     XSetState(disp, gc, 0L, 255L, GXcopy, AllPlanes);
  260.     XSetLineAttributes(disp,gc,2,
  261.                LineSolid,CapNotLast,JoinMiter);
  262.     XPutImage(disp,drawable,gc,ximage,0,0,0,0,dw_width,dw_height);
  263.     XSync(disp,False);
  264.     free(pixmap);
  265.     /* XDestroyImage(ximage); */
  266.     /* XFreeGC(gc); */
  267.     /* free(imdata); */
  268.     return(1);
  269. }
  270.  
  271. static plane_min_max(plane,plane_info,limits,minval,maxval)
  272. PLANE *plane;
  273. PLANE_INFO *plane_info;
  274. LIMITS *limits;
  275. double *minval, *maxval;
  276. {
  277.     int runlevel;
  278.     int deltapllevel;
  279.     int roff,coff;
  280.     int crow,ccol;
  281.     int flag;
  282.     double pixel,fback;
  283.     
  284.     runlevel = limits->level;
  285.  
  286.     if (plane_info->datatype == FLOAT) fback = plane_info->background.flonum;
  287.     else fback = plane_info->background.fixnum;
  288.  
  289.     deltapllevel = runlevel - plane_info->level;
  290.  
  291.     flag = TRUE;
  292.  
  293.     for (crow = limits->startrow;
  294.          crow >= limits->endrow;
  295.          crow += limits->deltarow) {
  296.  
  297.         TRANSLEVEL(roff,crow,deltapllevel,plane_info->row_location);
  298.  
  299.         for (ccol = limits->startcol;
  300.              ccol <= limits->endcol;
  301.              ccol += limits->deltacol) {
  302.  
  303.             TRANSLEVEL(coff,ccol,deltapllevel,plane_info->column_location);
  304.     
  305.             GET_PIXEL(pixel,fback,plane,roff,coff,
  306.                         (*plane_info));
  307.  
  308.             if (flag) {
  309.                 *minval = pixel;
  310.             *maxval = pixel;
  311.             flag = FALSE;
  312.             } else if (pixel > *maxval) *maxval = pixel;
  313.             else if (pixel < *minval) *minval = pixel;
  314.          }
  315.      }
  316. }
  317.  
  318.  
  319. #ifdef TESTING
  320.  
  321. main(argc,argv)
  322. int argc;
  323. char *argv[];
  324. {
  325.     LIMITS pl_lims;
  326.     PLANE *inpl;
  327.     PLANE_INFO *inpl_info;
  328.     char *assoc;
  329.     char *infile;
  330.     register int iargc;
  331.     Display *disp;
  332.     WindowAndPixmap w_p, CreateDisplayWindow();
  333.     XEvent event;
  334.  
  335.     infile = NULL;
  336.     for (iargc = 1; iargc < argc; iargc++) {
  337.     if (*argv[iargc] != '-') {
  338.         if (infile != NULL) usage();
  339.         infile = argv[iargc];
  340.         }
  341.     }
  342.     if (infile == NULL) usage();
  343.     if (read_plane(&inpl,&inpl_info,&assoc,infile) < 0) {
  344.     perror(argv[0]);
  345.     exit(12);
  346.     }
  347.     pl_lims.level = inpl_info->level;
  348.     pl_lims.deltarow = 1;    
  349.     pl_lims.deltacol = 1;    
  350.     pl_lims.startrow = inpl_info->row_location;
  351.     pl_lims.startcol = inpl_info->column_location;
  352.     pl_lims.endrow   = inpl_info->row_location + inpl_info->row_dimension - 1;
  353.     pl_lims.endcol   = inpl_info->column_location +  inpl_info->column_dimension - 1;
  354.     for (iargc = 1; iargc < argc; iargc++) {
  355.     if (*argv[iargc] != '-') continue;
  356.     if (strncmp(argv[iargc],"-start-row=",11) == 0)
  357.         pl_lims.startrow = atoi(argv[iargc]+11);
  358.     else if (strncmp(argv[iargc],"-start-col=",11) == 0)
  359.         pl_lims.startcol = atoi(argv[iargc]+11);
  360.     else if (strncmp(argv[iargc],"-end-row=",9) == 0)
  361.         pl_lims.endrow = atoi(argv[iargc]+9);
  362.     else if (strncmp(argv[iargc],"-end-col=",9) == 0)
  363.         pl_lims.endcol = atoi(argv[iargc]+9);
  364.     else {
  365.         fprintf(stderr,"show_plane: unknown option: %s\n",argv[iargc]);
  366.         usage();
  367.         }
  368.     }
  369.     printf("Plane file: %s\nAssoc List: %s\nStart (%d,%d), End (%d,%d)\n\n",
  370.        infile,assoc,pl_lims.startrow,pl_lims.startcol,pl_lims.endrow,
  371.        pl_lims.endcol);
  372.     disp = OpenXDisplay();
  373.     if (disp == NULL) {
  374.     printf("Could not open display!\n");
  375.     exit(1);
  376.     }
  377.     w_p = CreateDisplayWindow(disp,512,512,"Show Plane");
  378.     ShowPlane(disp,w_p->background,inpl,inpl_info,&pl_lims,TRUE,FALSE,FALSE,0.0,0.0,
  379.           DEFAULT_NUMBER_GREY,w_p->colormap);
  380.     /* XSetWindowBackgroundPixmap(disp,w_p->window,w_p->background); */
  381.     XClearWindow(disp,w_p->window);
  382.     XSync(disp,False);
  383.     printf("Hit any key to stop:");
  384.     getchar();        
  385. }
  386.  
  387. static usage()
  388. {
  389.     fprintf(stderr,"SYNOPIS: show_plane <opts> plane_file <opts>\n");
  390.     fprintf(stderr,"This program displays a LLVS plane on an X Display\n\n");
  391.     fprintf(stderr,"Options are:\n");
  392.     fprintf(stderr,"-start-row=n\tRow to start printing\n");
  393.     fprintf(stderr,"-start-col=n\tColumn to start printing\n");
  394.     fprintf(stderr,"-end-row=n\tRow to end printing\n");
  395.     fprintf(stderr,"-end-col=n\tColumn to end printing\n");
  396.     exit(12);
  397.     }
  398.  
  399. #endif
  400.