StdCmap.c

Go to the documentation of this file.
00001 /* $Xorg: StdCmap.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
00002 
00003 /* 
00004 
00005 Copyright 1989, 1998  The Open Group
00006 
00007 Permission to use, copy, modify, distribute, and sell this software and its
00008 documentation for any purpose is hereby granted without fee, provided that
00009 the above copyright notice appear in all copies and that both that
00010 copyright notice and this permission notice appear in supporting
00011 documentation.
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 
00023 Except as contained in this notice, the name of The Open Group shall not be
00024 used in advertising or otherwise to promote the sale, use or other dealings
00025 in this Software without prior written authorization from The Open Group.
00026 
00027 */
00028 /* $XFree86: xc/lib/Xmu/StdCmap.c,v 1.5 2001/01/17 19:42:56 dawes Exp $ */
00029 
00030 /*
00031  * Author:  Donna Converse, MIT X Consortium
00032  */
00033 
00034 #include <stdio.h>
00035 #include <X11/Xlib.h>
00036 #include <X11/Xatom.h>
00037 #include <X11/Xutil.h>
00038 #include <X11/Xmu/StdCmap.h>
00039 
00040 #define lowbit(x) ((x) & (~(x) + 1))
00041 
00042 /*
00043  * Prototypes
00044  */
00045 /* argument restrictions */
00046 static Status valid_args(XVisualInfo*, unsigned long, unsigned long,
00047                          unsigned long, Atom);
00048 
00049 /*
00050  * To create any one standard colormap, use XmuStandardColormap().
00051  *
00052  * Create a standard colormap for the given screen, visualid, and visual
00053  * depth, with the given red, green, and blue maximum values, with the
00054  * given standard property name.  Return a pointer to an XStandardColormap
00055  * structure which describes the newly created colormap, upon success.
00056  * Upon failure, return NULL.
00057  * 
00058  * XmuStandardColormap() calls XmuCreateColormap() to create the map.
00059  *
00060  * Resources created by this function are not made permanent; that is the
00061  * caller's responsibility.
00062  */
00063 
00064 XStandardColormap *
00065 XmuStandardColormap(Display *dpy, int screen, VisualID visualid,
00066                     unsigned int depth, Atom property, Colormap cmap,
00067                     unsigned long red_max, unsigned long green_max,
00068                     unsigned long blue_max)
00069      /*
00070       * dpy                             - specifies X server connection
00071       * screen                          - specifies display screen
00072       * visualid                        - identifies the visual type
00073       * depth                           - identifies the visual type
00074       * property                        - a standard colormap property
00075       * cmap                            - specifies colormap ID or None
00076       * red_max, green_max, blue_max    - allocations
00077       */
00078 {
00079     XStandardColormap   *stdcmap;
00080     Status              status;
00081     XVisualInfo         vinfo_template, *vinfo;
00082     long                vinfo_mask;
00083     int                 n;
00084 
00085     /* Match the required visual information to an actual visual */
00086     vinfo_template.visualid = visualid; 
00087     vinfo_template.screen = screen;
00088     vinfo_template.depth = depth;
00089     vinfo_mask = VisualIDMask | VisualScreenMask | VisualDepthMask;
00090     if ((vinfo = XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &n)) == NULL)
00091         return 0;
00092 
00093     /* Check the validity of the combination of visual characteristics,
00094      * allocation, and colormap property.  Create an XStandardColormap
00095      * structure.
00096      */
00097 
00098     if (! valid_args(vinfo, red_max, green_max, blue_max, property)
00099         || ((stdcmap = XAllocStandardColormap()) == NULL)) {
00100         XFree((char *) vinfo);
00101         return 0;
00102     }
00103 
00104     /* Fill in the XStandardColormap structure */
00105 
00106     if (cmap == DefaultColormap(dpy, screen)) {
00107         /* Allocating out of the default map, cannot use XFreeColormap() */
00108         Window win = XCreateWindow(dpy, RootWindow(dpy, screen), 1, 1, 1, 1,
00109                                    0, 0, InputOnly, vinfo->visual,
00110                                    (unsigned long) 0,
00111                                    (XSetWindowAttributes *)NULL);
00112         stdcmap->killid  = (XID) XCreatePixmap(dpy, win, 1, 1, depth);
00113         XDestroyWindow(dpy, win);
00114         stdcmap->colormap = cmap;
00115     } else {
00116         stdcmap->killid = ReleaseByFreeingColormap;
00117         stdcmap->colormap = XCreateColormap(dpy, RootWindow(dpy, screen),
00118                                             vinfo->visual, AllocNone);
00119     }
00120     stdcmap->red_max = red_max;
00121     stdcmap->green_max = green_max;
00122     stdcmap->blue_max = blue_max;
00123     if (property == XA_RGB_GRAY_MAP) 
00124         stdcmap->red_mult = stdcmap->green_mult = stdcmap->blue_mult = 1;
00125     else if (vinfo->class == TrueColor || vinfo->class == DirectColor) {
00126         stdcmap->red_mult = lowbit(vinfo->red_mask);
00127         stdcmap->green_mult = lowbit(vinfo->green_mask);
00128         stdcmap->blue_mult = lowbit(vinfo->blue_mask);
00129     } else {
00130         stdcmap->red_mult = (red_max > 0)
00131             ? (green_max + 1) * (blue_max + 1) : 0;
00132         stdcmap->green_mult = (green_max > 0) ? blue_max + 1 : 0;
00133         stdcmap->blue_mult = (blue_max > 0) ? 1 : 0;
00134     }
00135     stdcmap->base_pixel = 0;                    /* base pixel may change */
00136     stdcmap->visualid = vinfo->visualid;
00137 
00138     /* Make the colormap */
00139 
00140     status = XmuCreateColormap(dpy, stdcmap);
00141 
00142     /* Clean up */
00143 
00144     XFree((char *) vinfo);
00145     if (!status) {
00146 
00147         /* Free the colormap or the pixmap, if we created one */
00148         if (stdcmap->killid == ReleaseByFreeingColormap)
00149             XFreeColormap(dpy, stdcmap->colormap);
00150         else if (stdcmap->killid != None)
00151             XFreePixmap(dpy, stdcmap->killid);
00152         
00153         XFree((char *) stdcmap);
00154         return (XStandardColormap *) NULL;
00155     }
00156     return stdcmap;
00157 }
00158 
00159 /****************************************************************************/
00160 static Status
00161 valid_args(XVisualInfo *vinfo, unsigned long red_max, unsigned long green_max,
00162            unsigned long blue_max, Atom property)
00163      /*
00164       * vinfo                           - specifies visual
00165       * red_max, green_max, blue_max    - specifies alloc
00166       * property                        - specifies property name
00167       */
00168 {
00169     unsigned long       ncolors;        /* number of colors requested */
00170 
00171     /* Determine that the number of colors requested is <= map size */
00172 
00173     if ((vinfo->class == DirectColor) || (vinfo->class == TrueColor)) {
00174         unsigned long mask;
00175 
00176         mask = vinfo->red_mask;
00177         while (!(mask & 1))
00178             mask >>= 1;
00179         if (red_max > mask)
00180             return 0;
00181         mask = vinfo->green_mask;
00182         while (!(mask & 1))
00183             mask >>= 1;
00184         if (green_max > mask)
00185             return 0;
00186         mask = vinfo->blue_mask;
00187         while (!(mask & 1))
00188             mask >>= 1;
00189         if (blue_max > mask)
00190             return 0;
00191     } else if (property == XA_RGB_GRAY_MAP) {
00192         ncolors = red_max + green_max + blue_max + 1;
00193         if (ncolors > vinfo->colormap_size)
00194             return 0;
00195     } else {
00196         ncolors = (red_max + 1) * (green_max + 1) * (blue_max + 1);
00197         if (ncolors > vinfo->colormap_size)
00198             return 0;
00199     }
00200     
00201     /* Determine that the allocation and visual make sense for the property */
00202 
00203     switch (property)
00204     {
00205       case XA_RGB_DEFAULT_MAP:
00206         if (red_max == 0 || green_max == 0 || blue_max == 0)
00207             return 0;
00208         break;
00209       case XA_RGB_RED_MAP:
00210         if (red_max == 0)
00211             return 0;
00212         break;
00213       case XA_RGB_GREEN_MAP:
00214         if (green_max == 0)
00215             return 0;
00216         break;
00217       case XA_RGB_BLUE_MAP:     
00218         if (blue_max == 0)
00219             return 0;
00220         break;
00221       case XA_RGB_BEST_MAP:
00222         if (red_max == 0 || green_max == 0 || blue_max == 0)
00223             return 0;
00224         break;
00225       case XA_RGB_GRAY_MAP:
00226         if (red_max == 0 || blue_max == 0 || green_max == 0)
00227             return 0;
00228         break;
00229       default:
00230         return 0;
00231     }
00232     return 1;
00233 }

Generated on Sun Mar 4 15:03:56 2007 for Scilab [trunk] by  doxygen 1.5.1