| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
#include "bltInt.h" |
| 72 |
|
| 73 |
#ifndef WIN32 |
| 74 |
|
| 75 |
#include "bltHash.h" |
| 76 |
#include "bltImage.h" |
| 77 |
|
| 78 |
#define NCOLORS 256 |
| 79 |
|
| •80 |
#if 0 |
| 81 |
static void |
| 82 |
GetPaletteSizes(nColors, nRedsPtr, nGreensPtr, nBluesPtr) |
| 83 |
int nColors; |
| 84 |
unsigned int *nRedsPtr; |
| 85 |
unsigned int *nGreensPtr; |
| 86 |
unsigned int *nBluesPtr; |
| 87 |
{ |
| 88 |
unsigned int nBlues, nReds, nGreens; |
| 89 |
|
| 90 |
assert(nColors > 1); |
| 91 |
nBlues = nReds = nGreens = 0; |
| 92 |
while ((nBlues * nBlues * nBlues) <= nColors) { |
| 93 |
nBlues++; |
| 94 |
} |
| 95 |
nBlues--; |
| 96 |
while ((nReds * nReds * nBlues) <= nColors) { |
| 97 |
nReds++; |
| 98 |
} |
| 99 |
nReds--; |
| 100 |
nGreens = nColors / (nBlues * nReds); |
| 101 |
|
| 102 |
*nRedsPtr = nReds; |
| 103 |
*nGreensPtr = nGreens; |
| 104 |
*nBluesPtr = nBlues; |
| 105 |
} |
| 106 |
|
| 107 |
static void |
| 108 |
BuildColorRamp(palettePtr, nColors) |
| 109 |
Pix32 *palettePtr; |
| 110 |
int nColors; |
| 111 |
{ |
| 112 |
register unsigned int r, g, b; |
| 113 |
unsigned int short red, green, blue; |
| 114 |
unsigned int nReds, nGreens, nBlues; |
| 115 |
|
| 116 |
GetPaletteSizes(nColors, &nReds, &nGreens, &nBlues); |
| 117 |
for (r = 0; r < nReds; r++) { |
| 118 |
red = (r * USHRT_MAX) / (nReds - 1); |
| 119 |
for (g = 0; g < nGreens; g++) { |
| 120 |
green = (g * USHRT_MAX) / (nGreens - 1); |
| 121 |
for (b = 0; b < nBlues; b++) { |
| 122 |
blue = (b * USHRT_MAX) / (nBlues - 1); |
| 123 |
palettePtr->Red = red; |
| 124 |
palettePtr->Green = green; |
| 125 |
palettePtr->Blue = blue; |
| 126 |
palettePtr++; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
} |
| •132 |
#endif |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static int |
| 151 |
QueryColormap(display, colorMap, mapColors, numMapColorsPtr) |
| 152 |
Display *display; |
| 153 |
Colormap colorMap; |
| 154 |
XColor mapColors[]; |
| 155 |
int *numMapColorsPtr; |
| 156 |
{ |
| 157 |
unsigned long int pixelValues[NCOLORS]; |
| 158 |
int numAvail, numMapColors; |
| 159 |
register int i; |
| 160 |
register XColor *colorPtr; |
| 161 |
register unsigned long *indexPtr; |
| 162 |
int inUse[NCOLORS]; |
| 163 |
|
| 164 |
|
| 165 |
memset((char *)inUse, 0, sizeof(int) * NCOLORS); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
numAvail = 0; |
| 173 |
for (indexPtr = pixelValues, i = 0; i < NCOLORS; i++, indexPtr++) { |
| 174 |
if (!XAllocColorCells(display, colorMap, False, NULL, 0, indexPtr, 1)) { |
| 175 |
break; |
| 176 |
} |
| 177 |
inUse[*indexPtr] = TRUE; |
| 178 |
numAvail++; |
| 179 |
} |
| 180 |
XFreeColors(display, colorMap, pixelValues, numAvail, 0); |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
numMapColors = 0; |
| 187 |
colorPtr = mapColors; |
| 188 |
for (i = 0; i < NCOLORS; i++) { |
| 189 |
if (!inUse[i]) { |
| 190 |
colorPtr->pixel = i; |
| 191 |
colorPtr->flags = (DoRed | DoGreen | DoBlue); |
| 192 |
colorPtr++, numMapColors++; |
| 193 |
} |
| 194 |
} |
| 195 |
XQueryColors(display, colorMap, mapColors, numMapColors); |
| 196 |
*numMapColorsPtr = numMapColors; |
| 197 |
#ifdef notdef |
| 198 |
fprintf(stderr, "Number of colors (allocated/free) %d/%d\n", numMapColors, |
| 199 |
numAvail); |
| 200 |
#endif |
| 201 |
return numAvail; |
| 202 |
} |
| 203 |
|
| •204 |
#if 0 |
| 205 |
static void |
| 206 |
FindClosestColor(colorPtr, mapColors, numMapColors) |
| 207 |
ColorInfo *colorPtr; |
| 208 |
XColor mapColors[]; |
| 209 |
int numMapColors; |
| 210 |
{ |
| 211 |
double r, g, b; |
| 212 |
register int i; |
| 213 |
double dist, min; |
| 214 |
XColor *lastMatch; |
| 215 |
register XColor *mapColorPtr; |
| 216 |
|
| 217 |
min = DBL_MAX; |
| 218 |
lastMatch = NULL; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
mapColorPtr = mapColors; |
| 223 |
for (i = 0; i < numMapColors; i++, mapColorPtr++) { |
| 224 |
r = (double)mapColorPtr->red - (double)colorPtr->exact.red; |
| 225 |
g = (double)mapColorPtr->green - (double)colorPtr->exact.green; |
| 226 |
b = (double)mapColorPtr->blue - (double)colorPtr->exact.blue; |
| 227 |
|
| 228 |
dist = (r * r) + (b * b) + (g * g); |
| 229 |
if (dist < min) { |
| 230 |
min = dist; |
| 231 |
lastMatch = mapColorPtr; |
| 232 |
} |
| 233 |
} |
| 234 |
colorPtr->best = *lastMatch; |
| 235 |
colorPtr->best.flags = (DoRed | DoGreen | DoBlue); |
| 236 |
colorPtr->error = (float)sqrt(min); |
| 237 |
} |
| 238 |
|
| 239 |
static int |
| 240 |
CompareColors(a, b) |
| 241 |
void *a, *b; |
| 242 |
{ |
| 243 |
ColorInfo *i1Ptr, *i2Ptr; |
| 244 |
|
| 245 |
i1Ptr = *(ColorInfo **) a; |
| 246 |
i2Ptr = *(ColorInfo **) b; |
| 247 |
if (i2Ptr->error > i1Ptr->error) { |
| 248 |
return 1; |
| 249 |
} else if (i2Ptr->error < i1Ptr->error) { |
| 250 |
return -1; |
| 251 |
} |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
static float |
| 256 |
MatchColors(colorTabPtr, rgbPtr, numColors, numAvailColors, numMapColors, |
| 257 |
mapColors) |
| 258 |
struct ColorTableStruct *colorTabPtr; |
| 259 |
Pix32 *rgbPtr; |
| 260 |
int numColors; |
| 261 |
int numAvailColors; |
| 262 |
int numMapColors; |
| 263 |
XColor mapColors[NCOLORS]; |
| 264 |
{ |
| 265 |
int numMatched; |
| 266 |
float sum; |
| 267 |
register int i; |
| 268 |
register ColorInfo *colorPtr; |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
colorPtr = colorTabPtr->colorInfo; |
| 277 |
for (i = 0; i < numColors; i++, colorPtr++, rgbPtr++) { |
| 278 |
colorPtr->index = i; |
| 279 |
colorTabPtr->sortedColors[i] = colorPtr; |
| 280 |
colorPtr->exact.red = rgbPtr->Red; |
| 281 |
colorPtr->exact.green = rgbPtr->Green; |
| 282 |
colorPtr->exact.blue = rgbPtr->Blue; |
| 283 |
colorPtr->exact.flags = (DoRed | DoGreen | DoBlue); |
| 284 |
FindClosestColor(colorPtr, mapColors, numMapColors); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
qsort(colorTabPtr->sortedColors, numColors, sizeof(ColorInfo *), |
| 291 |
(QSortCompareProc *)CompareColors); |
| 292 |
|
| 293 |
for (i = 0; i < numColors; i++) { |
| 294 |
colorPtr = colorTabPtr->sortedColors[i]; |
| 295 |
fprintf(stderr, "%d. %04x%04x%04x / %04x%04x%04x = %f (%d)\n", i, |
| 296 |
colorPtr->exact.red, colorPtr->exact.green, colorPtr->exact.blue, |
| 297 |
colorPtr->best.red, colorPtr->best.green, colorPtr->best.blue, |
| 298 |
colorPtr->error, colorPtr->freq); |
| 299 |
} |
| 300 |
sum = 0.0; |
| 301 |
numMatched = 0; |
| 302 |
for (i = numAvailColors; i < numColors; i++) { |
| 303 |
colorPtr = colorTabPtr->sortedColors[i]; |
| 304 |
sum += colorPtr->error; |
| 305 |
numMatched++; |
| 306 |
} |
| 307 |
if (numMatched > 0) { |
| 308 |
sum /= numMatched; |
| 309 |
} |
| 310 |
return sum; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
static int |
| 315 |
AllocateColors(nImageColors, colorTabPtr, matchOnly) |
| 316 |
int nImageColors; |
| 317 |
struct ColorTableStruct *colorTabPtr; |
| 318 |
int matchOnly; |
| 319 |
{ |
| 320 |
register int i; |
| 321 |
register ColorInfo *colorPtr; |
| 322 |
unsigned long int pixelValue; |
| 323 |
|
| 324 |
for (i = 0; i < nImageColors; i++) { |
| 325 |
colorPtr = colorTabPtr->sortedColors[i]; |
| 326 |
if (matchOnly) { |
| 327 |
XAllocColor(colorTabPtr->display, colorTabPtr->colorMap, |
| 328 |
&colorPtr->best); |
| 329 |
pixelValue = colorPtr->best.pixel; |
| 330 |
} else { |
| 331 |
colorPtr->allocated = XAllocColor(colorTabPtr->display, |
| 332 |
colorTabPtr->colorMap, &colorPtr->exact); |
| 333 |
|
| 334 |
if (colorPtr->allocated) { |
| 335 |
pixelValue = colorPtr->exact.pixel; |
| 336 |
} else { |
| 337 |
XAllocColor(colorTabPtr->display, colorTabPtr->colorMap, |
| 338 |
&colorPtr->best); |
| 339 |
pixelValue = colorPtr->best.pixel; |
| 340 |
} |
| 341 |
} |
| 342 |
colorTabPtr->pixelValues[colorPtr->index] = pixelValue; |
| 343 |
} |
| 344 |
colorTabPtr->nPixels = nImageColors; |
| 345 |
return 1; |
| 346 |
} |
| •347 |
#endif |
| 348 |
|
| 349 |
ColorTable |
| 350 |
Blt_CreateColorTable(tkwin) |
| 351 |
Tk_Window tkwin; |
| 352 |
{ |
| 353 |
XVisualInfo visualInfo, *visualInfoPtr; |
| 354 |
int nVisuals; |
| 355 |
Visual *visualPtr; |
| 356 |
Display *display; |
| 357 |
struct ColorTableStruct *colorTabPtr; |
| 358 |
|
| 359 |
display = Tk_Display(tkwin); |
| 360 |
visualPtr = Tk_Visual(tkwin); |
| 361 |
|
| 362 |
colorTabPtr = Blt_Calloc(1, sizeof(struct ColorTableStruct)); |
| 363 |
assert(colorTabPtr); |
| 364 |
colorTabPtr->display = Tk_Display(tkwin); |
| 365 |
colorTabPtr->colorMap = Tk_Colormap(tkwin); |
| 366 |
|
| 367 |
visualInfo.screen = Tk_ScreenNumber(tkwin); |
| 368 |
visualInfo.visualid = XVisualIDFromVisual(visualPtr); |
| 369 |
visualInfoPtr = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, |
| 370 |
&visualInfo, &nVisuals); |
| 371 |
|
| 372 |
colorTabPtr->visualInfo = *visualInfoPtr; |
| 373 |
XFree(visualInfoPtr); |
| 374 |
|
| 375 |
return colorTabPtr; |
| 376 |
} |
| 377 |
|
| 378 |
void |
| 379 |
Blt_FreeColorTable(colorTabPtr) |
| 380 |
struct ColorTableStruct *colorTabPtr; |
| 381 |
{ |
| 382 |
if (colorTabPtr == NULL) { |
| 383 |
return; |
| 384 |
} |
| 385 |
if (colorTabPtr->nPixels > 0) { |
| 386 |
XFreeColors(colorTabPtr->display, colorTabPtr->colorMap, |
| 387 |
colorTabPtr->pixelValues, colorTabPtr->nPixels, 0); |
| 388 |
} |
| 389 |
Blt_Free(colorTabPtr); |
| 390 |
} |
| 391 |
|
| 392 |
extern int redAdjust, greenAdjust, blueAdjust; |
| 393 |
extern int redMaskShift, greenMaskShift, blueMaskShift; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
ColorTable |
| 410 |
Blt_DirectColorTable(interp, tkwin, image) |
| 411 |
Tcl_Interp *interp; |
| 412 |
Tk_Window tkwin; |
| 413 |
Blt_ColorImage image; |
| 414 |
{ |
| 415 |
struct ColorTableStruct *colorTabPtr; |
| 416 |
Visual *visualPtr; |
| 417 |
Display *display; |
| 418 |
XColor color; |
| 419 |
int nr, ng, nb; |
| 420 |
int rBand, gBand, bBand; |
| 421 |
int rLast, gLast, bLast; |
| 422 |
unsigned int r, g, b; |
| 423 |
unsigned int value; |
| 424 |
register int i; |
| 425 |
|
| 426 |
display = Tk_Display(tkwin); |
| 427 |
visualPtr = Tk_Visual(tkwin); |
| 428 |
|
| 429 |
colorTabPtr = Blt_CreateColorTable(tkwin); |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
nr = ((unsigned int)visualPtr->red_mask >> redMaskShift) + 1; |
| 434 |
ng = ((unsigned int)visualPtr->green_mask >> greenMaskShift) + 1; |
| 435 |
nb = ((unsigned int)visualPtr->blue_mask >> blueMaskShift) + 1; |
| 436 |
|
| 437 |
#ifdef notdef |
| 438 |
assert((nr <= visualPtr->map_entries) && (ng <= visualPtr->map_entries) && |
| 439 |
(nb <= visualPtr->map_entries)); |
| 440 |
#endif |
| 441 |
rBand = NCOLORS / nr; |
| 442 |
gBand = NCOLORS / ng; |
| 443 |
bBand = NCOLORS / nb; |
| 444 |
|
| 445 |
retry: |
| 446 |
color.flags = (DoRed | DoGreen | DoBlue); |
| 447 |
rLast = gLast = bLast = 0; |
| 448 |
r = g = b = 0; |
| 449 |
for (i = 0; i < visualPtr->map_entries; i++) { |
| 450 |
if (rLast < NCOLORS) { |
| 451 |
r = rLast + rBand; |
| 452 |
if (r > NCOLORS) { |
| 453 |
r = NCOLORS; |
| 454 |
} |
| 455 |
} |
| 456 |
if (gLast < NCOLORS) { |
| 457 |
g = gLast + gBand; |
| 458 |
if (g > NCOLORS) { |
| 459 |
g = NCOLORS; |
| 460 |
} |
| 461 |
} |
| 462 |
if (bLast < NCOLORS) { |
| 463 |
b = bLast + bBand; |
| 464 |
if (b > NCOLORS) { |
| 465 |
b = NCOLORS; |
| 466 |
} |
| 467 |
} |
| 468 |
color.red = (r - 1) * (NCOLORS + 1); |
| 469 |
color.green = (g - 1) * (NCOLORS + 1); |
| 470 |
color.blue = (b - 1) * (NCOLORS + 1); |
| 471 |
|
| 472 |
if (!XAllocColor(display, colorTabPtr->colorMap, &color)) { |
| 473 |
XFreeColors(display, colorTabPtr->colorMap, |
| 474 |
colorTabPtr->pixelValues, i, 0); |
| 475 |
if ((colorTabPtr->flags & PRIVATE_COLORMAP) == 0) { |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
fprintf(stderr, "Need to allocate private colormap\n"); |
| 482 |
colorTabPtr->colorMap = Tk_GetColormap(interp, tkwin, "."); |
| 483 |
|
| 484 |
XSetWindowColormap(display, Tk_WindowId(tkwin), |
| 485 |
colorTabPtr->colorMap); |
| 486 |
colorTabPtr->flags |= PRIVATE_COLORMAP; |
| 487 |
goto retry; |
| 488 |
} |
| 489 |
#ifdef notdef |
| 490 |
fprintf(stderr, "Failed to allocate after %d colors\n", i); |
| 491 |
#endif |
| 492 |
Blt_Free(colorTabPtr); |
| 493 |
return NULL; |
| 494 |
} |
| 495 |
colorTabPtr->pixelValues[i] = color.pixel; |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
value = color.pixel & visualPtr->red_mask; |
| 500 |
while (rLast < r) { |
| 501 |
colorTabPtr->red[rLast++] = value; |
| 502 |
} |
| 503 |
value = color.pixel & visualPtr->green_mask; |
| 504 |
while (gLast < g) { |
| 505 |
colorTabPtr->green[gLast++] = value; |
| 506 |
} |
| 507 |
value = color.pixel & visualPtr->blue_mask; |
| 508 |
while (bLast < b) { |
| 509 |
colorTabPtr->blue[bLast++] = value; |
| 510 |
} |
| 511 |
} |
| 512 |
colorTabPtr->nPixels = i; |
| 513 |
return colorTabPtr; |
| 514 |
} |
| 515 |
|
| •516 |
#if 0 |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
static int |
| 523 |
GetUniqueColors(image) |
| 524 |
Blt_ColorImage image; |
| 525 |
{ |
| 526 |
register int i, nColors; |
| 527 |
register Pix32 *pixelPtr; |
| 528 |
Pix32 color; |
| 529 |
Blt_HashEntry *hPtr; |
| 530 |
int isNew, nPixels; |
| 531 |
int refCount; |
| 532 |
Blt_HashTable colorTable; |
| 533 |
|
| 534 |
Blt_InitHashTable(&colorTable, BLT_ONE_WORD_KEYS); |
| 535 |
|
| 536 |
nPixels = Blt_ColorImageWidth(image) * Blt_ColorImageHeight(image); |
| 537 |
nColors = 0; |
| 538 |
pixelPtr = Blt_ColorImageBits(image); |
| 539 |
for (i = 0; i < nPixels; i++, pixelPtr++) { |
| 540 |
color.value = pixelPtr->value; |
| 541 |
color.Alpha = 0xFF; |
| 542 |
hPtr = Blt_CreateHashEntry(&colorTable, (char *)color.value, &isNew); |
| 543 |
if (isNew) { |
| 544 |
refCount = 1; |
| 545 |
nColors++; |
| 546 |
} else { |
| 547 |
refCount = (int)Blt_GetHashValue(hPtr); |
| 548 |
refCount++; |
| 549 |
} |
| 550 |
Blt_SetHashValue(hPtr, (ClientData)refCount); |
| 551 |
} |
| 552 |
Blt_DeleteHashTable(&colorTable); |
| 553 |
return nColors; |
| 554 |
} |
| •555 |
#endif |
| 556 |
|
| 557 |
#define Blt_DefaultColormap(tkwin) \ |
| 558 |
DefaultColormap(Tk_Display(tkwin), Tk_ScreenNumber(tkwin)) |
| 559 |
|
| 560 |
|
| 561 |
static void |
| 562 |
PrivateColormap(interp, colorTabPtr, image, tkwin) |
| 563 |
Tcl_Interp *interp; |
| 564 |
struct ColorTableStruct *colorTabPtr; |
| 565 |
Blt_ColorImage image; |
| 566 |
Tk_Window tkwin; |
| 567 |
{ |
| 568 |
int keepColors = 0; |
| 569 |
register int i; |
| 570 |
XColor usedColors[NCOLORS]; |
| •571 |
int nUsedColors; |
| 572 |
Colormap colorMap; |
| 573 |
int inUse[NCOLORS]; |
| 574 |
XColor *colorPtr; |
| •575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
colorTabPtr->colorMap = colorMap = Tk_Colormap(tkwin); |
| 583 |
|
| 584 |
nUsedColors = 0; |
| 585 |
|
| 586 |
if (colorTabPtr->nPixels > 0) { |
| 587 |
XFreeColors(colorTabPtr->display, colorTabPtr->colorMap, |
| 588 |
colorTabPtr->pixelValues, colorTabPtr->nPixels, 0); |
| 589 |
} |
| •590 |
QueryColormap(colorTabPtr->display, colorMap, usedColors, |
| 591 |
&nUsedColors); |
| 592 |
memset((char *)inUse, 0, sizeof(int) * NCOLORS); |
| 593 |
if ((nUsedColors == 0) && (keepColors > 0)) { |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| •600 |
QueryColormap(colorTabPtr->display, |
| 601 |
Blt_DefaultColormap(tkwin), usedColors, &nUsedColors); |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
if (nUsedColors > keepColors) { |
| 612 |
nUsedColors = keepColors; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
} |
| 621 |
for (colorPtr = usedColors, i = 0; i < nUsedColors; i++, colorPtr++) { |
| 622 |
inUse[colorPtr->pixel] = TRUE; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| •630 |
|
| 631 |
|
| 632 |
Tk_SetWindowColormap(tkwin, colorMap); |
| 633 |
} |
| 634 |
|
| 635 |
ColorTable |
| 636 |
Blt_PseudoColorTable(interp, tkwin, image) |
| 637 |
Tcl_Interp *interp; |
| 638 |
Tk_Window tkwin; |
| 639 |
Blt_ColorImage image; |
| 640 |
{ |
| 641 |
struct ColorTableStruct *colorTabPtr; |
| 642 |
Colormap defColorMap; |
| 643 |
int usePrivate; |
| 644 |
|
| 645 |
colorTabPtr = Blt_CreateColorTable(tkwin); |
| 646 |
defColorMap = DefaultColormap(colorTabPtr->display, Tk_ScreenNumber(tkwin)); |
| 647 |
if (colorTabPtr->colorMap == defColorMap) { |
| 648 |
fprintf(stderr, "Using default colormap\n"); |
| 649 |
} |
| 650 |
|
| 651 |
colorTabPtr->lut = Blt_Malloc(sizeof(unsigned int) * 33 * 33 * 33); |
| 652 |
assert(colorTabPtr->lut); |
| 653 |
|
| 654 |
usePrivate = TRUE; |
| 655 |
if (usePrivate) { |
| 656 |
PrivateColormap(interp, colorTabPtr, image, tkwin); |
| 657 |
} else { |
| 658 |
#ifdef notdef |
| 659 |
ReadOnlyColormap(colorTabPtr, image, tkwin); |
| 660 |
#endif |
| 661 |
} |
| 662 |
return colorTabPtr; |
| 663 |
} |
| 664 |
|
| 665 |
#ifdef notdef |
| 666 |
|
| 667 |
static void |
| 668 |
ConvoleColorImage(srcImage, destImage, kernelPtr) |
| 669 |
Blt_ColorImage srcImage, destImage; |
| 670 |
ConvoleKernel *kernelPtr; |
| 671 |
{ |
| 672 |
Pix32 *srcPtr, *destPtr; |
| 673 |
Pix32 *src[MAXROWS]; |
| 674 |
register int x, y, i, j; |
| 675 |
int red, green, blue; |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
srcPtr = Blt_ColorImageBits(srcImage); |
| 680 |
destPtr = Blt_ColorImageBits(destImage); |
| 681 |
|
| 682 |
width = Blt_ColorImageWidth(srcImage); |
| 683 |
height = Blt_ColorImageHeight(srcImage); |
| 684 |
|
| 685 |
yOffset = kernelPtr->height / 2; |
| 686 |
xOffset = kernelPtr->width / 2; |
| 687 |
for (y = yOffset; y < (height - yOffset); y++) { |
| 688 |
|
| 689 |
for (i = 0; i < kernelPtr->height; i++) { |
| 690 |
src[i] = srcPtr + (i * width); |
| 691 |
} |
| 692 |
for (x = xOffset; x < (width - xOffset); x++) { |
| 693 |
red = green = blue = 0; |
| 694 |
kernPtr = kernelPtr->values; |
| 695 |
for (i = 0; i < kernelPtr->height; i++) { |
| 696 |
for (j = 0; j < kernelPtr->width; j++) { |
| 697 |
red += *valuePtr * src[i][j].Red; |
| 698 |
green += *valuePtr * src[i][j].Green; |
| 699 |
blue += *valuePtr * src[i][j].Blue; |
| 700 |
valuePtr++; |
| 701 |
} |
| 702 |
} |
| 703 |
destPtr->Red = red / kernelPtr->sum; |
| 704 |
destPtr->Green = green / kernelPtr->sum; |
| 705 |
destPtr->Blue = blue / kernelPtr->sum; |
| 706 |
destPtr++; |
| 707 |
} |
| 708 |
srcPtr += width; |
| 709 |
} |
| 710 |
sum = bot[0].Red + |
| 711 |
red = bot[0].Red + bot[1].Red + mid[1].Red + top[0].Red + top[1].Red; |
| 712 |
green = bot[0].Green + bot[1].Green + mid[1].Green + top[0].Green + |
| 713 |
top[1].Green; |
| 714 |
blue = bot[0].Blue + bot[1].Blue + mid[1].Blue + top[0].Blue + top[1].Blue; |
| 715 |
error = (red / 5) - mid[0].Red; |
| 716 |
redVal = mid[0].Red - (error * blend / blend_divisor); |
| 717 |
error = (green / 5) - mid[0].Green; |
| 718 |
greenVal = mid[0].Green - (error * blend / blend_divisor); |
| 719 |
error = (blue / 5) - mid[0].Blue; |
| 720 |
blueVal = mid[0].Blue - (error * blend / blend_divisor); |
| 721 |
|
| 722 |
out[0].Red = CLAMP(redVal); |
| 723 |
out[0].Green = CLAMP(greenVal); |
| 724 |
out[0].Blue = CLAMP(blueVal); |
| 725 |
|
| 726 |
for (i = 1; i < (width - 1); i++) { |
| 727 |
for (chan = 0; chan < 3; chan++) { |
| 728 |
total = bot[chan][i - 1] + bot[chan][i] + bot[chan][i + 1] + |
| 729 |
mid[chan][i - 1] + mid[chan][i + 1] + |
| 730 |
top[chan][i - 1] + top[chan][i] + top[chan][i + 1]; |
| 731 |
avg = total >> 3; |
| 732 |
diff = avg - mid[chan][i]; |
| 733 |
result = mid[chan][i] - (diff * blend / blend_divisor); |
| 734 |
out[chan][i] = CLAMP(result); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
for (chan = 0; chan < 3; chan++) { |
| 739 |
total = bot[chan][i - 1] + bot[chan][i] + |
| 740 |
mid[chan][i - 1] + |
| 741 |
top[chan][i - 1] + top[chan][i]; |
| 742 |
avg = total / 5; |
| 743 |
diff = avg - mid[chan][i]; |
| 744 |
result = mid[chan][i] - (diff * blend / blend_divisor); |
| 745 |
out[chan][i] = CLAMP(result); |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
static void |
| 750 |
DitherRow(srcImage, destImage, lastRow, curRow) |
| 751 |
Blt_ColorImage srcImage, destImage; |
| 752 |
int width, height; |
| 753 |
int bottom, top; |
| 754 |
{ |
| 755 |
int width, height; |
| 756 |
|
| 757 |
width = Blt_ColorImageWidth(srcImage); |
| 758 |
topPtr = Blt_ColorImageBits(destPtr) + (width * row); |
| 759 |
rowPtr = topPtr + width; |
| 760 |
botPtr = rowPtr + width; |
| 761 |
|
| 762 |
for (x = 0; x < width; x++) { |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
midPtr->red = CLAMP(midPtr->red); |
| 767 |
midPtr->blue = CLAMP(midPtr->blue); |
| 768 |
midPtr->green = CLAMP(midPtr->green); |
| 769 |
|
| 770 |
r = (midPtr->red >> 3) + 1; |
| 771 |
g = (midPtr->green >> 3) + 1; |
| 772 |
b = (midPtr->blue >> 3) + 1; |
| 773 |
index = colorTabPtr->lut[r][g][b]; |
| 774 |
|
| 775 |
redVal = midPtr->red * (NCOLORS + 1); |
| 776 |
greenVal = midPtr->green * (NCOLORS + 1); |
| 777 |
blueVal = midPtr->blue * (NCOLORS + 1); |
| 778 |
|
| 779 |
error = colorVal - colorMap[index].red; |
| 780 |
if (x < 511) { |
| 781 |
currRow[x + 1].Red = currRow[x + 1].Red + 7 * error / 16; |
| 782 |
nextRow[x + 1].Red = nextRow[x + 1].Red + error / 16; |
| 783 |
} |
| 784 |
nextRow[x].Red = nextRow[x].Red + 5 * error / 16; |
| 785 |
if (x > 0) { |
| 786 |
nextRow[x - 1].Red = nextRow[x - 1].Red + 3 * error / 16; |
| 787 |
} |
| 788 |
error = row[x][c] - colormap[index][c]; |
| 789 |
|
| 790 |
value = srcPtr->channel[i] * error[i]; |
| 791 |
value = CLAMP(value); |
| 792 |
destPtr->channel[i] = value; |
| 793 |
|
| 794 |
|
| 795 |
pixel = PsuedoColorPixel(); |
| 796 |
error[RED] = colorPtr->Red - srcPtr->Red * (NCOLORS + 1); |
| 797 |
|
| 798 |
|
| 799 |
colorTabPtr->lut[r][g][b]; |
| 800 |
colorPtr = PixelToColorInfo(pixel); |
| 801 |
error = colorPtr->error; |
| 802 |
|
| 803 |
register rle_pixel *optr; |
| 804 |
register int j; |
| 805 |
register short *thisptr, *nextptr = NULL; |
| 806 |
int chan; |
| 807 |
static int nchan = 0; |
| 808 |
int lastline = 0, lastpixel; |
| 809 |
static int *cval = 0; |
| 810 |
static rle_pixel *pixel = 0; |
| 811 |
|
| 812 |
if (nchan != in_hdr->ncolors) |
| 813 |
if (cval) { |
| 814 |
Blt_Free(cval); |
| 815 |
Blt_Free(pixel); |
| 816 |
} |
| 817 |
nchan = in_hdr->ncolors; |
| 818 |
if (!cval) { |
| 819 |
if ((cval = Blt_Malloc(nchan * sizeof(int))) == 0) |
| 820 |
malloc_ERR; |
| 821 |
if ((pixel = Blt_Malloc(nchan * sizeof(rle_pixel))) == 0) |
| 822 |
malloc_ERR; |
| 823 |
} |
| 824 |
optr = outrow[RLE_RED]; |
| 825 |
|
| 826 |
thisptr = row_top; |
| 827 |
if (row_bottom) |
| 828 |
nextptr = row_bottom; |
| 829 |
else |
| 830 |
lastline = 1; |
| 831 |
|
| 832 |
for (x = 0; x < width; x++) { |
| 833 |
int cmap_index = 0; |
| 834 |
|
| 835 |
lastpixel = (x == (width - 1)); |
| 836 |
val = srcPtr->Red; |
| 837 |
|
| 838 |
for (chan = 0; chan < 3; chan++) { |
| 839 |
cval[chan] = *thisptr++; |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
if (cval[chan] < 0) |
| 846 |
cval[chan] = 0; |
| 847 |
else if (cval[chan] > 255) |
| 848 |
cval[chan] = 255; |
| 849 |
|
| 850 |
pixel[chan] = cval[chan]; |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
find_closest(map, nchan, maplen, pixel, &cmap_index); |
| 855 |
*optr++ = cmap_index; |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
for (chan = 0; chan < nchan; chan++) { |
| 863 |
cval[chan] -= map[chan][cmap_index]; |
| 864 |
|
| 865 |
if (!lastpixel) { |
| 866 |
thisptr[chan] += cval[chan] * 7 / 16; |
| 867 |
} |
| 868 |
if (!lastline) { |
| 869 |
if (j != 0) { |
| 870 |
nextptr[-nchan] += cval[chan] * 3 / 16; |
| 871 |
} |
| 872 |
nextptr[0] += cval[chan] * 5 / 16; |
| 873 |
if (!lastpixel) { |
| 874 |
nextptr[nchan] += cval[chan] / 16; |
| 875 |
} |
| 876 |
nextptr++; |
| 877 |
} |
| 878 |
} |
| 879 |
} |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
static Blt_ColorImage |
| 885 |
DoColorDither(pic24, pic8, w, h, rmap, gmap, bmap, rdisp, gdisp, bdisp, maplen) |
| 886 |
byte *pic24, *pic8, *rmap, *gmap, *bmap, *rdisp, *gdisp, *bdisp; |
| 887 |
int w, h, maplen; |
| 888 |
{ |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
byte *np, *ep, *newpic; |
| 902 |
short *cache; |
| 903 |
int r2, g2, b2; |
| 904 |
int *thisline, *nextline, *thisptr, *nextptr, *tmpptr; |
| 905 |
int i, j, rerr, gerr, berr, pwide3; |
| 906 |
int imax, jmax; |
| 907 |
int key; |
| 908 |
long cnt1, cnt2; |
| 909 |
int error[512]; |
| 910 |
|
| 911 |
|
| 912 |
for (i = j = 0; i <= 0x40; i++, j++) { |
| 913 |
error[256 + i] = j; |
| 914 |
error[256 - i] = -j; |
| 915 |
} |
| 916 |
for ( ; i < 0x80; i++, j += !(i & 1) ? 1 : 0) { |
| 917 |
error[256 + i] = j; |
| 918 |
error[256 - i] = -j; |
| 919 |
} |
| 920 |
for ( ; i <= 0xff; i++) { |
| 921 |
error[256 + i] = j; |
| 922 |
error[256 - i] = -j; |
| 923 |
} |
| 924 |
|
| 925 |
cnt1 = cnt2 = 0; |
| 926 |
pwide3 = w * 3; |
| 927 |
imax = h - 1; |
| 928 |
jmax = w - 1; |
| 929 |
ep = (pic24) ? pic24 : pic8; |
| 930 |
|
| 931 |
|
| 932 |
newpic = Blt_Malloc((size_t) (w * h)); |
| 933 |
cache = Blt_Calloc((size_t) (2 << 14), sizeof(short)); |
| 934 |
thisline = Blt_Malloc(pwide3 * sizeof(int)); |
| 935 |
nextline = Blt_Malloc(pwide3 * sizeof(int)); |
| 936 |
if (!cache || !newpic || !thisline || !nextline) { |
| 937 |
if (newpic) |
| 938 |
Blt_Free(newpic); |
| 939 |
if (cache) |
| 940 |
Blt_Free(cache); |
| 941 |
if (thisline) |
| 942 |
Blt_Free(thisline); |
| 943 |
if (nextline) |
| 944 |
Blt_Free(nextline); |
| 945 |
return (byte *) NULL; |
| 946 |
} |
| 947 |
np = newpic; |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
srcPtr = Blt_ColorImageBits(image), tempPtr = tempArr; |
| 952 |
for (x = 0; x < width; x++, tempPtr++, srcPtr--) { |
| 953 |
*tempPtr = *srcPtr; |
| 954 |
} |
| 955 |
|
| 956 |
for (y = 0; y < height; y++) { |
| 957 |
tempPtr = curRowPtr, curRowPtr = nextRowPtr, nextRowPtr = tempPtr; |
| 958 |
|
| 959 |
if (y != (height - 1)) { |
| 960 |
for (x = 0; x < width; x++, tempPtr++, srcPtr--) |
| 961 |
*tempPtr = *srcPtr; |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
Blt_Free(thisline); |
| 967 |
Blt_Free(nextline); |
| 968 |
Blt_Free(cache); |
| 969 |
|
| 970 |
return newpic; |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
static void |
| 975 |
DitherImage(image) |
| 976 |
Blt_ColorImage image; |
| 977 |
{ |
| 978 |
int width, height; |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
} |
| 983 |
|
| 984 |
#endif |
| 985 |
|
| 986 |
#endif |