📄 xcmscolnm.c
字号:
int *pSectionSize;/* * DESCRIPTION * Determines the amount of memory required to store the * color name strings and also the number of strings. * * RETURNS * XcmsSuccess if succeeded, otherwise XcmsFailure. * */{ char buf[XCMSDB_MAXLINELEN]; char token[XCMSDB_MAXLINELEN]; char token2[XCMSDB_MAXLINELEN]; char *pBuf; char *f1; char *f2; int i; *pNumEntries = 0; *pSectionSize = 0; /* * Advance to START_TOKEN * Anything before is just considered as comments. */ while((pBuf = fgets(buf, XCMSDB_MAXLINELEN, stream)) != NULL) { if ((sscanf(buf, "%s %s", token, token2)) && (strcmp(token, START_TOKEN) == 0)) { if (strcmp(token2, FORMAT_VERSION) != 0) { /* text file not in the right format */ return(XcmsFailure); } break; } /* else it was just a blank line or comment */ } if (pBuf == NULL) { return(XcmsFailure); } while((pBuf = fgets(buf, XCMSDB_MAXLINELEN, stream)) != NULL) { if ((sscanf(buf, "%s", token)) && (strcmp(token, END_TOKEN) == 0)) { break; } if (field2(buf, DELIM_CHAR, &f1, &f2) != XcmsSuccess) { return(XcmsFailure); } (*pNumEntries)++; (*pSectionSize) += (i = strlen(f1)) + 1; for (; i; i--, f1++) { /* REMOVE SPACES FROM COUNT */ if (isspace(*f1)) { (*pSectionSize)--; } } (*pSectionSize) += (i = strlen(f2)) + 1; for (; i; i--, f2++) { /* REMOVE SPACES FROM COUNT */ if (isspace(*f2)) { (*pSectionSize)--; } } } return(XcmsSuccess);}/* * NAME * ReadColornameDB - Read the Color Name Database * * SYNOPSIS */static StatusReadColornameDB(stream, pRec, pString) FILE *stream; XcmsPair *pRec; char *pString;/* * DESCRIPTION * Loads the Color Name Database from a text file. * * RETURNS * XcmsSuccess if succeeded, otherwise XcmsFailure. * */{ char buf[XCMSDB_MAXLINELEN]; char token[XCMSDB_MAXLINELEN]; char token2[XCMSDB_MAXLINELEN]; char *f1; char *f2; char *pBuf; /* * Advance to START_TOKEN * Anything before is just considered as comments. */ while((pBuf = fgets(buf, XCMSDB_MAXLINELEN, stream)) != NULL) { if ((sscanf(buf, "%s %s", token, token2)) && (strcmp(token, START_TOKEN) == 0)) { if (strcmp(token2, FORMAT_VERSION) != 0) { /* text file not in the right format */ return(XcmsFailure); } break; } /* else it was just a blank line or comment */ } if (pBuf == NULL) { return(XcmsFailure); } /* * Process lines between START_TOKEN to END_TOKEN */ while ((pBuf = fgets(buf, XCMSDB_MAXLINELEN, stream)) != NULL) { if ((sscanf(buf, "%s", token)) && (strcmp(token, END_TOKEN) == 0)) { /* * Found END_TOKEN so break out of for loop */ break; } /* * Get pairs */ if (field2(buf, DELIM_CHAR, &f1, &f2) != XcmsSuccess) { /* Invalid line */ continue; } /* * Add strings */ /* Left String */ pRec->first = pString; _XcmsCopyISOLatin1Lowered(pString, f1); pString += (1 + RemoveSpaces(pString)); pRec->second = pString; /* Right String */ _XcmsCopyISOLatin1Lowered(pString, f2); pString += RemoveSpaces(pString) + 1; pRec++; } return(XcmsSuccess);}/* * NAME * LoadColornameDB - Load the Color Name Database * * SYNOPSIS */static StatusLoadColornameDB()/* * DESCRIPTION * Loads the Color Name Database from a text file. * * RETURNS * XcmsSuccess if succeeded, otherwise XcmsFailure. * */{ int size; FILE *stream; char *pathname; struct stat txt; int length; /* use and name of this env var is not part of the standard */ /* implementation-dependent feature */ if ((pathname = getenv("XCMSDB")) == NULL) { pathname = XCMSDB; } length = strlen(pathname); if ((length == 0) || (length >= (BUFSIZ - 5))){ XcmsColorDbState = XcmsDbInitFailure; return(XcmsFailure); } if (stat(pathname, &txt)) { /* can't stat file */ XcmsColorDbState = XcmsDbInitFailure; return(XcmsFailure); } if ((stream = fopen(pathname, "r")) == NULL) { return(XcmsFailure); } stringSectionSize(stream, &nEntries, &size); rewind(stream); strings = (char *) Xmalloc(size); pairs = (XcmsPair *)Xcalloc(nEntries, sizeof(XcmsPair)); ReadColornameDB(stream, pairs, strings); /* * sort the pair recs */ qsort((char *)pairs, nEntries, sizeof(XcmsPair), FirstCmp); XcmsColorDbState = XcmsDbInitSuccess; return(XcmsSuccess);}/* * NAME * XcmsFreeColorDB - Free Color Name Database * * SYNOPSIS */voidXcmsFreeColorDB()/* * DESCRIPTION * Creates * * RETURNS * XcmsSuccess if succeeded, otherwise XcmsFailure. * */{ /* * Check if XcmsColorDB has been intialized */ if (XcmsColorDbState != XcmsDbInitSuccess) { return; } /* * Free memory */ free(strings); free(pairs);}/************************************************************************ * * * API PRIVATE ROUTINES * * * ************************************************************************//* * NAME * _XcmsCopyISOLatin1Lowered * * SYNOPSIS */void_XcmsCopyISOLatin1Lowered(dst, src) char *dst, *src;/* * DESCRIPTION * ISO Latin-1 case conversion routine * Identical to XmuCopyISOLatin1Lowered() but provided here * to eliminate need to link with libXmu.a. * * IMPLEMENTORS NOTE: * This routine is also used in XcmsFormatOfPrefix. * * RETURNS * Void * */{ register unsigned char *dest, *source; for (dest = (unsigned char *)dst, source = (unsigned char *)src; *source; source++, dest++) { if ((*source >= XK_A) && (*source <= XK_Z)) *dest = *source + (XK_a - XK_A); else if ((*source >= XK_Agrave) && (*source <= XK_Odiaeresis)) *dest = *source + (XK_agrave - XK_Agrave); else if ((*source >= XK_Ooblique) && (*source <= XK_Thorn)) *dest = *source + (XK_oslash - XK_Ooblique); else *dest = *source; } *dest = '\0';}/* * NAME * _XcmsResolveColorString - * * SYNOPSIS */#if NeedFunctionPrototypesStatus_XcmsResolveColorString ( XcmsCCC ccc, _Xconst char **color_string, XcmsColor *pColor_exact_return, XcmsColorFormat result_format)#elseStatus_XcmsResolveColorString(ccc, color_string, pColor_exact_return, result_format) XcmsCCC ccc; char **color_string; XcmsColor *pColor_exact_return; XcmsColorFormat result_format;#endif/* * DESCRIPTION * The XcmsLookupColor function finds the color specification * associated with a color name in the Device-Independent Color * Name Database. * RETURNS * XcmsFailure if failed to parse string or find any entry in * the database. * XcmsSuccess if succeeded in converting color string to * XcmsColor. * _XCMS_NEWNAME if succeeded in converting color string (which * is a color name to yet another color name. Note * that the new color name is returned via 'name'. * * This function returns both the color specification found in the * database (db specification) and the color specification for the * color displayable by the specified screen (screen * specification). The calling routine sets the format for these * returned specifications in the XcmsColor format component. * If XcmsUndefinedFormat, the specification is returned in the * format used to store the color in the database. */{ XcmsColor dbWhitePt; /* whitePt associated with pColor_exact_return*/ /* the screen's white point */ XcmsColor *pClientWhitePt; int retval; char *strptr = whitePtStr;/* * 0. Check for invalid arguments. */ if (ccc == NULL || (*color_string)[0] == '\0' || pColor_exact_return == NULL) { return(XcmsFailure); }/* * 1. First attempt to parse the string * If successful, then convert the specification to the target format * and return. */ if (_XcmsParseColorString(ccc, *color_string, pColor_exact_return) == 1) { if (result_format != XcmsUndefinedFormat && pColor_exact_return->format != result_format) { /* need to be converted to the target format */ return(XcmsConvertColors(ccc, pColor_exact_return, 1, result_format, (Bool *)NULL)); } else { return(XcmsSuccess); } }/* * 2. Attempt to find it in the DI Color Name Database */ /* * a. Convert String into a XcmsColor structure * Attempt to extract the specification for color_string from the * DI Database (pColor_exact_return). If the DI Database does not * have this entry, then return failure. */ retval = _XcmsLookupColorName(ccc, color_string, pColor_exact_return); if (retval == _XCMS_NEWNAME) { /* color_string replaced with a color name */ return(retval); } if ((retval == XcmsFailure) || (pColor_exact_return->format == XcmsUndefinedFormat)) { return(XcmsFailure); } /* * b. If result_format not defined, then assume target format * is the exact format. */ if (result_format == XcmsUndefinedFormat) { result_format = pColor_exact_return->format; } if ((ClientWhitePointOfCCC(ccc))->format == XcmsUndefinedFormat) { pClientWhitePt = ScreenWhitePointOfCCC(ccc); } else { pClientWhitePt = ClientWhitePointOfCCC(ccc); } /* * c. Convert to the target format, making adjustments for white * point differences as necessary. */ if (XCMS_DD_ID(pColor_exact_return->format)) { /* * The spec format is Device-Dependent, therefore assume the * its white point is the Screen White Point. */ if (XCMS_DD_ID(result_format)) { /* * Target format is Device-Dependent * Therefore, DD --> DD conversion */ return(_XcmsDDConvertColors(ccc, pColor_exact_return, 1, result_format, (Bool *) NULL)); } else { /* * Target format is Device-Independent * Therefore, DD --> DI conversion */ if (ccc->whitePtAdjProc && !_XcmsEqualWhitePts(ccc, pClientWhitePt, ScreenWhitePointOfCCC(ccc))) { return((*ccc->whitePtAdjProc)(ccc, ScreenWhitePointOfCCC(ccc), pClientWhitePt, result_format, pColor_exact_return, 1, (Bool *) NULL)); } else { if (_XcmsDDConvertColors(ccc, pColor_exact_return, 1, XcmsCIEXYZFormat, (Bool *) NULL) == XcmsFailure) { return(XcmsFailure); } return(_XcmsDIConvertColors(ccc, pColor_exact_return, pClientWhitePt, 1, result_format)); } } } else { /* * The spec format is Device-Independent, therefore attempt * to find a database white point. * * If the Database does not have a white point, then assume the * database white point is the same as the Screen White Point. */ if (_XcmsLookupColorName(ccc, &strptr, &dbWhitePt) != 1) { bcopy((char *)&ccc->pPerScrnInfo->screenWhitePt, (char *)&dbWhitePt, sizeof(XcmsColor)); } if (XCMS_DD_ID(result_format)) { /* * Target format is Device-Dependent * Therefore, DI --> DD conversion */ if (ccc->whitePtAdjProc && !_XcmsEqualWhitePts(ccc, &dbWhitePt, ScreenWhitePointOfCCC(ccc))) { return((*ccc->whitePtAdjProc)(ccc, &dbWhitePt, ScreenWhitePointOfCCC(ccc), result_format, pColor_exact_return, 1, (Bool *)NULL)); } else { if (pColor_exact_return->format != XcmsCIEXYZFormat) { if (_XcmsDIConvertColors(ccc, pColor_exact_return, &dbWhitePt, 1, XcmsCIEXYZFormat) == XcmsFailure) { return(XcmsFailure); } } return (_XcmsDDConvertColors(ccc, pColor_exact_return, 1, result_format, (Bool *)NULL)); } } else { /* * Target format is Device-Independent * Therefore, DI --> DI conversion */ if (ccc->whitePtAdjProc && !_XcmsEqualWhitePts(ccc, &dbWhitePt, pClientWhitePt)) { /* * The calling routine wants to resolve this color * in terms if it's white point (i.e. Client White Point). * Therefore, apply white adjustment for the displacement * between dbWhitePt to clientWhitePt. */ return((*ccc->whitePtAdjProc)(ccc, &dbWhitePt, pClientWhitePt, result_format, pColor_exact_return, 1, (Bool *)NULL)); } else if (_XcmsEqualWhitePts(ccc, &dbWhitePt, pClientWhitePt)) { /* * Can use either dbWhitePt or pClientWhitePt to * convert to the result_format. */ if (pColor_exact_return->format == result_format) { return(XcmsSuccess); } else { return (_XcmsDIConvertColors(ccc, pColor_exact_return, &dbWhitePt, 1, result_format)); } } else { /* * Need to convert to a white point independent color * space (let's choose CIEXYZ) then convert to the * target color space. Why? Lets assume that * pColor_exact_return->format and result format * are white point dependent format (e.g., CIELUV, CIELAB, * TekHVC ... same or any combination). If so, we'll * need to convert the color with dbWhitePt to an absolute * spec (i.e. non-white point dependent) then convert that * absolute value with clientWhitePt to the result_format. */ if (pColor_exact_return->format != XcmsCIEXYZFormat) { if (_XcmsDIConvertColors(ccc, pColor_exact_return, &dbWhitePt, 1, XcmsCIEXYZFormat) == XcmsFailure) { return(XcmsFailure); } } if (result_format == XcmsCIEXYZFormat) { return(XcmsSuccess); } else { return(_XcmsDIConvertColors(ccc, pColor_exact_return, pClientWhitePt, 1, result_format)); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -