home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / StdCmap.c.orig < prev    next >
Encoding:
Text File  |  1989-10-08  |  7.1 KB  |  212 lines

  1. /* $XConsortium: StdCmap.c,v 1.11 89/10/08 15:04:52 rws Exp $ 
  2.  * 
  3.  * Copyright 1989 by the Massachusetts Institute of Technology
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose and without fee is hereby granted, provided 
  7.  * that the above copyright notice appear in all copies and that both that 
  8.  * copyright notice and this permission notice appear in supporting 
  9.  * documentation, and that the name of M.I.T. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific, 
  11.  * written prior permission. M.I.T. makes no representations about the 
  12.  * suitability of this software for any purpose.  It is provided "as is"
  13.  * without express or implied warranty.
  14.  *
  15.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  17.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  20.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  * Author:  Donna Converse, MIT X Consortium
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <X11/Xlib.h>
  27. #include <X11/Xatom.h>
  28. #include <X11/Xutil.h>
  29. #include <X11/Xmu/StdCmap.h>
  30.  
  31. static Status valid_args();        /* argument restrictions */
  32.  
  33. /*
  34.  * To create any one standard colormap, use XmuStandardColormap().
  35.  *
  36.  * Create a standard colormap for the given screen, visualid, and visual
  37.  * depth, with the given red, green, and blue maximum values, with the
  38.  * given standard property name.  Return a pointer to an XStandardColormap
  39.  * structure which describes the newly created colormap, upon success.
  40.  * Upon failure, return NULL.
  41.  * 
  42.  * XmuStandardColormap() calls XmuCreateColormap() to create the map.
  43.  *
  44.  * Resources created by this function are not made permanent; that is the
  45.  * caller's responsibility.
  46.  */
  47.  
  48. XStandardColormap *XmuStandardColormap(dpy, screen, visualid, depth, property,
  49.                        cmap, red_max, green_max, blue_max)
  50.     Display        *dpy;        /* specifies X server connection */
  51.     int            screen;     /* specifies display screen */
  52.     VisualID        visualid;    /* identifies the visual type */
  53.     unsigned int    depth;        /* identifies the visual type */
  54.     Atom        property;    /* a standard colormap property */
  55.     Colormap        cmap;        /* specifies colormap ID or None */
  56.     unsigned long    red_max, green_max, blue_max;    /* allocations */
  57. {
  58.     XStandardColormap    *stdcmap;
  59.     Status        status;
  60.     XVisualInfo        vinfo_template, *vinfo;
  61.     long        vinfo_mask;
  62.     int            n;
  63.  
  64.     /* Match the required visual information to an actual visual */
  65.     vinfo_template.visualid = visualid;    
  66.     vinfo_template.screen = screen;
  67.     vinfo_template.depth = depth;
  68.     vinfo_mask = VisualIDMask | VisualScreenMask | VisualDepthMask;
  69.     if ((vinfo = XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &n)) == NULL)
  70.     return 0;
  71.  
  72.     /* Check the validity of the combination of visual characteristics,
  73.      * allocation, and colormap property.  Create an XStandardColormap
  74.      * structure.
  75.      */
  76.  
  77.     if (! valid_args(vinfo, red_max, green_max, blue_max, property)
  78.     || ((stdcmap = XAllocStandardColormap()) == NULL)) {
  79.     XFree((char *) vinfo);
  80.     return 0;
  81.     }
  82.  
  83.     /* Fill in the XStandardColormap structure */
  84.  
  85.     if (cmap == DefaultColormap(dpy, screen)) {
  86.     /* Allocating out of the default map, cannot use XFreeColormap() */
  87.     Window win = XCreateWindow(dpy, RootWindow(dpy, screen), 1, 1, 1, 1,
  88.                    0, 0, InputOnly, vinfo->visual,
  89.                    (unsigned long) 0,
  90.                    (XSetWindowAttributes *)NULL);
  91.     stdcmap->killid  = (XID) XCreatePixmap(dpy, win, 1, 1, depth);
  92.     XDestroyWindow(dpy, win);
  93.     stdcmap->colormap = cmap;
  94.     } else {
  95.     stdcmap->killid = ReleaseByFreeingColormap;
  96.     stdcmap->colormap = XCreateColormap(dpy, RootWindow(dpy, screen),
  97.                         vinfo->visual, AllocNone);
  98.     }
  99.     stdcmap->red_max = red_max;
  100.     stdcmap->green_max = green_max;
  101.     stdcmap->blue_max = blue_max;
  102.     if (property == XA_RGB_GRAY_MAP) 
  103.     stdcmap->red_mult = stdcmap->green_mult = stdcmap->blue_mult = 1;
  104.     else {
  105.     stdcmap->red_mult = (red_max > 0)
  106.         ? (green_max + 1) * (blue_max + 1) : 0;
  107.     stdcmap->green_mult = (green_max > 0) ? blue_max + 1 : 0;
  108.     stdcmap->blue_mult = (blue_max > 0) ? 1 : 0;
  109.     }
  110.     stdcmap->base_pixel = 0;            /* base pixel may change */
  111.     stdcmap->visualid = vinfo->visualid;
  112.  
  113.     /* Make the colormap */
  114.  
  115.     status = XmuCreateColormap(dpy, stdcmap);
  116.  
  117.     /* Clean up */
  118.  
  119.     XFree((char *) vinfo);
  120.     if (!status) {
  121.  
  122.     /* Free the colormap or the pixmap, if we created one */
  123.     if (stdcmap->killid == ReleaseByFreeingColormap)
  124.         XFreeColormap(dpy, stdcmap->colormap);
  125.     else if (stdcmap->killid != None)
  126.         XFreePixmap(dpy, stdcmap->killid);
  127.     
  128.     XFree((char *) stdcmap);
  129.     return (XStandardColormap *) NULL;
  130.     }
  131.     return stdcmap;
  132. }
  133.  
  134. /****************************************************************************/
  135. static Status valid_args(vinfo, red_max, green_max, blue_max, property)
  136.     XVisualInfo        *vinfo;        /* specifies visual */
  137.     unsigned long    red_max, green_max, blue_max;    /* specifies alloc */
  138.     Atom        property;    /* specifies property name */
  139. {
  140.     unsigned long    ncolors;    /* number of colors requested */
  141.  
  142.     /* Determine that the number of colors requested is <= map size */
  143.  
  144.     if ((vinfo->class == DirectColor) || (vinfo->class == TrueColor)) {
  145.     unsigned long mask;
  146.  
  147.     mask = vinfo->red_mask;
  148.     while (!(mask & 1))
  149.         mask >>= 1;
  150.     if (red_max > mask)
  151.         return 0;
  152.     mask = vinfo->green_mask;
  153.     while (!(mask & 1))
  154.         mask >>= 1;
  155.     if (green_max > mask)
  156.         return 0;
  157.     mask = vinfo->blue_mask;
  158.     while (!(mask & 1))
  159.         mask >>= 1;
  160.     if (blue_max > mask)
  161.         return 0;
  162.     } else if (property == XA_RGB_GRAY_MAP) {
  163.     ncolors = red_max + green_max + blue_max + 1;
  164.     if (ncolors > vinfo->colormap_size)
  165.         return 0;
  166.     } else {
  167.     ncolors = (red_max + 1) * (green_max + 1) * (blue_max + 1);
  168.     if (ncolors > vinfo->colormap_size)
  169.         return 0;
  170.     }
  171.     
  172.     /* Determine that the allocation and visual make sense for the property */
  173.  
  174.     switch (property)
  175.     {
  176.       case XA_RGB_DEFAULT_MAP:
  177.     if ((red_max == 0 || green_max == 0 || blue_max == 0) ||
  178.         (vinfo->class != PseudoColor && vinfo->class != DirectColor &&
  179.          vinfo->class != GrayScale))
  180.         return 0;
  181.     break;
  182.       case XA_RGB_RED_MAP:
  183.     if ((vinfo->class != PseudoColor && vinfo->class != DirectColor) ||
  184.         (red_max == 0))
  185.         return 0;
  186.     break;
  187.       case XA_RGB_GREEN_MAP:
  188.     if ((vinfo->class != PseudoColor && vinfo->class != DirectColor) ||
  189.         (green_max == 0))
  190.         return 0;
  191.     break;
  192.       case XA_RGB_BLUE_MAP:    
  193.     if ((vinfo->class != PseudoColor && vinfo->class != DirectColor) ||
  194.         blue_max == 0)
  195.         return 0;
  196.     break;
  197.       case XA_RGB_BEST_MAP:
  198.     if (vinfo->class == GrayScale || vinfo->class == StaticGray ||
  199.         red_max == 0 || green_max == 0 || blue_max == 0)
  200.         return 0;
  201.     break;
  202.       case XA_RGB_GRAY_MAP:
  203.     if (vinfo->class == StaticColor || vinfo->class == TrueColor ||
  204.         red_max == 0 || blue_max == 0 || green_max == 0)
  205.         return 0;
  206.     break;
  207.       default:
  208.     return 0;
  209.     }
  210.     return 1;
  211. }
  212.