[comp.windows.x] Patch to 'xmag'

bradley@xmos.cis.upenn.edu (John Bradley) (05/13/88)

Here's a patch to xmag that let's you click on pixels in the magnified 
window and get their pixel/r/g/b values.  Handy when debugging image processing
thingies, or at least it *should* be.

John Bradley  -  bradley@cis.upenn.edu

-----------------------(cut here)-------------------------
*** xmag.c.std	Thu May 12 16:06:24 1988
--- xmag.c	Thu May 12 18:46:30 1988
***************
*** 29,34 ****
--- 29,36 ----
  #define DEFAULT_CURSOR_SIZE 64
  #define DEFAULT_MAGNIFICATION 5
  
+ #define PIXFONT "8x13"
+ 
  int Argc;				/* copy of argc */
  char **Argv;				/* copy of argv */
  
***************
*** 433,439 ****
      attr.background_pixel = back_pixel;
      attr.border_pixel = border_pixel;
      attr.event_mask = (ExposureMask|ButtonPressMask|KeyPressMask);
!     attr.cursor = XCreateFontCursor (dpy, XC_top_left_arrow);
      valuemask = (CWBackPixel | CWBorderPixel | CWEventMask | CWCursor);
  
      w = XCreateWindow (dpy, root, 
--- 435,441 ----
      attr.background_pixel = back_pixel;
      attr.border_pixel = border_pixel;
      attr.event_mask = (ExposureMask|ButtonPressMask|KeyPressMask);
!     attr.cursor = XCreateFontCursor (dpy, XC_draft_small);
      valuemask = (CWBackPixel | CWBorderPixel | CWEventMask | CWCursor);
  
      w = XCreateWindow (dpy, root, 
***************
*** 608,614 ****
      if (!image) return (False);
  
      /*
!      * Map the window and do the work.  Button press or space means do another;
       * q, Q, or ^C mean quit.
       */
  
--- 610,617 ----
      if (!image) return (False);
  
      /*
!      * Map the window and do the work.  Space means do another;
!      * Button1 press displays pixel value.
       * q, Q, or ^C mean quit.
       */
  
***************
*** 617,623 ****
      XMapWindow (dpy, w);
      for (done = False; !done; ) {
  	XEvent event;
! 	int len;
  	char keybuffer[10];
  
  	XNextEvent (dpy, &event);
--- 620,629 ----
      XMapWindow (dpy, w);
      for (done = False; !done; ) {
  	XEvent event;
!         XButtonEvent *buttevent;
!         XColor def;
!         unsigned long pixel,lastpix;
! 	int len,first;
  	char keybuffer[10];
  
  	XNextEvent (dpy, &event);
***************
*** 626,634 ****
  	    repaint_image (image, &event, magnification, back_pixel);
  	    break;
  	  case ButtonPress:
! 	    domore = True;
! 	    done = True;
  	    break;
  	  case KeyPress:
  	    len = XLookupString (&event, keybuffer, sizeof keybuffer,
  				 NULL, NULL);
--- 632,673 ----
  	    repaint_image (image, &event, magnification, back_pixel);
  	    break;
  	  case ButtonPress:
!             buttevent = (XButtonEvent *) &event;
!             if (buttevent->button != Button1) break;   /* not Button1. fnord */
!             first=1;
!             if (buttevent->y/magnification > height/2) CreatePixWind(w,0);
!                                                   else CreatePixWind(w,1);
!             while (1) {     /* loop until button released */
!                 Window rootW,childW;
!                 int rx,ry,x,y,lx,ly;
!                 unsigned int mask;
!                 char str[64];
! 
!                 if (XQueryPointer(dpy,w,&rootW,&childW,&rx,&ry,&x,&y,&mask)) {
!                     if (!(mask & Button1Mask)) break;    /* button released */
!                     
!                     x=x/magnification;  y=y/magnification;
! 
!                     if ( (first || x!=lx || y!=ly) && 
!                          (x>=0 && x<width && y>=0 && y<height)) {  /* new pix */
!                         pixel = XGetPixel (image, x, y);
!                         
!                         if (pixel!=lastpix || first) {
!                             def.pixel = pixel;
!                             XQueryColor(dpy,cmap,&def);
!             
!                             sprintf(str,"Pixel: %3ld   (%04x,%04x,%04x)",
!                                     pixel, def.red, def.green, def.blue);
!                             DrawPixWind(str);
!                             }
!                         first=0;  lx=x;  ly=y;  lastpix=pixel;
!                         }
!                     }
!                 else break;
!                 }
!             ClosePixWind();
  	    break;
+ 
  	  case KeyPress:
  	    len = XLookupString (&event, keybuffer, sizeof keybuffer,
  				 NULL, NULL);
***************
*** 756,758 ****
--- 795,849 ----
      XFlush (dpy);
      return;
  }
+ 
+ 
+ static Window pixwind;
+ static XFontStruct *pixfinfo;
+ 
+ CreatePixWind(wind,bot)
+ Window wind;
+ int bot;
+ {
+ 
+     /* creates a small window inside of 'wind', at the top or bottom,
+        suitable for displaying one line of text.  Also loads in a font */
+ 
+     Window rootW;
+     int x,y;
+     unsigned int w,h,bwide,depth,thigh;
+ 
+     XGetGeometry(dpy,wind,&rootW,&x,&y,&w,&h,&bwide,&depth);
+ 
+     pixfinfo = XLoadQueryFont(dpy,PIXFONT);
+ 
+     if (pixfinfo==NULL) {
+         fprintf(stderr, "%s:  unable to load font '%s'\n",ProgramName,PIXFONT);
+         Exit(1);
+         }
+ 
+     XSetFont(dpy,fillGC,pixfinfo->fid);
+ 
+     thigh = pixfinfo->ascent + pixfinfo->descent + 4;
+     if (bot) y=h-thigh;  else y=0;
+ 
+     pixwind = XCreateSimpleWindow(dpy, wind, 0, y, w, thigh, 0,0,0);
+ 
+     XMapWindow(dpy,pixwind);
+ }
+ 
+ 
+ DrawPixWind(str)
+ char *str;
+ {
+     XClearWindow(dpy,pixwind);
+     XSetForeground(dpy,fillGC,1);
+     XDrawString(dpy,pixwind,fillGC,4,2+pixfinfo->ascent,str,strlen(str));
+ }
+ 
+ 
+ ClosePixWind()
+ {
+     XDestroyWindow(dpy,pixwind);
+     XFreeFont(dpy,pixfinfo);
+ }
+