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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
|
/*
* bltColor.c --
*
* This module contains routines for color allocation strategies
* used with color images in the BLT toolkit.
*
* Copyright 1997-1998 Lucent Technologies, Inc.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that the copyright notice and warranty
* disclaimer appear in supporting documentation, and that the names
* of Lucent Technologies any of their entities not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission.
*
* Lucent Technologies disclaims all warranties with regard to this
* software, including all implied warranties of merchantability and
* fitness. In no event shall Lucent Technologies be liable for any
* special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether in
* an action of contract, negligence or other tortuous action, arising
* out of or in connection with the use or performance of this
* software.
*/
/*
* Color strategies of 8-bit visuals:
*
* Try to "best" represent an N-color image into 8-bit (256 color)
* colormap. The simplest method is use the high bits of each RGB
* value (3 bits for red and green, 2 bits for blue). But this
* produces lots of contouring since the distribution of colors tends
* to be clustered. Other problems: probably can't allocate even 256
* colors. Other applications will have already taken some color
* slots. Furthermore, we might be displaying several images, and we
* can't assume that all images are representative of the colors used.
*
* If we use a private colormap, we may want to allocate some number
* of colors from the default colormap to prevent flashing when
* colormaps are switched.
*
* Switches:
*
* -exact boolean Try to match the colors of the image rather
* then generating a "best" color ramp.
*
* -threshold value Maximum average error. Indicates how far
* to reduce the quantized color palette.
*
* -tolerance value Allow colors within this distance to match.
* This will weight allocation towards harder
* to match colors, rather than the most
* frequent.
*
* -mincolors number Minimum number of reduced quantized colors.
* or color ramp.
*
* -dither boolean Turn on/off Floyd-Steinberg dithering.
*
* -keep number Hint to keep the first N colors in the
* in the default colormap. This only applies to
* private colormaps.
*
* -ramp number Number of colors to use in a linear ramp.
*
*/
#include "bltInt.h"
#ifndef WIN32
#include "bltHash.h"
#include "bltImage.h"
#define NCOLORS 256
#if 0
static void
GetPaletteSizes(nColors, nRedsPtr, nGreensPtr, nBluesPtr)
int nColors; /* Number of colors requested. */
unsigned int *nRedsPtr; /* (out) Number of red components. */
unsigned int *nGreensPtr; /* (out) Number of green components. */
unsigned int *nBluesPtr; /* (out) Number of blue components. */
{
unsigned int nBlues, nReds, nGreens;
assert(nColors > 1);
nBlues = nReds = nGreens = 0;
while ((nBlues * nBlues * nBlues) <= nColors) {
nBlues++;
}
nBlues--;
while ((nReds * nReds * nBlues) <= nColors) {
nReds++;
}
nReds--;
nGreens = nColors / (nBlues * nReds);
*nRedsPtr = nReds;
*nGreensPtr = nGreens;
*nBluesPtr = nBlues;
}
static void
BuildColorRamp(palettePtr, nColors)
Pix32 *palettePtr;
int nColors;
{
register unsigned int r, g, b;
unsigned int short red, green, blue;
unsigned int nReds, nGreens, nBlues;
GetPaletteSizes(nColors, &nReds, &nGreens, &nBlues);
for (r = 0; r < nReds; r++) {
red = (r * USHRT_MAX) / (nReds - 1);
for (g = 0; g < nGreens; g++) {
green = (g * USHRT_MAX) / (nGreens - 1);
for (b = 0; b < nBlues; b++) {
blue = (b * USHRT_MAX) / (nBlues - 1);
palettePtr->Red = red;
palettePtr->Green = green;
palettePtr->Blue = blue;
palettePtr++;
}
}
}
}
#endif
/*
*----------------------------------------------------------------------
*
* QueryColormap --
*
* This is for psuedo-color displays only. Fills an array or
* XColors with the color values (RGB and pixel) currently
* allocated in the colormap.
*
* Results:
* The number of colors allocated is returned. The array "colorArr"
* will contain the XColor values of each color in the colormap.
*
*----------------------------------------------------------------------
*/
static int
QueryColormap(display, colorMap, mapColors, numMapColorsPtr)
Display *display;
Colormap colorMap;
XColor mapColors[];
int *numMapColorsPtr;
{
unsigned long int pixelValues[NCOLORS];
int numAvail, numMapColors;
register int i;
register XColor *colorPtr;
register unsigned long *indexPtr;
int inUse[NCOLORS];
/* Initially, we assume all color cells are allocated. */
memset((char *)inUse, 0, sizeof(int) * NCOLORS);
/*
* Start allocating color cells. This will tell us which color cells
* haven't already been allocated in the colormap. We'll release the
* cells as soon as we find out how many there are.
*/
numAvail = 0;
for (indexPtr = pixelValues, i = 0; i < NCOLORS; i++, indexPtr++) {
if (!XAllocColorCells(display, colorMap, False, NULL, 0, indexPtr, 1)) {
break;
}
inUse[*indexPtr] = TRUE;/* Indicate the cell is unallocated */
numAvail++;
}
XFreeColors(display, colorMap, pixelValues, numAvail, 0);
/*
* Put the indices of the cells already allocated into a color array.
* We'll use the array to query the RGB values of the allocated colors.
*/
numMapColors = 0;
colorPtr = mapColors;
for (i = 0; i < NCOLORS; i++) {
if (!inUse[i]) {
colorPtr->pixel = i;
colorPtr->flags = (DoRed | DoGreen | DoBlue);
colorPtr++, numMapColors++;
}
}
XQueryColors(display, colorMap, mapColors, numMapColors);
*numMapColorsPtr = numMapColors;
#ifdef notdef
fprintf(stderr, "Number of colors (allocated/free) %d/%d\n", numMapColors,
numAvail);
#endif
return numAvail;
}
#if 0
static void
FindClosestColor(colorPtr, mapColors, numMapColors)
ColorInfo *colorPtr;
XColor mapColors[];
int numMapColors;
{
double r, g, b;
register int i;
double dist, min;
XColor *lastMatch;
register XColor *mapColorPtr;
min = DBL_MAX; /* Any color is closer. */
lastMatch = NULL;
/* Linear search of color */
mapColorPtr = mapColors;
for (i = 0; i < numMapColors; i++, mapColorPtr++) {
r = (double)mapColorPtr->red - (double)colorPtr->exact.red;
g = (double)mapColorPtr->green - (double)colorPtr->exact.green;
b = (double)mapColorPtr->blue - (double)colorPtr->exact.blue;
dist = (r * r) + (b * b) + (g * g);
if (dist < min) {
min = dist;
lastMatch = mapColorPtr;
}
}
colorPtr->best = *lastMatch;
colorPtr->best.flags = (DoRed | DoGreen | DoBlue);
colorPtr->error = (float)sqrt(min);
}
static int
CompareColors(a, b)
void *a, *b;
{
ColorInfo *i1Ptr, *i2Ptr;
i1Ptr = *(ColorInfo **) a;
i2Ptr = *(ColorInfo **) b;
if (i2Ptr->error > i1Ptr->error) {
return 1;
} else if (i2Ptr->error < i1Ptr->error) {
return -1;
}
return 0;
}
static float
MatchColors(colorTabPtr, rgbPtr, numColors, numAvailColors, numMapColors,
mapColors)
struct ColorTableStruct *colorTabPtr;
Pix32 *rgbPtr;
int numColors;
int numAvailColors;
int numMapColors;
XColor mapColors[NCOLORS];
{
int numMatched;
float sum;
register int i;
register ColorInfo *colorPtr;
/*
* For each quantized color, compute and store the error (i.e
* the distance from a color that's already been allocated).
* We'll use this information to sort the colors based upon how
* badly they match and their frequency to the color image.
*/
colorPtr = colorTabPtr->colorInfo;
for (i = 0; i < numColors; i++, colorPtr++, rgbPtr++) {
colorPtr->index = i;
colorTabPtr->sortedColors[i] = colorPtr;
colorPtr->exact.red = rgbPtr->Red;
colorPtr->exact.green = rgbPtr->Green;
colorPtr->exact.blue = rgbPtr->Blue;
colorPtr->exact.flags = (DoRed | DoGreen | DoBlue);
FindClosestColor(colorPtr, mapColors, numMapColors);
}
/* Sort the colors, first by frequency (most to least), then by
* matching error (worst to best).
*/
qsort(colorTabPtr->sortedColors, numColors, sizeof(ColorInfo *),
(QSortCompareProc *)CompareColors);
for (i = 0; i < numColors; i++) {
colorPtr = colorTabPtr->sortedColors[i];
fprintf(stderr, "%d. %04x%04x%04x / %04x%04x%04x = %f (%d)\n", i,
colorPtr->exact.red, colorPtr->exact.green, colorPtr->exact.blue,
colorPtr->best.red, colorPtr->best.green, colorPtr->best.blue,
colorPtr->error, colorPtr->freq);
}
sum = 0.0;
numMatched = 0;
for (i = numAvailColors; i < numColors; i++) {
colorPtr = colorTabPtr->sortedColors[i];
sum += colorPtr->error;
numMatched++;
}
if (numMatched > 0) {
sum /= numMatched;
}
return sum;
}
static int
AllocateColors(nImageColors, colorTabPtr, matchOnly)
int nImageColors;
struct ColorTableStruct *colorTabPtr;
int matchOnly;
{
register int i;
register ColorInfo *colorPtr;
unsigned long int pixelValue;
for (i = 0; i < nImageColors; i++) {
colorPtr = colorTabPtr->sortedColors[i];
if (matchOnly) {
XAllocColor(colorTabPtr->display, colorTabPtr->colorMap,
&colorPtr->best);
pixelValue = colorPtr->best.pixel;
} else {
colorPtr->allocated = XAllocColor(colorTabPtr->display,
colorTabPtr->colorMap, &colorPtr->exact);
if (colorPtr->allocated) {
pixelValue = colorPtr->exact.pixel;
} else {
XAllocColor(colorTabPtr->display, colorTabPtr->colorMap,
&colorPtr->best);
pixelValue = colorPtr->best.pixel;
}
}
colorTabPtr->pixelValues[colorPtr->index] = pixelValue;
}
colorTabPtr->nPixels = nImageColors;
return 1;
}
#endif
ColorTable
Blt_CreateColorTable(tkwin)
Tk_Window tkwin;
{
XVisualInfo visualInfo, *visualInfoPtr;
int nVisuals;
Visual *visualPtr;
Display *display;
struct ColorTableStruct *colorTabPtr;
display = Tk_Display(tkwin);
visualPtr = Tk_Visual(tkwin);
colorTabPtr = Blt_Calloc(1, sizeof(struct ColorTableStruct));
assert(colorTabPtr);
colorTabPtr->display = Tk_Display(tkwin);
colorTabPtr->colorMap = Tk_Colormap(tkwin);
visualInfo.screen = Tk_ScreenNumber(tkwin);
visualInfo.visualid = XVisualIDFromVisual(visualPtr);
visualInfoPtr = XGetVisualInfo(display, VisualScreenMask | VisualIDMask,
&visualInfo, &nVisuals);
colorTabPtr->visualInfo = *visualInfoPtr;
XFree(visualInfoPtr);
return colorTabPtr;
}
void
Blt_FreeColorTable(colorTabPtr)
struct ColorTableStruct *colorTabPtr;
{
if (colorTabPtr == NULL) {
return;
}
if (colorTabPtr->nPixels > 0) {
XFreeColors(colorTabPtr->display, colorTabPtr->colorMap,
colorTabPtr->pixelValues, colorTabPtr->nPixels, 0);
}
Blt_Free(colorTabPtr);
}
extern int redAdjust, greenAdjust, blueAdjust;
extern int redMaskShift, greenMaskShift, blueMaskShift;
/*
*----------------------------------------------------------------------
*
* Blt_DirectColorTable --
*
* Creates a color table using the DirectColor visual. We try
* to allocate colors across the color spectrum.
*
* Results:
*
*
*----------------------------------------------------------------------
*/
/*ARGSUSED*/
ColorTable
Blt_DirectColorTable(interp, tkwin, image)
Tcl_Interp *interp;
Tk_Window tkwin;
Blt_ColorImage image;
{
struct ColorTableStruct *colorTabPtr;
Visual *visualPtr;
Display *display;
XColor color;
int nr, ng, nb;
int rBand, gBand, bBand;
int rLast, gLast, bLast;
unsigned int r, g, b;
unsigned int value;
register int i;
display = Tk_Display(tkwin);
visualPtr = Tk_Visual(tkwin);
colorTabPtr = Blt_CreateColorTable(tkwin);
/*
* Compute the number of distinct colors in each band
*/
nr = ((unsigned int)visualPtr->red_mask >> redMaskShift) + 1;
ng = ((unsigned int)visualPtr->green_mask >> greenMaskShift) + 1;
nb = ((unsigned int)visualPtr->blue_mask >> blueMaskShift) + 1;
#ifdef notdef
assert((nr <= visualPtr->map_entries) && (ng <= visualPtr->map_entries) &&
(nb <= visualPtr->map_entries));
#endif
rBand = NCOLORS / nr;
gBand = NCOLORS / ng;
bBand = NCOLORS / nb;
retry:
color.flags = (DoRed | DoGreen | DoBlue);
rLast = gLast = bLast = 0;
r = g = b = 0;
for (i = 0; i < visualPtr->map_entries; i++) {
if (rLast < NCOLORS) {
r = rLast + rBand;
if (r > NCOLORS) {
r = NCOLORS;
}
}
if (gLast < NCOLORS) {
g = gLast + gBand;
if (g > NCOLORS) {
g = NCOLORS;
}
}
if (bLast < NCOLORS) {
b = bLast + bBand;
if (b > NCOLORS) {
b = NCOLORS;
}
}
color.red = (r - 1) * (NCOLORS + 1);
color.green = (g - 1) * (NCOLORS + 1);
color.blue = (b - 1) * (NCOLORS + 1);
if (!XAllocColor(display, colorTabPtr->colorMap, &color)) {
XFreeColors(display, colorTabPtr->colorMap,
colorTabPtr->pixelValues, i, 0);
if ((colorTabPtr->flags & PRIVATE_COLORMAP) == 0) {
/*
* If we can't allocate a color in the default
* colormap, try again, this time with a private
* colormap.
*/
fprintf(stderr, "Need to allocate private colormap\n");
colorTabPtr->colorMap = Tk_GetColormap(interp, tkwin, ".");
XSetWindowColormap(display, Tk_WindowId(tkwin),
colorTabPtr->colorMap);
colorTabPtr->flags |= PRIVATE_COLORMAP;
goto retry;
}
#ifdef notdef
fprintf(stderr, "Failed to allocate after %d colors\n", i);
#endif
Blt_Free(colorTabPtr);
return NULL; /* Ran out of colors in private map? */
}
colorTabPtr->pixelValues[i] = color.pixel;
/*
* Fill in pixel values for each band at this intensity
*/
value = color.pixel & visualPtr->red_mask;
while (rLast < r) {
colorTabPtr->red[rLast++] = value;
}
value = color.pixel & visualPtr->green_mask;
while (gLast < g) {
colorTabPtr->green[gLast++] = value;
}
value = color.pixel & visualPtr->blue_mask;
while (bLast < b) {
colorTabPtr->blue[bLast++] = value;
}
}
colorTabPtr->nPixels = i;
return colorTabPtr;
}
#if 0
/*
* First attempt:
* Allocate colors all the colors in the image (up to NCOLORS). Bail out
* on the first failure or if we need more than NCOLORS.
*/
static int
GetUniqueColors(image)
Blt_ColorImage image;
{
register int i, nColors;
register Pix32 *pixelPtr;
Pix32 color;
Blt_HashEntry *hPtr;
int isNew, nPixels;
int refCount;
Blt_HashTable colorTable;
Blt_InitHashTable(&colorTable, BLT_ONE_WORD_KEYS);
nPixels = Blt_ColorImageWidth(image) * Blt_ColorImageHeight(image);
nColors = 0;
pixelPtr = Blt_ColorImageBits(image);
for (i = 0; i < nPixels; i++, pixelPtr++) {
color.value = pixelPtr->value;
color.Alpha = 0xFF; /* Ignore alpha-channel values */
hPtr = Blt_CreateHashEntry(&colorTable, (char *)color.value, &isNew);
if (isNew) {
refCount = 1;
nColors++;
} else {
refCount = (int)Blt_GetHashValue(hPtr);
refCount++;
}
Blt_SetHashValue(hPtr, (ClientData)refCount);
}
Blt_DeleteHashTable(&colorTable);
return nColors;
}
#endif
#define Blt_DefaultColormap(tkwin) \
DefaultColormap(Tk_Display(tkwin), Tk_ScreenNumber(tkwin))
static void
PrivateColormap(interp, colorTabPtr, image, tkwin)
Tcl_Interp *interp;
struct ColorTableStruct *colorTabPtr;
Blt_ColorImage image;
Tk_Window tkwin;
{
int keepColors = 0;
register int i;
XColor usedColors[NCOLORS];
int nUsedColors;
Colormap colorMap;
int inUse[NCOLORS];
XColor *colorPtr;
/* XColor *imageColors; */
/*
* Create a private colormap if one doesn't already exist for the
* window.
*/
colorTabPtr->colorMap = colorMap = Tk_Colormap(tkwin);
nUsedColors = 0; /* Number of colors allocated */
if (colorTabPtr->nPixels > 0) {
XFreeColors(colorTabPtr->display, colorTabPtr->colorMap,
colorTabPtr->pixelValues, colorTabPtr->nPixels, 0);
}
QueryColormap(colorTabPtr->display, colorMap, usedColors,
&nUsedColors);
memset((char *)inUse, 0, sizeof(int) * NCOLORS);
if ((nUsedColors == 0) && (keepColors > 0)) {
/*
* We're starting with a clean colormap so find out what colors
* have been used in the default colormap.
*/
QueryColormap(colorTabPtr->display,
Blt_DefaultColormap(tkwin), usedColors, &nUsedColors);
/*
* Copy a number of colors from the default colormap into the private
* colormap. We can assume that this is the working set from most
* (non-image related) applications. While this doesn't stop our
* image from flashing and looking dumb when colormaps are swapped
* in and out, at least everything else should remain unaffected.
*/
if (nUsedColors > keepColors) {
nUsedColors = keepColors;
}
/*
* We want to allocate colors in the same ordering as the old colormap,
* and we can't assume that the colors in the old map were contiguous.
* So mark the colormap locations (i.e. pixels) that we find in use.
*/
}
for (colorPtr = usedColors, i = 0; i < nUsedColors; i++, colorPtr++) {
inUse[colorPtr->pixel] = TRUE;
}
/*
* In an "exact" colormap, we try to allocate as many of colors from the
* image as we can fit. If necessary, we'll cheat and reduce the number
* of colors by quantizing.
*/
/* imageColors = usedColors + nUsedColors; */
Tk_SetWindowColormap(tkwin, colorMap);
}
ColorTable
Blt_PseudoColorTable(interp, tkwin, image)
Tcl_Interp *interp;
Tk_Window tkwin;
Blt_ColorImage image;
{
struct ColorTableStruct *colorTabPtr;
Colormap defColorMap;
int usePrivate;
colorTabPtr = Blt_CreateColorTable(tkwin);
defColorMap = DefaultColormap(colorTabPtr->display, Tk_ScreenNumber(tkwin));
if (colorTabPtr->colorMap == defColorMap) {
fprintf(stderr, "Using default colormap\n");
}
/* All other visuals use an 8-bit colormap */
colorTabPtr->lut = Blt_Malloc(sizeof(unsigned int) * 33 * 33 * 33);
assert(colorTabPtr->lut);
usePrivate = TRUE;
if (usePrivate) {
PrivateColormap(interp, colorTabPtr, image, tkwin);
} else {
#ifdef notdef
ReadOnlyColormap(colorTabPtr, image, tkwin);
#endif
}
return colorTabPtr;
}
#ifdef notdef
static void
ConvoleColorImage(srcImage, destImage, kernelPtr)
Blt_ColorImage srcImage, destImage;
ConvoleKernel *kernelPtr;
{
Pix32 *srcPtr, *destPtr;
Pix32 *src[MAXROWS];
register int x, y, i, j;
int red, green, blue;
/* i = 0 case, ignore left column of pixels */
srcPtr = Blt_ColorImageBits(srcImage);
destPtr = Blt_ColorImageBits(destImage);
width = Blt_ColorImageWidth(srcImage);
height = Blt_ColorImageHeight(srcImage);
yOffset = kernelPtr->height / 2;
xOffset = kernelPtr->width / 2;
for (y = yOffset; y < (height - yOffset); y++) {
/* Set up pointers to individual rows */
for (i = 0; i < kernelPtr->height; i++) {
src[i] = srcPtr + (i * width);
}
for (x = xOffset; x < (width - xOffset); x++) {
red = green = blue = 0;
kernPtr = kernelPtr->values;
for (i = 0; i < kernelPtr->height; i++) {
for (j = 0; j < kernelPtr->width; j++) {
red += *valuePtr * src[i][j].Red;
green += *valuePtr * src[i][j].Green;
blue += *valuePtr * src[i][j].Blue;
valuePtr++;
}
}
destPtr->Red = red / kernelPtr->sum;
destPtr->Green = green / kernelPtr->sum;
destPtr->Blue = blue / kernelPtr->sum;
destPtr++;
}
srcPtr += width;
}
sum = bot[0].Red +
red = bot[0].Red + bot[1].Red + mid[1].Red + top[0].Red + top[1].Red;
green = bot[0].Green + bot[1].Green + mid[1].Green + top[0].Green +
top[1].Green;
blue = bot[0].Blue + bot[1].Blue + mid[1].Blue + top[0].Blue + top[1].Blue;
error = (red / 5) - mid[0].Red;
redVal = mid[0].Red - (error * blend / blend_divisor);
error = (green / 5) - mid[0].Green;
greenVal = mid[0].Green - (error * blend / blend_divisor);
error = (blue / 5) - mid[0].Blue;
blueVal = mid[0].Blue - (error * blend / blend_divisor);
out[0].Red = CLAMP(redVal);
out[0].Green = CLAMP(greenVal);
out[0].Blue = CLAMP(blueVal);
for (i = 1; i < (width - 1); i++) {
for (chan = 0; chan < 3; chan++) {
total = bot[chan][i - 1] + bot[chan][i] + bot[chan][i + 1] +
mid[chan][i - 1] + mid[chan][i + 1] +
top[chan][i - 1] + top[chan][i] + top[chan][i + 1];
avg = total >> 3; /* divide by 8 */
diff = avg - mid[chan][i];
result = mid[chan][i] - (diff * blend / blend_divisor);
out[chan][i] = CLAMP(result);
}
}
/* i = in_hdr.xmax case, ignore right column of pixels */
for (chan = 0; chan < 3; chan++) {
total = bot[chan][i - 1] + bot[chan][i] +
mid[chan][i - 1] +
top[chan][i - 1] + top[chan][i];
avg = total / 5;
diff = avg - mid[chan][i];
result = mid[chan][i] - (diff * blend / blend_divisor);
out[chan][i] = CLAMP(result);
}
}
static void
DitherRow(srcImage, destImage, lastRow, curRow)
Blt_ColorImage srcImage, destImage;
int width, height;
int bottom, top;
{
int width, height;
width = Blt_ColorImageWidth(srcImage);
topPtr = Blt_ColorImageBits(destPtr) + (width * row);
rowPtr = topPtr + width;
botPtr = rowPtr + width;
for (x = 0; x < width; x++) {
/* Clamp current error entry */
midPtr->red = CLAMP(midPtr->red);
midPtr->blue = CLAMP(midPtr->blue);
midPtr->green = CLAMP(midPtr->green);
r = (midPtr->red >> 3) + 1;
g = (midPtr->green >> 3) + 1;
b = (midPtr->blue >> 3) + 1;
index = colorTabPtr->lut[r][g][b];
redVal = midPtr->red * (NCOLORS + 1);
greenVal = midPtr->green * (NCOLORS + 1);
blueVal = midPtr->blue * (NCOLORS + 1);
error = colorVal - colorMap[index].red;
if (x < 511) {
currRow[x + 1].Red = currRow[x + 1].Red + 7 * error / 16;
nextRow[x + 1].Red = nextRow[x + 1].Red + error / 16;
}
nextRow[x].Red = nextRow[x].Red + 5 * error / 16;
if (x > 0) {
nextRow[x - 1].Red = nextRow[x - 1].Red + 3 * error / 16;
}
error = row[x][c] - colormap[index][c];
value = srcPtr->channel[i] * error[i];
value = CLAMP(value);
destPtr->channel[i] = value;
/* Closest pixel */
pixel = PsuedoColorPixel();
error[RED] = colorPtr->Red - srcPtr->Red * (NCOLORS + 1);
/* translate pixel to colorInfoPtr to get error */
colorTabPtr->lut[r][g][b];
colorPtr = PixelToColorInfo(pixel);
error = colorPtr->error;
register rle_pixel *optr;
register int j;
register short *thisptr, *nextptr = NULL;
int chan;
static int nchan = 0;
int lastline = 0, lastpixel;
static int *cval = 0;
static rle_pixel *pixel = 0;
if (nchan != in_hdr->ncolors)
if (cval) {
Blt_Free(cval);
Blt_Free(pixel);
}
nchan = in_hdr->ncolors;
if (!cval) {
if ((cval = Blt_Malloc(nchan * sizeof(int))) == 0)
malloc_ERR;
if ((pixel = Blt_Malloc(nchan * sizeof(rle_pixel))) == 0)
malloc_ERR;
}
optr = outrow[RLE_RED];
thisptr = row_top;
if (row_bottom)
nextptr = row_bottom;
else
lastline = 1;
for (x = 0; x < width; x++) {
int cmap_index = 0;
lastpixel = (x == (width - 1));
val = srcPtr->Red;
for (chan = 0; chan < 3; chan++) {
cval[chan] = *thisptr++;
/*
* Current channel value has been accumulating error,
* it could be out of range.
*/
if (cval[chan] < 0)
cval[chan] = 0;
else if (cval[chan] > 255)
cval[chan] = 255;
pixel[chan] = cval[chan];
}
/* find closest color */
find_closest(map, nchan, maplen, pixel, &cmap_index);
*optr++ = cmap_index;
/* thisptr is now looking at pixel to the right of current pixel
* nextptr is looking at pixel below current pixel
* So, increment thisptr as stuff gets stored. nextptr gets moved
* by one, and indexing is done +/- nchan.
*/
for (chan = 0; chan < nchan; chan++) {
cval[chan] -= map[chan][cmap_index];
if (!lastpixel) {
thisptr[chan] += cval[chan] * 7 / 16;
}
if (!lastline) {
if (j != 0) {
nextptr[-nchan] += cval[chan] * 3 / 16;
}
nextptr[0] += cval[chan] * 5 / 16;
if (!lastpixel) {
nextptr[nchan] += cval[chan] / 16;
}
nextptr++;
}
}
}
}
}
/********************************************/
static Blt_ColorImage
DoColorDither(pic24, pic8, w, h, rmap, gmap, bmap, rdisp, gdisp, bdisp, maplen)
byte *pic24, *pic8, *rmap, *gmap, *bmap, *rdisp, *gdisp, *bdisp;
int w, h, maplen;
{
/* takes a 24 bit picture, of size w*h, dithers with the colors in
rdisp, gdisp, bdisp (which have already been allocated),
and generates an 8-bit w*h image, which it returns.
ignores input value 'pic8'
returns NULL on error
note: the rdisp,gdisp,bdisp arrays should be the 'displayed' colors,
not the 'desired' colors
if pic24 is NULL, uses the passed-in pic8 (an 8-bit image) as
the source, and the rmap,gmap,bmap arrays as the desired colors */
byte *np, *ep, *newpic;
short *cache;
int r2, g2, b2;
int *thisline, *nextline, *thisptr, *nextptr, *tmpptr;
int i, j, rerr, gerr, berr, pwide3;
int imax, jmax;
int key;
long cnt1, cnt2;
int error[512]; /* -255 .. 0 .. +255 */
/* compute somewhat non-linear floyd-steinberg error mapping table */
for (i = j = 0; i <= 0x40; i++, j++) {
error[256 + i] = j;
error[256 - i] = -j;
}
for ( /*empty*/ ; i < 0x80; i++, j += !(i & 1) ? 1 : 0) {
error[256 + i] = j;
error[256 - i] = -j;
}
for ( /*empty*/ ; i <= 0xff; i++) {
error[256 + i] = j;
error[256 - i] = -j;
}
cnt1 = cnt2 = 0;
pwide3 = w * 3;
imax = h - 1;
jmax = w - 1;
ep = (pic24) ? pic24 : pic8;
/* attempt to malloc things */
newpic = Blt_Malloc((size_t) (w * h));
cache = Blt_Calloc((size_t) (2 << 14), sizeof(short));
thisline = Blt_Malloc(pwide3 * sizeof(int));
nextline = Blt_Malloc(pwide3 * sizeof(int));
if (!cache || !newpic || !thisline || !nextline) {
if (newpic)
Blt_Free(newpic);
if (cache)
Blt_Free(cache);
if (thisline)
Blt_Free(thisline);
if (nextline)
Blt_Free(nextline);
return (byte *) NULL;
}
np = newpic;
/* Get first line of picture in reverse order. */
srcPtr = Blt_ColorImageBits(image), tempPtr = tempArr;
for (x = 0; x < width; x++, tempPtr++, srcPtr--) {
*tempPtr = *srcPtr;
}
for (y = 0; y < height; y++) {
tempPtr = curRowPtr, curRowPtr = nextRowPtr, nextRowPtr = tempPtr;
if (y != (height - 1)) {/* get next line */
for (x = 0; x < width; x++, tempPtr++, srcPtr--)
*tempPtr = *srcPtr;
}
}
Blt_Free(thisline);
Blt_Free(nextline);
Blt_Free(cache);
return newpic;
}
static void
DitherImage(image)
Blt_ColorImage image;
{
int width, height;
}
#endif
#endif /* WIN32 */
|