Debian Codegraph @

generic/bltColor.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1
2 /*
3 * bltColor.c --
4 *
5 * This module contains routines for color allocation strategies
6 * used with color images in the BLT toolkit.
7 *
8 * Copyright 1997-1998 Lucent Technologies, Inc.
9 *
10 * Permission to use, copy, modify, and distribute this software and
11 * its documentation for any purpose and without fee is hereby
12 * granted, provided that the above copyright notice appear in all
13 * copies and that both that the copyright notice and warranty
14 * disclaimer appear in supporting documentation, and that the names
15 * of Lucent Technologies any of their entities not be used in
16 * advertising or publicity pertaining to distribution of the software
17 * without specific, written prior permission.
18 *
19 * Lucent Technologies disclaims all warranties with regard to this
20 * software, including all implied warranties of merchantability and
21 * fitness. In no event shall Lucent Technologies be liable for any
22 * special, indirect or consequential damages or any damages
23 * whatsoever resulting from loss of use, data or profits, whether in
24 * an action of contract, negligence or other tortuous action, arising
25 * out of or in connection with the use or performance of this
26 * software.
27 */
28
29 /*
30 * Color strategies of 8-bit visuals:
31 *
32 * Try to "best" represent an N-color image into 8-bit (256 color)
33 * colormap. The simplest method is use the high bits of each RGB
34 * value (3 bits for red and green, 2 bits for blue). But this
35 * produces lots of contouring since the distribution of colors tends
36 * to be clustered. Other problems: probably can't allocate even 256
37 * colors. Other applications will have already taken some color
38 * slots. Furthermore, we might be displaying several images, and we
39 * can't assume that all images are representative of the colors used.
40 *
41 * If we use a private colormap, we may want to allocate some number
42 * of colors from the default colormap to prevent flashing when
43 * colormaps are switched.
44 *
45 * Switches:
46 *
47 * -exact boolean Try to match the colors of the image rather
48 * then generating a "best" color ramp.
49 *
50 * -threshold value Maximum average error. Indicates how far
51 * to reduce the quantized color palette.
52 *
53 * -tolerance value Allow colors within this distance to match.
54 * This will weight allocation towards harder
55 * to match colors, rather than the most
56 * frequent.
57 *
58 * -mincolors number Minimum number of reduced quantized colors.
59 * or color ramp.
60 *
61 * -dither boolean Turn on/off Floyd-Steinberg dithering.
62 *
63 * -keep number Hint to keep the first N colors in the
64 * in the default colormap. This only applies to
65 * private colormaps.
66 *
67 * -ramp number Number of colors to use in a linear ramp.
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; /* Number of colors requested. */
84 unsigned int *nRedsPtr; /* (out) Number of red components. */
85 unsigned int *nGreensPtr; /* (out) Number of green components. */
86 unsigned int *nBluesPtr; /* (out) Number of blue components. */
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 * QueryColormap --
138 *
139 * This is for psuedo-color displays only. Fills an array or
140 * XColors with the color values (RGB and pixel) currently
141 * allocated in the colormap.
142 *
143 * Results:
144 * The number of colors allocated is returned. The array "colorArr"
145 * will contain the XColor values of each color in the colormap.
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 /* Initially, we assume all color cells are allocated. */
165 memset((char *)inUse, 0, sizeof(int) * NCOLORS);
166
167 /*
168 * Start allocating color cells. This will tell us which color cells
169 * haven't already been allocated in the colormap. We'll release the
170 * cells as soon as we find out how many there are.
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;/* Indicate the cell is unallocated */
178 numAvail++;
179 }
180 XFreeColors(display, colorMap, pixelValues, numAvail, 0);
181
182 /*
183 * Put the indices of the cells already allocated into a color array.
184 * We'll use the array to query the RGB values of the allocated colors.
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; /* Any color is closer. */
218 lastMatch = NULL;
219
220 /* Linear search of color */
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 * For each quantized color, compute and store the error (i.e
272 * the distance from a color that's already been allocated).
273 * We'll use this information to sort the colors based upon how
274 * badly they match and their frequency to the color image.
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 /* Sort the colors, first by frequency (most to least), then by
288 * matching error (worst to best).
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 * Blt_DirectColorTable --
399 *
400 * Creates a color table using the DirectColor visual. We try
401 * to allocate colors across the color spectrum.
402 *
403 * Results:
404 *
405 *
406 *----------------------------------------------------------------------
407 */
408 /*ARGSUSED*/
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 * Compute the number of distinct colors in each band
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 * If we can't allocate a color in the default
478 * colormap, try again, this time with a private
479 * colormap.
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; /* Ran out of colors in private map? */
494 }
495 colorTabPtr->pixelValues[i] = color.pixel;
496 /*
497 * Fill in pixel values for each band at this intensity
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 * First attempt:
519 * Allocate colors all the colors in the image (up to NCOLORS). Bail out
520 * on the first failure or if we need more than NCOLORS.
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; /* Ignore alpha-channel values */
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 /* XColor *imageColors; */
576
577 /*
578 * Create a private colormap if one doesn't already exist for the
579 * window.
580 */
581
582 colorTabPtr->colorMap = colorMap = Tk_Colormap(tkwin);
583
584 nUsedColors = 0; /* Number of colors allocated */
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 * We're starting with a clean colormap so find out what colors
597 * have been used in the default colormap.
598 */
599
•600 QueryColormap(colorTabPtr->display,
601 Blt_DefaultColormap(tkwin), usedColors, &nUsedColors);
602
603 /*
604 * Copy a number of colors from the default colormap into the private
605 * colormap. We can assume that this is the working set from most
606 * (non-image related) applications. While this doesn't stop our
607 * image from flashing and looking dumb when colormaps are swapped
608 * in and out, at least everything else should remain unaffected.
609 */
610
611 if (nUsedColors > keepColors) {
612 nUsedColors = keepColors;
613 }
614 /*
615 * We want to allocate colors in the same ordering as the old colormap,
616 * and we can't assume that the colors in the old map were contiguous.
617 * So mark the colormap locations (i.e. pixels) that we find in use.
618 */
619
620 }
621 for (colorPtr = usedColors, i = 0; i < nUsedColors; i++, colorPtr++) {
622 inUse[colorPtr->pixel] = TRUE;
623 }
624
625 /*
626 * In an "exact" colormap, we try to allocate as many of colors from the
627 * image as we can fit. If necessary, we'll cheat and reduce the number
628 * of colors by quantizing.
629 */
•630 /* imageColors = usedColors + nUsedColors; */
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 /* All other visuals use an 8-bit colormap */
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 /* i = 0 case, ignore left column of pixels */
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 /* Set up pointers to individual rows */
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; /* divide by 8 */
732 diff = avg - mid[chan][i];
733 result = mid[chan][i] - (diff * blend / blend_divisor);
734 out[chan][i] = CLAMP(result);
735 }
736 }
737 /* i = in_hdr.xmax case, ignore right column of pixels */
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 /* Clamp current error entry */
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 /* Closest pixel */
795 pixel = PsuedoColorPixel();
796 error[RED] = colorPtr->Red - srcPtr->Red * (NCOLORS + 1);
797
798 /* translate pixel to colorInfoPtr to get error */
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 * Current channel value has been accumulating error,
843 * it could be out of range.
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 /* find closest color */
854 find_closest(map, nchan, maplen, pixel, &cmap_index);
855 *optr++ = cmap_index;
856
857 /* thisptr is now looking at pixel to the right of current pixel
858 * nextptr is looking at pixel below current pixel
859 * So, increment thisptr as stuff gets stored. nextptr gets moved
860 * by one, and indexing is done +/- nchan.
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 /* takes a 24 bit picture, of size w*h, dithers with the colors in
890 rdisp, gdisp, bdisp (which have already been allocated),
891 and generates an 8-bit w*h image, which it returns.
892 ignores input value 'pic8'
893 returns NULL on error
894
895 note: the rdisp,gdisp,bdisp arrays should be the 'displayed' colors,
896 not the 'desired' colors
897
898 if pic24 is NULL, uses the passed-in pic8 (an 8-bit image) as
899 the source, and the rmap,gmap,bmap arrays as the desired colors */
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]; /* -255 .. 0 .. +255 */
910
911 /* compute somewhat non-linear floyd-steinberg error mapping table */
912 for (i = j = 0; i <= 0x40; i++, j++) {
913 error[256 + i] = j;
914 error[256 - i] = -j;
915 }
916 for ( /*empty*/ ; i < 0x80; i++, j += !(i & 1) ? 1 : 0) {
917 error[256 + i] = j;
918 error[256 - i] = -j;
919 }
920 for ( /*empty*/ ; 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 /* attempt to malloc things */
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 /* Get first line of picture in reverse order. */
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)) {/* get next line */
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 /* WIN32 */