home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / clrinit.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  7KB  |  236 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    clrinit.c (Color Initialize)
  6.  * Purpose:    Allocate the needed colors
  7.  * Subroutine:    init_color()            returns: void
  8.  * Xlib calls:    XGetVisualInfo(), XFree()
  9.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  10.  *        You may do anything you like with this file except remove
  11.  *        this copyright.  The Smithsonian Astrophysical Observatory
  12.  *        makes no representations about the suitability of this
  13.  *        software for any purpose.  It is provided "as is" without
  14.  *        express or implied warranty.
  15.  * Modified:    {0} Michael VanHilst    initial version           9 May 1989
  16.  *        {n} <who> -- <does what> -- <when>
  17.  */
  18.  
  19. #include <stdio.h>        /* stderr, NULL, etc. */
  20. #include <X11/Xlib.h>        /* X window stuff */
  21. #include <X11/Xutil.h>        /* X window manager stuff */
  22. #include "hfiles/color.h"    /* color structs */
  23. #include "hfiles/constant.h"    /* define codes */
  24. #include "hfiles/control.h"    /* to find verbose */
  25. #include "hfiles/define.h"    /* YES, NO, MIN, MAX and more */
  26. extern struct controlRec control;    /* decide how much to print */
  27.  
  28. #define MINPLANES 4
  29. #define MAXPLANES 8
  30.  
  31. static int screen;
  32. static XVisualInfo default_vinfo;    /* info about the default visual */
  33. static XVisualInfo private_vinfo;    /* info about the our own visual */
  34. static Display *display;
  35.  
  36. /*
  37.  * Subroutine:    init_color
  38.  * Purpose:    Claim and set up colors (and do any needed initial work)
  39.  */
  40. void init_color ( color, init )
  41.      struct colorRec *color;
  42.      int init;
  43. {
  44.   int alloc_colors();
  45.   void exit_errmsg(), init_hard_colors(), init_halftone();
  46.   void free_color_cells(), lookup_cursor_colors(), free_cursor_cell_color();
  47.   void init_overlay_color(), init_cell_color(), init_halftone_color();
  48.   static int init_visual();
  49.  
  50.   if( init ) {
  51.     display = color->display;
  52.     if( init_visual(color, MINPLANES) == 0 ) {
  53.       color->colormap_mode = VOP_Halftone;
  54.     } else if( color->colormap_mode != VOP_Halftone ) {
  55.       init_hard_colors(color, color->map.default_colormap);
  56.       lookup_cursor_colors(color, color->map.default_colormap, 1);
  57.     }
  58.     /* set pointers for halftone tools */
  59.     init_halftone();
  60.   } else if( color->colors_alloced ) {
  61.     /* free currently alloc'ed colors */
  62.     free_color_cells(color, color->colormap);
  63.     if( (color->old_mode != color->colormap_mode) &&
  64.         (color->colormap_mode == VOP_Cells) )
  65.       /* free read-only cursor colors if we think they won't be needed */
  66.       free_cursor_cell_color(color);
  67.   }
  68.   switch( color->colormap_mode ) {
  69.   case VOP_PseudoColor:
  70.   case VOP_StaticColor:
  71.     if( color->cells.overlay ) {
  72.       if( alloc_colors(color) ) {
  73.     init_overlay_color(color);
  74.     if( control.verbose )
  75.       (void)printf("%d color levels with overlay reserved\n",
  76.                color->ncolors);
  77.     color->old_mode = VOP_Overlay;
  78.     break;
  79.       }
  80.       color->cells.overlay = 0;
  81.     }
  82.     if( alloc_colors(color) ) {
  83.       init_cell_color(color);
  84.       if( control.verbose )
  85.     (void)printf("%d color levels reserved\n", color->ncolors);
  86.       color->old_mode = VOP_Cells;
  87.       break;
  88.     }
  89.     color->colormap_mode = VOP_Halftone;
  90.   case VOP_Halftone:
  91.     color->colormap = color->map.default_colormap;
  92.     color->cells.overlay = 0;
  93.     init_halftone_color(color);
  94.     color->old_mode = VOP_Halftone;
  95.     break;
  96.   default:
  97.     exit_errmsg("Unknown color mode");
  98.   }
  99.   color->cursor_overlay = color->cells.overlay;
  100. }
  101.  
  102. /*
  103.  * Subroutine:    init_visual
  104.  * Purpose:    Get basic info about the color hardware on hand
  105.  */
  106. static int init_visual ( color, mindepth )
  107.      struct colorRec *color;
  108.      int mindepth;        /* i: minimum planes needed for color */
  109. {
  110.   int verify_pseudocolor();
  111.   void exit_errmsg();
  112.  
  113.   display = color->display;
  114.   if( color->screen < 0 )
  115.     color->screen = DefaultScreen(display);
  116.   screen = color->screen;
  117.   color->screen_depth = DisplayPlanes(display, screen);
  118.   color->hard.std_white = WhitePixel(display, screen);
  119.   color->hard.std_black = BlackPixel(display, screen);
  120.   color->gcset.menu.foreground = color->hard.std_black;
  121.   color->gcset.menu.background = color->hard.std_white;
  122.   color->gcset.black.background = color->hard.std_white;
  123.   /* initially set visual to default visual */
  124.   color->visual = DefaultVisual(display, screen);
  125.   color->map.default_colormap = DefaultColormap(display, screen);
  126.   if( color->screen_depth < mindepth ) {
  127.     color->map.default_enable = NO;
  128.     color->map.private_enable = NO;
  129.     return( 0 );
  130.   } else {
  131.     XVisualInfo *vinfo;
  132.     int vinfo_cnt, vptr;
  133.  
  134.     color->map.default_vinfo = &default_vinfo;
  135.     default_vinfo.screen = screen;
  136.     default_vinfo.visual = color->visual;
  137. #ifdef XV11R2
  138.     default_vinfo.visualid = default_vinfo.visual->visualid;
  139. #else
  140.     default_vinfo.visualid = XVisualIDFromVisual(default_vinfo.visual);
  141. #endif
  142.     vinfo = XGetVisualInfo(display, VisualIDMask | VisualScreenMask,
  143.                &default_vinfo, &vinfo_cnt);
  144.     vptr = 0;
  145.     if( vinfo_cnt > 1 ) {
  146.       /* just take the first visual that matches */
  147.       while( (vptr < vinfo_cnt) &&
  148.          (vinfo[vptr].visual != default_vinfo.visual) )
  149.     vptr++;
  150.     }
  151.     if( vinfo_cnt <= vptr )
  152.       exit_errmsg("Default visual not found");
  153.     default_vinfo.depth = vinfo[vptr].depth;
  154.     default_vinfo.class = vinfo[vptr].class;
  155.     default_vinfo.colormap_size = vinfo[vptr].colormap_size;
  156.     XFree((char *)vinfo);
  157.     switch( default_vinfo.class ) {
  158.     case PseudoColor:
  159.       color->map.default_enable = YES;
  160.       return( 1 );
  161.     case StaticColor:
  162.     case DirectColor:
  163.     case TrueColor:
  164.     case GrayScale:
  165.     case StaticGray:
  166.     default:
  167.       if( color->colormap_mode == VOP_Halftone ) {
  168.     color->map.default_enable = YES;
  169.     color->map.default_permit = YES;
  170.     default_vinfo.depth = 1;
  171.     return( 0 );
  172.       } else {
  173.     color->map.default_enable = NO;
  174.     color->map.default_permit = NO;
  175.     if( verify_pseudocolor(color, 4, 16) ) {
  176.       color->map.private_enable = YES;
  177.       return( 1 );
  178.     } else {
  179.       color->map.private_enable = NO;
  180.       color->map.private_permit = NO;
  181.       return( 0 );
  182.     }
  183.       }
  184.     }
  185.   }
  186. }
  187.  
  188. /*
  189.  * Subroutine:    verify_pseudocolor
  190.  * Purpose:    Verify that server can provide a satisfactory colormap
  191.  * Returns:    1 if yes, else 0
  192.  * PostState:    Fills in visual info in passed vinfo
  193.  * Xlib calls:    XGetVisualInfo(), XFree()
  194.  */
  195. int verify_pseudocolor ( color, min_depth, min_size )
  196.      struct colorRec *color;
  197.      int min_depth;        /* i: minimum required depth */
  198.      int min_size;        /* i: minimum number of map cells */
  199. {
  200.   XVisualInfo *linfo;        /* l: list returned by XGetVisualInfo */
  201.   int cnt, i, ptr;
  202.  
  203.   private_vinfo.screen = color->screen;
  204.   private_vinfo.class = PseudoColor;
  205.   linfo = XGetVisualInfo(color->display, VisualClassMask | VisualScreenMask,
  206.              &private_vinfo, &cnt);
  207.   if( cnt == 0 ) {
  208.     color->map.private_enable = NO;
  209.     color->map.private_permit = NO;
  210.     return( 0 );
  211.   }
  212.   ptr = 0;
  213.   if( cnt > 1 ) {
  214.     for( i=0; i<cnt; i++ ) {
  215.       /* look for best we can do */
  216.       if( (linfo[i].depth >= linfo[ptr].depth) &&
  217.       (linfo[i].colormap_size >= linfo[ptr].colormap_size) )
  218.     ptr = i;
  219.     }
  220.   }
  221.   if( (linfo[ptr].depth < (unsigned int)min_depth) ||
  222.       (linfo[ptr].colormap_size < min_size) ) {
  223.     XFree((char *)linfo);
  224.     color->map.private_enable = NO;
  225.     color->map.private_permit = NO;
  226.     return( 0 );
  227.   } else {
  228.     bcopy((char *)(&linfo[ptr]), (char *)(&private_vinfo),
  229.       sizeof(XVisualInfo));
  230.     color->map.private_vinfo = &private_vinfo;
  231.     color->map.private_enable = YES;
  232.     XFree((char *)linfo);
  233.     return( 1 );
  234.   }
  235. }
  236.