home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / fj / maillis / xwindow / 18895 < prev    next >
Encoding:
Internet Message Format  |  1992-12-24  |  4.9 KB

  1. Path: sparky!uunet!stanford.edu!sun-barr!sh.wide!wnoc-tyo-news!scslwide!wsgw!wsservra!onoe
  2. From: dwb@IMD.Sterling.COM (David Boyd)
  3. Newsgroups: fj.mail-lists.x-window
  4. Subject: Re: An Open Challenge (it can't be done!)
  5. Message-ID: <1992Dec24.193915.15957@sm.sony.co.jp>
  6. Date: 24 Dec 92 19:39:15 GMT
  7. Sender: onoe@sm.sony.co.jp (Atsushi Onoe)
  8. Distribution: fj
  9. Organization: Sterling Software
  10. Lines: 138
  11. Approved: michael@sm.sony.co.jp
  12.  
  13. Date: Wed, 23 Dec 1992 23:32:56 GMT
  14. Message-Id: <1992Dec23.233256.2178@sparky.imd.sterling.com>
  15. Newsgroups: comp.windows.x
  16. References: <78716@hydra.gatech.EDU>
  17. Sender: xpert-request@expo.lcs.mit.edu
  18.  
  19. In article <78716@hydra.gatech.EDU>, gt4417a@prism.gatech.EDU (SKELTON,JOEL
  20. PHILLIP) writes:
  21. |> This is okay if the image is
  22. |> all one color but for a complex satellite image or a noisy image 
  23. |> an xor'ed line is nearly invisible. 
  24.     We have fought the same problem in doing rubber banding over images and
  25. digitized maps.
  26. |> 
  27. |> Here's the short form:
  28. |>  - 8 bit image in backing pixmap used to update window
  29. |>  - Image drawn in 8 bit X window
  30. |>  - Want to draw diagonal lines on top of image that can
  31. |>    be removed quickly (i.e. replace them with the data they so
  32. |>    recently covered)
  33. |>  - Can't do server extensions (too many platforms to support)
  34.     To get around the visibility problem with XOR mode I use the
  35. following function to get the pixel value for foreground which will when
  36. XORed with the image pixels have the greatest contrast with most number of
  37. image pixel values.  The code as provided, has a delay at start up do
  38. read back the XImage from our source window.  If your background window
  39. changes infrequently then the overhead of retrieving the XImage may be 
  40. acceptable.  This code will also work fairly well even if you do not
  41. take into account the frequency of the pixel values in your image.  This 
  42. code can be speeded up quite a bit depending on what information you may
  43. already have accessable in your program.  This could also be combined with
  44. Oliver Jones suggestion about drawning/redrawing dashed lines.  Here the code:
  45. /*++ _GetBestXorColor 
  46.  *
  47.  *   &PURPOSE:
  48.  *
  49.  *   Scans the colormap for a window a computes the best color for
  50.  *   the XORed rubber band line.
  51.  *
  52.  *   &PROCESSING:
  53.  *    
  54.  *   Gets the window attributes to identify the colormap.  Querys the
  55.  *   color map for the allocated colors and computes the best pixel
  56.  *   value for the Xor rubberband line.
  57.  *
  58.  *   &PARAMETERS:
  59.  *
  60.  *   w        Widget        input        window to perform 
  61.  *                        rubberbanding in.    
  62.  *   
  63.  *   fg_color   unsigned long *  output          foreground color for GC
  64.  *
  65.  *   bg_color   unsigned long *  output          background color for GC
  66.  *
  67. --*/
  68. static void _GetBestXorColor(w,fg_color, bg_color )
  69. Widget w;
  70. unsigned long *fg_color;
  71. unsigned long *bg_color;
  72. {
  73. XWindowAttributes win_atts;
  74. Colormap my_colormap;
  75. int    max_cells = CellsOfScreen(XtScreen(w));
  76. XColor *color_cells = (XColor *)calloc(max_cells,sizeof(XColor));
  77. int    *num_colors = (int *)calloc(max_cells,sizeof(XColor));
  78. unsigned long best_pick_fg; 
  79. double rgb_diff, best_diff;
  80. int i,j,k;
  81. XImage *win_image;
  82.  
  83.    SL_PUSH("_GetBestXorColor");
  84.  
  85.    XGetWindowAttributes(XtDisplay(w), XtWindow(w), &win_atts);
  86.    if (win_atts.colormap == None)
  87.       my_colormap = DefaultColormapOfScreen(XtScreen(w));
  88.    else
  89.       my_colormap = win_atts.colormap;
  90.  
  91.    for (i=0; i<max_cells; i++)
  92.        color_cells[i].pixel = i;
  93.  
  94.    XQueryColors( XtDisplay(w), my_colormap, color_cells, max_cells);
  95.  
  96.    /* Now find out which colors are used in the window */
  97.    win_image = XGetImage(XtDisplay(w), XtWindow(w), 0, 0,
  98.         win_atts.width, win_atts.height, AllPlanes, ZPixmap);
  99.  
  100.    for (j=0; j<win_atts.width; j++)
  101.        for (k=0; k<win_atts.height; k++)
  102.            {
  103.            unsigned long a_pixel = XGetPixel(win_image, j, k);
  104.        num_colors[a_pixel] ++;
  105.            };
  106.  
  107.    XDestroyImage(win_image);
  108.  
  109.    best_diff = 0.0;
  110.  
  111.    for (i=0; i<max_cells; i++)
  112.        {
  113.        rgb_diff = 0.0;
  114.        for (j=0; j<max_cells; j++)
  115.            {
  116.            unsigned int tmp;
  117.            if (num_colors[j])
  118.               {
  119.               tmp = (i ^ j) % max_cells;
  120.               rgb_diff += 1.0 * (abs (((long)color_cells[tmp].red) -
  121.                           ((long)color_cells[j].red)) +
  122.                 abs (((long)color_cells[tmp].green) -
  123.                           ((long)color_cells[j].green)) +
  124.            abs (((long)color_cells[tmp].blue) -
  125.                           ((long)color_cells[j].blue))) * num_colors[j];
  126.                }
  127.            }
  128.         
  129.         if (rgb_diff > best_diff)
  130.            {
  131.            best_diff = rgb_diff;
  132.            best_pick_fg = i;
  133.            }
  134.         }
  135.  
  136.    *fg_color = best_pick_fg;
  137.    *bg_color = best_pick_fg ^ win_atts.backing_pixel;
  138.  
  139.    free(color_cells);
  140.    free(num_colors);
  141.  
  142.    SL_POPVOID(); 
  143. }
  144.  
  145.  
  146. -- 
  147. David W. Boyd                 UUCP:     uunet!sparky!dwb
  148. Sterling Software IMD        INTERNET: dwb@IMD.Sterling.COM
  149. 1404 Ft. Crook Rd. South     Phone:    (402) 291-8300 
  150. Bellevue, NE. 68005-2969     FAX:      (402) 291-4362
  151.