home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xcms_SetGetCols.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  6KB  |  203 lines

  1. /* $XConsortium: SetGetCols.c,v 1.5 91/05/13 23:28:55 rws Exp $" */
  2.  
  3. /*
  4.  * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
  5.  *     All Rights Reserved
  6.  * 
  7.  * This file is a component of an X Window System-specific implementation
  8.  * of Xcms based on the TekColor Color Management System.  Permission is
  9.  * hereby granted to use, copy, modify, sell, and otherwise distribute this
  10.  * software and its documentation for any purpose and without fee, provided
  11.  * that this copyright, permission, and disclaimer notice is reproduced in
  12.  * all copies of this software and in supporting documentation.  TekColor
  13.  * is a trademark of Tektronix, Inc.
  14.  * 
  15.  * Tektronix makes no representation about the suitability of this software
  16.  * for any purpose.  It is provided "as is" and with all faults.
  17.  * 
  18.  * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
  19.  * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  20.  * PARTICULAR PURPOSE.  IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
  21.  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  22.  * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
  23.  * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  24.  * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  *
  27.  *    NAME
  28.  *        XcmsSetGet.c
  29.  *
  30.  *    DESCRIPTION
  31.  *        Source for _XcmsSetGetColors()
  32.  *
  33.  *
  34.  */
  35.  
  36. /*
  37.  *      EXTERNAL INCLUDES
  38.  *              Include files that must be exported to any package or
  39.  *              program using this package.
  40.  */
  41. #include "Xlib_private.h"
  42. #include "Xcmsint.h"
  43.  
  44.  
  45. /*
  46.  *      EXTERNS
  47.  */
  48.  
  49. extern void _XcmsRGB_to_XColor();
  50. extern void _XColor_to_XcmsRGB();
  51.  
  52.  
  53.  
  54. /************************************************************************
  55.  *                                    *
  56.  *            API PRIVATE ROUTINES                *
  57.  *                                    *
  58.  ************************************************************************/
  59.  
  60. /*
  61.  *    NAME
  62.  *        XcmsSetColors - 
  63.  *
  64.  *    SYNOPSIS
  65.  */
  66. Status
  67. _XcmsSetGetColors(xColorProc, dpy, cmap, pColors_in_out, nColors,
  68.     result_format, pCompressed)
  69.     Status (*xColorProc)();
  70.     Display *dpy;
  71.     Colormap cmap;
  72.     XcmsColor *pColors_in_out;
  73.     unsigned int nColors;
  74.     XcmsColorFormat result_format;
  75.     Bool *pCompressed;
  76. /*
  77.  *    DESCRIPTION
  78.  *        Routine containing code common to:
  79.  *            XcmsAllocColor
  80.  *            XcmsQueryColor
  81.  *            XcmsQueryColors
  82.  *            XcmsStoreColor
  83.  *            XcmsStoreColors
  84.  *
  85.  *    RETURNS
  86.  *        XcmsFailure if failed;
  87.  *        XcmsSuccess if it succeeded without gamut compression;
  88.  *        XcmsSuccessWithCompression if it succeeded with gamut
  89.  *            compression;
  90.  */
  91. {
  92.     DBUG_ENTER("_XcmsSetGetColors")
  93.     XcmsCCC ccc;
  94.     XColor *pXColors_in_out;
  95.     Status retval = XcmsSuccess;
  96.  
  97.     /*
  98.      * Argument Checking
  99.      *    1. Assume xColorProc is correct
  100.      *    2. Insure ccc not NULL
  101.      *    3. Assume cmap correct (should be checked by Server)
  102.      *    4. Insure pColors_in_out valid
  103.      *    5. Assume method_in is valid (should be checked by Server)
  104.      *    6. Insure nColors > 0
  105.      */
  106.  
  107.     if (dpy == NULL) {
  108.     DBUG_RETURN(XcmsFailure);
  109.     }
  110.  
  111.     if (nColors == 0) {
  112.     DBUG_RETURN(XcmsSuccess);
  113.     }
  114.  
  115.     if (result_format == XcmsUndefinedFormat) {
  116.     DBUG_RETURN(XcmsFailure);
  117.     }
  118.  
  119.     if (!((*xColorProc == XAllocColor) || (*xColorProc == XStoreColor)
  120.         || (*xColorProc == XStoreColors) || (*xColorProc == XQueryColor) 
  121.         || (*xColorProc == XQueryColors))) {
  122.     DBUG_RETURN(XcmsFailure);
  123.     }
  124.  
  125.     if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) {
  126.     DBUG_RETURN(XcmsFailure);
  127.     }
  128.  
  129.     if ((*xColorProc == XAllocColor) || (*xColorProc == XStoreColor)
  130.         || (*xColorProc == XQueryColor)) {
  131.     nColors = 1;
  132.     }
  133.  
  134.     /*
  135.      * Allocate space for XColors
  136.      */
  137.     if ((pXColors_in_out = (XColor *)Xcalloc(nColors, sizeof(XColor))) ==
  138.         NULL) {
  139.     DBUG_RETURN(XcmsFailure);
  140.     }
  141.  
  142.     if ((*xColorProc == XQueryColor) || (*xColorProc == XQueryColors)) {
  143.     goto Query;
  144.     }
  145.     /*
  146.      * Convert to RGB, adjusting for white point differences if necessary.
  147.      */
  148.     if ((retval = XcmsConvertColors(ccc, pColors_in_out, nColors, XcmsRGBFormat,
  149.         pCompressed)) == XcmsFailure) {
  150.     DBUG_RETURN(XcmsFailure);
  151.     }
  152.  
  153. Query:
  154.     /*
  155.      * Convert XcmsColor to XColor structures
  156.      */
  157.     _XcmsRGB_to_XColor(pColors_in_out, pXColors_in_out, nColors);
  158.  
  159.     /*
  160.      * Now make appropriate X Call
  161.      */
  162.     if (*xColorProc == XAllocColor) {
  163.     if ((*xColorProc)(ccc->dpy, cmap, pXColors_in_out) == 0) {
  164.         Xfree((char *)pXColors_in_out);
  165.         DBUG_RETURN(XcmsFailure);
  166.     }
  167.     } else if ((*xColorProc == XQueryColor) || (*xColorProc == XStoreColor)) {
  168.     /* Note: XQueryColor and XStoreColor do not return any Status */
  169.     (*xColorProc)(ccc->dpy, cmap, pXColors_in_out);
  170.     } else if ((*xColorProc == XQueryColors) || (*xColorProc == XStoreColors)){
  171.     /* Note: XQueryColors and XStoreColors do not return any Status */
  172.     (*xColorProc)(ccc->dpy, cmap, pXColors_in_out, nColors);
  173.     } else {
  174.     Xfree((char *)pXColors_in_out);
  175.     DBUG_RETURN(XcmsFailure);
  176.     }
  177.  
  178.     if ((*xColorProc == XStoreColor) || (*xColorProc == XStoreColors)) {
  179.     Xfree((char *)pXColors_in_out);
  180.     DBUG_RETURN(retval);
  181.     }
  182.  
  183.     /*
  184.      * Now, convert returned XColor(i.e., rgb) to XcmsColor structures
  185.      */
  186.     _XColor_to_XcmsRGB(ccc, pXColors_in_out, pColors_in_out, nColors);
  187.     Xfree((char *)pXColors_in_out);
  188.  
  189.     /*
  190.      * Then, convert XcmsColor structures to the original specification
  191.      *    format.  Note that we must use NULL instead of passing
  192.      *    pCompressed.
  193.      */
  194.  
  195.     if (result_format != XcmsRGBFormat) {
  196.     if (XcmsConvertColors(ccc, pColors_in_out, nColors, result_format,
  197.         (Bool *) NULL) == XcmsFailure) {
  198.         DBUG_RETURN(XcmsFailure);
  199.     }
  200.     }
  201.     DBUG_RETURN(retval);
  202. }
  203.