/* *Constantstocontrolthefillingofthecolormap. *Bydefault,trytoallocatecolorsinthedefaultcolormapuntil *CMAP_ALLOC_DEFAULTcolorsarebeingused(byJavaand/orother *applications). *Forcaseswherethedefaultcolormapmayalreadyhavealarge *numberofcolorsinit,makesurethatweourselvestrytoadd *atleastCMAP_ALLOC_MINnewcolors,evenifweneedtoallocate *morethantheDEFAULTtodothat. *Undernocircumstanceswillthecolormapbefilledtomorethan *CMAP_ALLOC_MAXcolors.
*/ #define CMAP_ALLOC_MIN 100/* minimum number of colors to "add" */ #define CMAP_ALLOC_DEFAULT 200/* default number of colors in cmap */ #define CMAP_ALLOC_MAX 245/* maximum number of colors in cmap */
#ifndef HEADLESS /* *Findthebestcolor.
*/ int
awt_color_matchTC(int r, int g, int b, AwtGraphicsConfigDataPtr awt_data)
{
r = CLIP(r, 0, 255);
g = CLIP(g, 0, 255);
b = CLIP(b, 0, 255); return (((r >> awt_data->awtImage->clrdata.rScale)
<< awt_data->awtImage->clrdata.rOff) |
((g >> awt_data->awtImage->clrdata.gScale)
<< awt_data->awtImage->clrdata.gOff) |
((b >> awt_data->awtImage->clrdata.bScale)
<< awt_data->awtImage->clrdata.bOff));
}
int
awt_color_matchGS(int r, int g, int b, AwtGraphicsConfigDataPtr awt_data)
{
r = CLIP(r, 0, 255);
g = CLIP(g, 0, 255);
b = CLIP(b, 0, 255); return awt_data->color_data->img_grays[RGBTOGRAY(r, g, b)];
}
int
awt_color_match(int r, int g, int b, AwtGraphicsConfigDataPtr awt_data)
{ int besti = 0; int mindist, i, t, d;
ColorEntry *p = awt_data->color_data->awt_Colors;
r = CLIP(r, 0, 255);
g = CLIP(g, 0, 255);
b = CLIP(b, 0, 255);
/* look for pure gray match */ if ((r == g) && (g == b)) {
mindist = 256; for (i = 0 ; i < awt_data->awt_num_colors ; i++, p++) if (p->flags == ALLOCATED_COLOR) { if (! ((p->r == p->g) && (p->g == p->b)) ) continue;
d = ABS(p->r - r); if (d == 0) return i; if (d < mindist) {
besti = i;
mindist = d;
}
} return besti;
}
/* look for non-pure gray match */
mindist = 256 * 256 * 256; for (i = 0 ; i < awt_data->awt_num_colors ; i++, p++) if (p->flags == ALLOCATED_COLOR) {
t = p->r - r;
d = t * t; if (d >= mindist) continue;
t = p->g - g;
d += t * t; if (d >= mindist) continue;
t = p->b - b;
d += t * t; if (d >= mindist) continue; if (d == 0) return i; if (d < mindist) {
besti = i;
mindist = d;
}
} return besti;
}
/* *AllocateacolorintheXcolormapandreturnthepixel. *Ifthe"expectedpixel"isnon-negativethenwewillonly *accepttheallocationifwegetexactlythatpixelvalue. *ThispreventsusfromseeingabunchofReadWritepixels *allocatedbyanotherimagingapplicationandduplicating *thatsetofinaccessiblepixelsinourpreciousremaining *ReadOnlycolormapcells.
*/ staticint
alloc_col(Display *dpy, Colormap cm, int r, int g, int b, int pixel,
AwtGraphicsConfigDataPtr awt_data)
{
XColor col;
r = CLIP(r, 0, 255);
g = CLIP(g, 0, 255);
b = CLIP(b, 0, 255);
col.flags = DoRed | DoGreen | DoBlue;
col.red = (r << 8) | r;
col.green = (g << 8) | g;
col.blue = (b << 8) | b; if (XAllocColor(dpy, cm, &col)) { #ifdef DEBUG if (debug_colormap)
jio_fprintf(stdout, "allocated %d (%d,%d, %d)\n", col.pixel, r, g, b); #endif if (pixel >= 0 && col.pixel != (unsignedlong)pixel) { /* *Ifweweretryingtoallocateashareable"ReadOnly" *colorthenwewouldhavegottenbacktheexpected *pixel.Ifthereturnedpixelwasdifferent,then *thesourcecolorthatwewereattemptingtogain *accesstomustbesomeotherapplication'sReadWrite *privatecolor.Wefreethereturnedpixelsothat *wewon'twastepreciouscolormapentriesbyduplicating *thatcolorintheasyetunallocatedentries.We *return-1heretoindicatethefailuretogetthe *expectedpixel.
*/ #ifdef DEBUG if (debug_colormap)
jio_fprintf(stdout, " used by other app, freeing\n"); #endif
awt_data->color_data->awt_Colors[pixel].flags = UNAVAILABLE_COLOR;
XFreeColors(dpy, cm, &col.pixel, 1, 0); return -1;
} /* *Ourcurrentimplementationdoesn'tsupportpixelswhich *don'tfitin8bit(evenfor12-bitvisuals)
*/ if (col.pixel > 255) { #ifdef DEBUG if (debug_colormap)
jio_fprintf(stdout, "pixel %d for (%d,%d, %d) is > 8 bit, releasing.\n",
col.pixel, r, g, b); #endif
XFreeColors(dpy, cm, &col.pixel, 1, 0); return awt_color_match(r, g, b, awt_data);
}
alloc_col(dpy, cm, 255, 255, 255, -1, awt_data);
alloc_col(dpy, cm, 0, 0, 0, -1, awt_data);
if (awt_data->awtImage->clrdata.grayscale) { int g;
ColorEntry *p;
if (!forcemono) { for (i = 128; i > 0; i >>= 1) { for (g = i; g < 256; g += (i * 2)) {
alloc_col(dpy, cm, g, g, g, -1, awt_data);
}
}
}
awt_data->color_data->img_grays =
(unsignedchar *)calloc(256, sizeof(unsignedchar)); if ( awt_data->color_data->img_grays == NULL) {
cleanup_graphics_config_data(awt_data); return0;
} for (g = 0; g < 256; g++) { int mindist, besti; int d;
p = awt_data->color_data->awt_Colors;
mindist = 256;
besti = 0; for (i = 0 ; i < awt_data->awt_num_colors ; i++, p++) { if (forcegray && (p->r != p->g || p->g != p->b)) continue; if (forcemono && p->g != 0 && p->g != 255) continue; if (p->flags == ALLOCATED_COLOR) {
d = p->g - g; if (d < 0) d = -d; if (d < mindist) {
besti = i; if (d == 0) { break;
}
mindist = d;
}
}
}
awt_data->color_data->img_grays[g] = besti;
}
if (forcemono || (depth == 1)) { char *gammastr = getenv("HJGAMMA"); double gamma = atof(gammastr ? gammastr : "1.6"); if (gamma < 0.01) gamma = 1.0; #ifdef DEBUG if (debug_colormap) {
jio_fprintf(stderr, "gamma = %f\n", gamma);
} #endif for (i = 0; i < 256; i++) {
img_bwgamma[i] = (int) (pow(i/255.0, gamma) * 255); #ifdef DEBUG if (debug_colormap) {
jio_fprintf(stderr, "%3d ", img_bwgamma[i]); if ((i & 7) == 7)
jio_fprintf(stderr, "\n");
} #endif
}
} else { for (i = 0; i < 256; i++) {
img_bwgamma[i] = i;
}
}
#ifdef DEBUG if (debug_colormap) {
jio_fprintf(stderr, "GrayScale initialized\n");
jio_fprintf(stderr, "color table:\n"); for (i = 0; i < awt_data->awt_num_colors; i++) {
jio_fprintf(stderr, "%3d: %3d %3d %3d\n",
i, awt_data->color_data->awt_Colors[i].r,
awt_data->color_data->awt_Colors[i].g,
awt_data->color_data->awt_Colors[i].b);
}
jio_fprintf(stderr, "gray table:\n"); for (i = 0; i < 256; i++) {
jio_fprintf(stderr, "%3d ", awt_data->color_data->img_grays[i]); if ((i & 7) == 7)
jio_fprintf(stderr, "\n");
}
} #endif
} else {
alloc_col(dpy, cm, 255, 0, 0, -1, awt_data);
alloc_col(dpy, cm, 0, 255, 0, -1,awt_data);
alloc_col(dpy, cm, 0, 0, 255, -1,awt_data);
alloc_col(dpy, cm, 255, 255, 0, -1,awt_data);
alloc_col(dpy, cm, 255, 0, 255, -1,awt_data);
alloc_col(dpy, cm, 0, 255, 255, -1,awt_data);
alloc_col(dpy, cm, 192, 192, 192, -1,awt_data);
alloc_col(dpy, cm, 255, 128, 128, -1,awt_data);
alloc_col(dpy, cm, 128, 255, 128, -1,awt_data);
alloc_col(dpy, cm, 128, 128, 255, -1,awt_data);
alloc_col(dpy, cm, 255, 255, 128, -1,awt_data);
alloc_col(dpy, cm, 255, 128, 255, -1,awt_data);
alloc_col(dpy, cm, 128, 255, 255, -1,awt_data);
}
allocatedColorsNum = 0;
unavailableColorsNum = 0; /* we do not support more than 256 entries in the colormap
even for 12-bit PseudoColor visuals */ for (i = 0; i < MAX_PALETTE8_SIZE; i++) { if (awt_data->color_data->awt_Colors[i].flags == ALLOCATED_COLOR)
{
reds[allocatedColorsNum] = awt_data->color_data->awt_Colors[i].r;
greens[allocatedColorsNum] = awt_data->color_data->awt_Colors[i].g;
blues[allocatedColorsNum] = awt_data->color_data->awt_Colors[i].b;
allocatedColorsNum++;
} elseif (awt_data->color_data->awt_Colors[i].flags ==
UNAVAILABLE_COLOR) {
unavailableColorsNum++;
}
}
#ifdef DEBUG if (debug_colormap) { int alloc_count = 0; int reuse_count = 0; int free_count = 0; for (i = 0; i < awt_data->awt_num_colors; i++) { switch (awt_data->color_data->awt_Colors[i].flags) { case ALLOCATED_COLOR:
alloc_count++; break; case LIKELY_COLOR:
reuse_count++; break; case FREE_COLOR:
free_count++; break;
}
}
jio_fprintf(stdout, "%d total, %d allocated, %d reused, %d still free.\n",
awt_data->awt_num_colors, alloc_count, reuse_count, free_count);
} #endif
/* Fill in the ICM lut and lut2cmap mapping */
awt_data->color_data->awt_numICMcolors = 0;
awt_data->color_data->awt_icmLUT2Colors =
(unsignedchar *)calloc(paletteSize, sizeof (unsignedchar));
awt_data->color_data->awt_icmLUT = (int *)calloc(paletteSize, sizeof(int)); if (awt_data->color_data->awt_icmLUT2Colors == NULL || awt_data->color_data->awt_icmLUT == NULL) {
cleanup_graphics_config_data(awt_data); return0;
}
for (i=0; i < paletteSize; i++) { /* Keep the mapping between this lut and the actual cmap */
awt_data->color_data->awt_icmLUT2Colors
[awt_data->color_data->awt_numICMcolors] = i;
if (aData->color_data->img_grays == NULL) {
(*env)->PopLocalFrame(env, 0); return NULL;
}
for (g = 0; g < 256; g++) { int mindist, besti; int d;
p = aData->color_data->awt_Colors;
mindist = 256;
besti = 0; for (i = 0 ; i < paletteSize; i++, p++) { if (p->flags == ALLOCATED_COLOR) {
d = p->g - g; if (d < 0) d = -d; if (d < mindist) {
besti = i; if (d == 0) { break;
}
mindist = d;
}
}
}
aData->color_data->img_grays[g] = besti;
}
for (i = 0; i < 256; i++) {
img_bwgamma[i] = i; /* REMIND: what is img_bwgamma? *isitstillusedanywhere?
*/
}
}
if (aData->awtImage->clrdata.grayscale) { int i;
ColorEntry *p;
/* For purposes of creating an IndexColorModel, use transparentblackfornon-allocatedornon-graycolors.
*/
p = aData->color_data->awt_Colors;
b = 0;
pValid = &valid[sizeof(valid)]; for (i = 0; i < paletteSize; i++, p++) { if ((p->flags != ALLOCATED_COLOR) ||
(p->r != p->g || p->g != p->b))
{
rgb[i] = 0;
b &= ~(1 << (i % 8));
allvalid = 0;
} else {
b |= (1 << (i % 8));
} if ((i % 8) == 7) {
*--pValid = b; /* b = 0; not needed as each bit is explicitly set */
}
}
if (aData->color_data->pGrayInverseLutData == NULL) { /* Compute the inverse gray LUT for this aData->color_data struct,ifnotalreadycomputed.
*/
initInverseGrayLut(rgb, aData->awt_num_colors,
aData->color_data);
}
}
#ifndef HEADLESS void
awt_allocate_systemrgbcolors (jint *rgbColors, int num_colors,
AwtGraphicsConfigDataPtr awtData) { int i, pixel; for (i = 0; i < num_colors; i++)
pixel = alloc_col (awt_display, awtData->awt_cmap, red (rgbColors [i]),
green (rgbColors [i]), blue (rgbColors [i]), -1,
awtData);
}
int
awtCreateX11Colormap(AwtGraphicsConfigDataPtr adata) { int screen = adata->awt_visInfo.screen;
Colormap cmap = (Colormap)NULL;
void
awtJNI_CreateColorData(JNIEnv *env, AwtGraphicsConfigDataPtr adata, int lock) {
/* Create Colormap */ if (lock) {
AWT_LOCK ();
}
awtCreateX11Colormap(adata);
/* If depth is 8, allocate system colors also... Here *wejustgetthearrayofSystemColorsandallocate *itwhichmaybeabitwasteful(ifonlysomewere *changed).Butwedon'tknowwhichoneswerechanged *andalloc-ingapixelthatisalreadyallocatedwon't
* hurt. */
/* Unlock now to initialize the SystemColor class */ if (lock) {
AWT_UNLOCK_CHECK_EXCEPTION(env);
}
sysColors = (*env)->FindClass (env, "java/awt/SystemColor");
CHECK_NULL(sysColors);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.