SetGetCols.c revision 1ab64890
1/* $Xorg: SetGetCols.c,v 1.3 2000/08/17 19:44:54 cpqbld 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/* $XFree86$ */ 36 37/* 38 * EXTERNAL INCLUDES 39 * Include files that must be exported to any package or 40 * program using this package. 41 */ 42#ifdef HAVE_CONFIG_H 43#include <config.h> 44#endif 45#include "Xlibint.h" 46#include "Xcmsint.h" 47#include "Cv.h" 48 49 50/************************************************************************ 51 * * 52 * API PRIVATE ROUTINES * 53 * * 54 ************************************************************************/ 55 56/* 57 * NAME 58 * XcmsSetColor - 59 * 60 * SYNOPSIS 61 */ 62Status 63_XcmsSetGetColor( 64 Status (*xColorProc)( 65 Display* /* display */, 66 Colormap /* colormap */, 67 XColor* /* screen_in_out */), 68 Display *dpy, 69 Colormap cmap, 70 XcmsColor *pColors_in_out, 71 XcmsColorFormat result_format, 72 Bool *pCompressed) 73/* 74 * DESCRIPTION 75 * Routine containing code common to: 76 * XcmsAllocColor 77 * XcmsQueryColor 78 * XcmsStoreColor 79 * 80 * RETURNS 81 * XcmsFailure if failed; 82 * XcmsSuccess if it succeeded without gamut compression; 83 * XcmsSuccessWithCompression if it succeeded with gamut 84 * compression; 85 */ 86{ 87 XcmsCCC ccc; 88 XColor XColors_in_out; 89 Status retval = XcmsSuccess; 90 91 /* 92 * Argument Checking 93 * 1. Assume xColorProc is correct 94 * 2. Insure ccc not NULL 95 * 3. Assume cmap correct (should be checked by Server) 96 * 4. Insure pColors_in_out valid 97 * 5. Assume method_in is valid (should be checked by Server) 98 */ 99 100 if (dpy == NULL) { 101 return(XcmsFailure); 102 } 103 104 if (result_format == XcmsUndefinedFormat) { 105 return(XcmsFailure); 106 } 107 108 if ( !((*xColorProc == XAllocColor) || (*xColorProc == XStoreColor) 109 || (*xColorProc == XQueryColor)) ) { 110 return(XcmsFailure); 111 } 112 113 if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) { 114 return(XcmsFailure); 115 } 116 117 if (*xColorProc == XQueryColor) { 118 goto Query; 119 } 120 121 /* 122 * Convert to RGB, adjusting for white point differences if necessary. 123 */ 124 if ((retval = XcmsConvertColors(ccc, pColors_in_out, 1, XcmsRGBFormat, 125 pCompressed)) == XcmsFailure) { 126 return(XcmsFailure); 127 } 128 129Query: 130 /* 131 * Convert XcmsColor to XColor structures 132 */ 133 _XcmsRGB_to_XColor(pColors_in_out, &XColors_in_out, 1); 134 135 /* 136 * Now make appropriate X Call 137 */ 138 if (*xColorProc == XAllocColor) { 139 if ((*xColorProc)(ccc->dpy, cmap, &XColors_in_out) == 0) { 140 return(XcmsFailure); 141 } 142 } else if ((*xColorProc == XQueryColor) || (*xColorProc == XStoreColor)) { 143 /* Note: XQueryColor and XStoreColor do not return any Status */ 144 (*xColorProc)(ccc->dpy, cmap, &XColors_in_out); 145 } else { 146 return(XcmsFailure); 147 } 148 149 if ((*xColorProc == XStoreColor)) { 150 return(retval); 151 } 152 153 /* 154 * Now, convert the returned XColor (i.e., rgb) to XcmsColor structures 155 */ 156 _XColor_to_XcmsRGB(ccc, &XColors_in_out, pColors_in_out, 1); 157 158 /* 159 * Then, convert XcmsColor structures to the original specification 160 * format. Note that we must use NULL instead of passing 161 * pCompressed. 162 */ 163 164 if (result_format != XcmsRGBFormat) { 165 if (XcmsConvertColors(ccc, pColors_in_out, 1, result_format, 166 (Bool *) NULL) == XcmsFailure) { 167 return(XcmsFailure); 168 } 169 } 170 return(retval); 171} 172 173/* 174 * NAME 175 * XcmsSetColors - 176 * 177 * SYNOPSIS 178 */ 179Status 180_XcmsSetGetColors( 181 Status (*xColorProc)( 182 Display* /* display */, 183 Colormap /* colormap */, 184 XColor* /* screen_in_out */, 185 int /* nColors */), 186 Display *dpy, 187 Colormap cmap, 188 XcmsColor *pColors_in_out, 189 int nColors, 190 XcmsColorFormat result_format, 191 Bool *pCompressed) 192/* 193 * DESCRIPTION 194 * Routine containing code common to: 195 * XcmsQueryColors 196 * XcmsStoreColors 197 * 198 * RETURNS 199 * XcmsFailure if failed; 200 * XcmsSuccess if it succeeded without gamut compression; 201 * XcmsSuccessWithCompression if it succeeded with gamut 202 * compression; 203 */ 204{ 205 XcmsCCC ccc; 206 XColor *pXColors_in_out; 207 Status retval = XcmsSuccess; 208 209 /* 210 * Argument Checking 211 * 1. Assume xColorProc is correct 212 * 2. Insure ccc not NULL 213 * 3. Assume cmap correct (should be checked by Server) 214 * 4. Insure pColors_in_out valid 215 * 5. Assume method_in is valid (should be checked by Server) 216 * 6. Insure nColors > 0 217 */ 218 219 if (dpy == NULL) { 220 return(XcmsFailure); 221 } 222 223 if (nColors == 0) { 224 return(XcmsSuccess); 225 } 226 227 if (result_format == XcmsUndefinedFormat) { 228 return(XcmsFailure); 229 } 230 231 if ( !((*xColorProc == XStoreColors) || (*xColorProc == XQueryColors)) ) { 232 return(XcmsFailure); 233 } 234 235 if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) { 236 return(XcmsFailure); 237 } 238 239 /* 240 * Allocate space for XColors 241 */ 242 if ((pXColors_in_out = (XColor *)Xcalloc(nColors, sizeof(XColor))) == 243 NULL) { 244 return(XcmsFailure); 245 } 246 247 if (*xColorProc == XQueryColors) { 248 goto Query; 249 } 250 251 /* 252 * Convert to RGB, adjusting for white point differences if necessary. 253 */ 254 if ((retval = XcmsConvertColors(ccc, pColors_in_out, nColors, XcmsRGBFormat, 255 pCompressed)) == XcmsFailure) { 256 Xfree((char *)pXColors_in_out); 257 return(XcmsFailure); 258 } 259 260Query: 261 /* 262 * Convert XcmsColor to XColor structures 263 */ 264 _XcmsRGB_to_XColor(pColors_in_out, pXColors_in_out, nColors); 265 266 /* 267 * Now make appropriate X Call 268 */ 269 if ((*xColorProc == XQueryColors) || (*xColorProc == XStoreColors)){ 270 /* Note: XQueryColors and XStoreColors do not return any Status */ 271 (*xColorProc)(ccc->dpy, cmap, pXColors_in_out, nColors); 272 } else { 273 Xfree((char *)pXColors_in_out); 274 return(XcmsFailure); 275 } 276 277 if (*xColorProc == XStoreColors) { 278 Xfree((char *)pXColors_in_out); 279 return(retval); 280 } 281 282 /* 283 * Now, convert the returned XColor (i.e., rgb) to XcmsColor structures 284 */ 285 _XColor_to_XcmsRGB(ccc, pXColors_in_out, pColors_in_out, nColors); 286 Xfree((char *)pXColors_in_out); 287 288 /* 289 * Then, convert XcmsColor structures to the original specification 290 * format. Note that we must use NULL instead of passing 291 * pCompressed. 292 */ 293 if (result_format != XcmsRGBFormat) { 294 if (XcmsConvertColors(ccc, pColors_in_out, nColors, result_format, 295 (Bool *) NULL) == XcmsFailure) { 296 return(XcmsFailure); 297 } 298 } 299 300 return(retval); 301} 302 303/* ### EOF ### */ 304