File: color_mapper.C

package info (click to toggle)
xjig 2.4-14.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 892 kB
  • sloc: cpp: 10,177; makefile: 1,142; perl: 23
file content (154 lines) | stat: -rw-r--r-- 3,345 bytes parent folder | download | duplicates (9)
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

#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>

#ifndef __color_mapper_h
#	include "color_mapper.H"
#endif

ColorMapper::ColorMapper(Display *dpy_in)
: dpy(dpy_in)
{
Screen	*screen = DefaultScreenOfDisplay(dpy);

	mymap=DefaultColormapOfScreen(screen);
	cells=0;
	colors=0;
	if (DefaultVisualOfScreen(screen)->c_class!=PseudoColor)		return;

	cells=CellsOfScreen(screen);
	colors      = new XColor[cells];
	setup_usage();
}

ColorMapper::~ColorMapper() {
	free_usage();
	if (colors)			delete [] colors;
}

void ColorMapper::free_usage() {
int i;

	for (i=0;i<cells;i++) {
		if (colors[i].flags) {
   		XFreeColors(dpy,mymap,&colors[i].pixel,1,0L);
   	}
	}
}

void ColorMapper::setup_usage() {
int i;
unsigned long	*help;

	for (i=0;i<cells;i++)	colors[i].pixel = i;
	XQueryColors(dpy,mymap,colors,cells);
	for (i=0;i<cells;i++)	colors[i].flags = DoRed | DoGreen | DoBlue;

	help = new unsigned long[cells];
/*
 * allocate unused cells of the colormap for read/write
 */
   i = 0;
   while(XAllocColorCells( dpy, mymap, False, 0L, 0, &help[i], 1 )) {
   	colors[help[i]].flags = 0;		// mark unusable for shared color
   	i++;
   }

/*
 * free the pixels again
 */
   XFreeColors(dpy,mymap,help,i,0L);
   delete [] help;
   XSync( dpy, 0 );

   for (i=0;i<cells;i++) {
   	if (colors[i].flags) {		// still not invalidated
   	// all shareable colors are again allocated !!
   		if (XAllocColor(dpy,mymap,&colors[i])) {
   			if ((unsigned)i!=colors[i].pixel) {
   				XFreeColors(dpy,mymap,&colors[i].pixel,1,0L);
   				colors[i].flags=0;		// different pixel -> not usefull
   			}
   		}
   		else {
   				colors[i].flags=0;		// not allocatable -> not usefull
   		}
   	}
   }
   return;
}

unsigned long ColorMapper::alloc_color(XColor *def) {
int	i;
long	min_dist;
int	min_i;

	if (!colors) {
		def->flags=DoRed | DoGreen | DoBlue;
		if (!XAllocColor(dpy,mymap,def)) {
			fprintf( stderr, "\n*** failed to allocated color on '%s'\n\n", DisplayString(dpy) );
			exit(0);
		}
		return def->pixel;
	}

	min_i=-1;
	min_dist=0;
	for (i=0;i<cells;i++) {
		if (colors[i].flags) {
			long rd = ((long)colors[i].red   - (long)def->red)/4;
			long gd = ((long)colors[i].green - (long)def->green)/4;
			long bd = ((long)colors[i].blue  - (long)def->blue)/4;
			long dist=rd*rd+gd*gd+bd*bd;

			if (min_i<0 || dist<min_dist) {
				min_dist = dist;
				min_i    = i;
			}
		}
	}

// reuse already allocated color, when possible
	if (min_i>=0 && min_dist<10000) {
		return colors[min_i].pixel;
	}

// allocate additional entry for that pixel
	def->flags=DoRed | DoGreen | DoBlue;
	if (XAllocColor(dpy,mymap,def)) {
		colors[def->pixel] = *def;
		return def->pixel;
	}

// allocate the closest entry
	if (min_i>=0) {
		return colors[min_i].pixel;
	}

// everything else failed ...
	fprintf( stderr, "can't handle colormap overflow ...\n" );
	exit(0);
	return 0;
}

unsigned long ColorMapper::alloc_named_color( const char *name ) {
XColor   def;
	if (!XLookupColor(dpy,mymap,name,&def,&def )) {
		fprintf( stderr, "\n*** failed to query color '%s'\n\n", name );
		exit(0);
	}
	def.flags = DoRed | DoGreen | DoBlue;
	return alloc_color(&def);
}

// ============================================================================

Port::Port(Display *dpy_in) {
	dpy = dpy_in;
	mapper = new ColorMapper(dpy);
}

Port::~Port() {
	delete mapper;
}