home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / x11 / lib / visual.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-05  |  7.2 KB  |  249 lines

  1. /*
  2. %    VISUAL . C
  3. %
  4. %    extracted from x11_stuff.c for public usage.
  5. */
  6.  
  7. #include "panel.h"
  8.  
  9. int    log2_levels;
  10.  
  11.  
  12. static    CONST_DECL char*
  13. visual_class_to_string(visual_type)
  14. int    visual_type;
  15. {
  16. CONST_DECL char *type_string;
  17.  
  18. switch (visual_type) {
  19.     case DirectColor:    type_string = "DirectColor";    break;
  20.     case PseudoColor:    type_string = "PseudoColor";    break;
  21.     case TrueColor:        type_string = "TrueColor";    break;
  22.     case StaticColor:    type_string = "StaticColor";    break;
  23.     case GrayScale:        type_string = "GrayScale";    break;
  24.     case StaticGray:    type_string = "StaticGray";    break;
  25.     default:         type_string = "any/unknown";    break;
  26.     }
  27. return type_string;
  28. }
  29.  
  30. void
  31. get_x_colormap(img)
  32. image_information *img;
  33. {
  34. img->colormap = XCreateColormap(img->dpy, Root_window, img->dpy_visual, AllocNone);
  35. if (img->colormap == NULL)
  36.     mesg("Could not create color map for visual\n");
  37.  
  38. VPRINTF(stderr, "created colormap for visual type %s\n",
  39.     visual_class_to_string(img->visual_class));
  40. }
  41.  
  42. #define BINARY_TABLE_INDEX    0
  43. #define MONOCHROME_TABLE_INDEX    1
  44. #define COLOR_TABLE_INDEX    2
  45.  
  46. /* Here we ALWAYS want to take a PseudoColor over the next best visual... */
  47. /* in Monochrome, we can do just as much with a pseudo color visual, and   */
  48. /* we may be able to preserve some of the default colors from other windows*/
  49. /* In color mode, some eight bit displays offer a Direct and TrueColor     */
  50. /* visual..   This is quite confusing, Have you ever seen a 24bit display  */
  51. /* offer a 24-bit PseudoColor visual?  They offer 8 bit PC visuals usually */
  52.  
  53. static int    desired_class_table[][6] = {
  54. { PseudoColor, StaticGray, GrayScale, DirectColor, TrueColor, StaticColor},
  55. { PseudoColor, GrayScale, StaticGray, DirectColor, TrueColor, StaticColor},
  56. { PseudoColor, DirectColor, TrueColor, StaticColor, GrayScale, StaticGray}
  57. };
  58.  
  59.  
  60. void
  61. find_appropriate_visual(img)
  62. register image_information *img;
  63. {
  64. static int        num_visuals = 0;
  65. static XVisualInfo    *visual_info = NULL;
  66. register XVisualInfo    *vi, *found_vi;
  67. XVisualInfo    *XGetVisualInfo();
  68. VisualID    def_visual_id;
  69. int        def_visual_index,
  70.         def_scrn = DefaultScreen(img->dpy),
  71.         desired_class, desired_depth,
  72.         deepest_visual, depth_delta;
  73. register int    i;
  74.     
  75.     DPRINTF(stderr, "In find_appropriate_visual(%d)\n", img->img_channels);
  76.  
  77.     if (visual_info == NULL) {
  78.     visual_info = XGetVisualInfo(img->dpy, VisualNoMask, NULL, &num_visuals);
  79.     if (visual_info == NULL || num_visuals == 0)
  80.         prgmerr(1, "XGetVisualInfo failed\n");
  81.     }
  82.  
  83.  
  84.     for (depth_delta=desired_depth=i=1; (i<<=1) < img->lvls; desired_depth++);
  85.  
  86.     if (img->mono_img && img->lvls == 2)
  87.     img->binary_img = True;
  88.  
  89.     if (img->binary_img) {
  90.     desired_class = BINARY_TABLE_INDEX;
  91.     desired_depth = 1;
  92.     }
  93.     else if (img->mono_img)
  94.     desired_class = MONOCHROME_TABLE_INDEX;
  95.     else {
  96.     desired_class = COLOR_TABLE_INDEX;    /* for default also */
  97.     desired_depth *= (depth_delta = 3);    /* needed if separate colors */
  98.     }
  99.     i = XDefaultDepth(img->dpy, def_scrn);    /* important modify */
  100.     if (desired_depth > i)
  101.     desired_depth = i;
  102.  
  103.     VPRINTF(stderr, "Searching for %s visual with desired depth >= %d\n",
  104.          visual_class_to_string(img->visual_class), desired_depth);
  105.  
  106.     /*
  107.     * find visual such that:
  108.     *
  109.     * 1. depth is as large as possible (up to true desired depth)
  110.     * 2. visual depth is the smallest of those supported >= depth
  111.     * 3. visual class is the `most desired' for the image type
  112.     *    Meaning! that if it is the DefaultVisual it IS the
  113.     *    most desired.  We can't choose one visual class over
  114.     *    another for all displays!  If the depth of the DefaultVisual
  115.     *    is large enough for the image, or as large as all others, we
  116.     *    use it!  This minimizes screw ups on our part.
  117.     */
  118.  
  119.     def_visual_id = DefaultVisual(img->dpy, def_scrn)->visualid;
  120.     found_vi = 0;
  121.  
  122.     for (i=deepest_visual=0; i < num_visuals; i++) {
  123.     if (deepest_visual < visual_info[i].depth)
  124.         deepest_visual = visual_info[i].depth;
  125.  
  126.     if (def_visual_id == visual_info[i].visualid)
  127.         def_visual_index = i;
  128.     }
  129.     
  130.     /* Take the Default Visual if it's cool enough... */
  131.     if (visual_info[def_visual_index].depth >= desired_depth ||
  132.     visual_info[def_visual_index].depth == deepest_visual)
  133.     found_vi = &visual_info[def_visual_index];
  134.  
  135.     /* if we were told to look for a specific type first, do it */
  136.     if (img->visual_class >= 0) {
  137.     int depth;
  138.     for (depth = desired_depth; depth >= 1; depth -= depth_delta) {
  139.         for (vi = visual_info; vi < visual_info + num_visuals; vi++)
  140.         if (vi->class == img->visual_class && vi->depth >= depth &&
  141.             vi->screen == def_scrn) {
  142.             found_vi = vi;
  143.             if (found_vi->depth == depth)    break;
  144.         }
  145.         if (found_vi != NULL && found_vi->class == img->visual_class)
  146.         break;
  147.     }
  148.     }
  149.     /* it == i = XMatchVisual(img->dpy, Screen, desired_depth, found_vi); */
  150.     if (img->visual_class < 0) {
  151.     int depth;
  152.     for (depth = desired_depth; depth >= 0; depth -= depth_delta) {
  153.         if (found_vi != NULL)
  154.         break;
  155.         for (i = 0; i < 6; i++) {    /* search for class and depth */
  156.         int    vt = desired_class_table[desired_class][i];
  157.  
  158.         for (vi = visual_info; vi < visual_info+num_visuals; vi++)
  159.             if (vi->class == vt && vi->depth >= depth &&
  160.             vi->screen == def_scrn) {
  161.             if (found_vi==NULL || found_vi->depth > vi->depth)
  162.                 found_vi = vi;
  163.             if (found_vi->depth == depth)
  164.                 break;
  165.             }
  166.         }
  167.     }
  168.     }
  169.  
  170.     if (!found_vi)
  171.     prgmerr(1, "Could not find appropriate visual type - %s\n",
  172.         visual_class_to_string(img->visual_class));
  173.  
  174.     /* set program the_hdr */
  175.     Screen = found_vi->screen;
  176.     Root_window = RootWindow(img->dpy, Screen);
  177.  
  178.     /* set img variables */
  179.     img->dpy_depth = found_vi->depth;
  180.     img->dpy_visual = found_vi->visual;
  181.     img->visual_class = found_vi->class;
  182.  
  183.     /* We want to give the DefaultColormap a shot first */
  184.     if (found_vi->visualid == def_visual_id)
  185.     img->colormap = DefaultColormap(img->dpy, Screen);
  186.     else
  187.     get_x_colormap(img);
  188.  
  189.     if (img->dpy_depth == 1 || img->binary_img) {
  190.     img->binary_img = img->mono_img = img->dither_img = True;
  191.     img->color_dpy = img->sep_colors = img->rw_cmap = False;
  192.     img->dpy_channels = log2_levels = 1;
  193.  
  194.     img->lvls = 2;
  195.     img->lvls_squared = 4;
  196.     }
  197.     else {
  198.     int class = img->visual_class;
  199.  
  200.     if (class == GrayScale || class == StaticGray) {
  201.         img->mono_img = True;
  202.         img->color_dpy = False;
  203.         img->dpy_channels = depth_delta = 1;
  204.     }
  205.  
  206.     img->rw_cmap = (class == PseudoColor || class == GrayScale);
  207.  
  208.     if (img->dpy_visual != DefaultVisual(img->dpy, Screen) &&
  209.         found_vi->visualid == def_visual_id)
  210.         img->colormap = XCreateColormap(img->dpy, Root_window,
  211.                     img->dpy_visual, AllocNone);
  212.  
  213.     if (class == StaticColor || class == DirectColor || class == TrueColor) {
  214.         img->sep_colors = True;
  215.  
  216.         i = 1 << (img->dpy_depth / depth_delta);
  217.  
  218.         if (img->lvls > i) {
  219.         img->lvls = i;
  220.         img->lvls_squared = i * i;
  221.         }
  222.     } else {
  223.         img->sep_colors = False;
  224.  
  225.         /* Image is monochrome ??? */
  226.         if (depth_delta == 1) {
  227.         i = found_vi->colormap_size;
  228.         }
  229.         else { /* color */
  230.         for (i=1; i * i * i < found_vi->colormap_size; i++) {}
  231.         i--;
  232.         }
  233.  
  234.         if (img->lvls > i) {
  235.         img->lvls = i;
  236.         img->lvls_squared = i * i;
  237.         }
  238.     }
  239.  
  240.     for (log2_levels = 1, i = 2; i < img->lvls; i <<= 1, log2_levels++);
  241.     /* do nothing */
  242.     }
  243.  
  244.     if (verbose)
  245.     message("Visual type %s, depth %d, screen %d\n",
  246.         visual_class_to_string(img->visual_class), img->dpy_depth, Screen),
  247.     message("levels: %d, log(2) levels: %d\n", img->lvls, log2_levels);
  248. }
  249.