summaryrefslogtreecommitdiff
path: root/src/Xw/Xw_get_color_index.cxx
blob: 53cc59b92b84dd619b6e9c32d25794bd0e889ff6 (plain)
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
#define PRO9517	/*GG_280897
		// Ameliorer le critere de comparaison de 2 couleurs
		// sinon ROUGE = VERT = BLEU !!!
*/

#include <Xw_Extension.h>

	/* ifdef then trace on */
#ifdef TRACE
#define TRACE_GET_COLOR_INDEX
#endif

/*
   STATUS Xw_get_color_index(acolormap,r,g,b,index):
   XW_EXT_COLORMAP *acolormap	Colormap extension structure
   float r,g,b ;	Red,Green,Blue color value 0. >= x <= 1.
   int *index ;		Return Color index 0 >= x < MAXCOLOR

	Gets the nearest color index from R,G,B Color values.

	Returns ERROR if BadColor Index or color is not defined
	Returns SUCCESS if Successful

*/

#define XTEST 1 
#ifdef TEST	/* Optim. No more necessary with XQueryColors() */
#define MAX_COLRS 256
static int indice = 0;
static int indice_max = 0;
static XW_EXT_COLORMAP *qcolormap = NULL;
static float tab_ind[MAX_COLRS][4];
#endif

#ifdef XW_PROTOTYPE
XW_STATUS Xw_get_color_index(void* acolormap,float r,float g,float b,int* index)
#else
XW_STATUS Xw_get_color_index(acolormap,r,g,b,index)
void *acolormap ;
float r,g,b ;
int *index ;
#endif /*XW_PROTOTYPE*/
{
XW_EXT_COLORMAP *pcolormap = (XW_EXT_COLORMAP*)acolormap ;
XW_STATUS status = XW_ERROR ;
XColor colors[MAXCOLOR] ;
int color_indexs[MAXCOLOR] ;
register int i,j,n ;
int curind = -1,freeind = -1,isapproximate ;
#ifdef PRO9517
int drmin,dgmin,dbmin;
#else
float curdist,newdist ;
float dr,dg,db ;
#endif

    if( !pcolormap ) {
	/*ERROR*Bad EXT_COLORMAP Address*/
	Xw_set_error( 42,"Xw_get_color_index",pcolormap ) ;
	return( XW_ERROR ) ;
    }

    if( _CCLASS != TrueColor ) {
      if( pcolormap->mapping == Xw_TOM_COLORCUBE ) {
        if( (_CGINFO.red_mult > 0) && (fabs(r-g) < 0.01) && (fabs(r-b) < 0.01) ) {
          curind = (int)(0.5+r*_CGINFO.red_max)*_CGINFO.red_mult;
	  if( _CINFO.red_max > 0 ) curind += (_CINFO.red_max+1)*(_CINFO.green_max+1)*(_CINFO.blue_max+1);
        } else if( _CINFO.red_mult > 0 ) {
          curind = ((int)(0.5+r*_CINFO.red_max))*_CINFO.red_mult+
                            ((int)(0.5+g*_CINFO.green_max))*_CINFO.green_mult+
                            ((int)(0.5+b*_CINFO.blue_max))*_CINFO.blue_mult;
        } else if( _CGINFO.red_mult > 0 ) {
	  float l = (r+g+b)/3.;
          curind = (int)(0.5+l*_CGINFO.red_max)*_CGINFO.red_mult;
	} else {
	  curind = 0;
	}
	if( pcolormap->pixels[curind] == curind +_CINFO.base_pixel )
							status = XW_SUCCESS ;
	else curind = -1 ;
      }
    } 

    if( curind < 0 ) {
#ifdef TEST
	if (pcolormap != qcolormap) {
		for (i=0; i<MAX_COLRS; i++) tab_ind[i][0] = -1.;
		qcolormap = pcolormap;
		indice_max = 0;
		indice = 0;
	}

	for (i=0; i<indice_max; i++) {
		if ( tab_ind[i][0] >= 0. ) {
			if ((tab_ind[i][1] == r) &&
			    (tab_ind[i][2] == g) &&
			    (tab_ind[i][3] == b)) {
				*index = curind	= (int) tab_ind[i][0];
				status	= XW_SUCCESS;
#ifdef TRACE_GET_COLOR_INDEX
if( Xw_get_trace() > 3 ) {
    printf(" %d = Xw_get_color_index(%x,%f,%f,%f,=%d/0x%x)\n",
		status,pcolormap,r,g,b,curind,pcolormap->pixels[curind]) ;
}
#endif
				return (status);
			}
		}
	}
#endif

#ifdef PRO9517
      	drmin = dgmin = dbmin = 65536;
#else
	newdist	= 4.0 ;
	curdist	= 4.0 ;
#endif
	curind	= -1 ;

	for( i=n=0 ; i<pcolormap->maxcolor ; i++ ) {
	    if( Xw_isdefine_color( pcolormap,i ) == XW_SUCCESS ) {
	      if( n < pcolormap->maxhcolor ) {
	        colors[n].pixel = pcolormap->pixels[i]; 
	        color_indexs[n] = i;
	        n++;
	      } else {
	        for( j=0 ; j<n ; j++ ) {
		  if( colors[j].pixel == pcolormap->pixels[i] ) break;
	        }
		if( j >= n ) {
		  /* Too many entrys in the virtual colormap */
		  Xw_set_error(129,"Xw_get_color_index",pcolormap ) ;
	 	}
	      }
	    } else {
	      if( freeind < 0 ) freeind = i ;
	    } 
	}

	if( _CCLASS != TrueColor ) {
	  if( n > 0 ) {
	    XQueryColors(_CDISPLAY,_CINFO.colormap,colors,n) ;
#ifdef PRO9517
	    {
	      unsigned long rred = (long unsigned int )( r * 0xFFFF);
	      unsigned long rgreen = (long unsigned int )( g * 0xFFFF);
	      unsigned long rblue = (long unsigned int )( b * 0xFFFF);
	      int dr,dg,db;
	      for( i=0 ; i<n ; i++ ){
		//#if defined(_AIX) || defined(__hpux)
                int aLocalInt =  rred - colors[i].red  ; dr = abs( aLocalInt ) >> 8;
                aLocalInt = rgreen - colors[i].green   ; dg = abs( aLocalInt ) >> 8;
                aLocalInt = rblue - colors[i].blue     ; db = abs( aLocalInt ) >> 8;
		//#else
		//                dr = abs( rred - colors[i].red ) >> 8;
		//                dg = abs( rgreen - colors[i].green ) >> 8;
		//                db = abs( rblue - colors[i].blue ) >> 8;
		//#endif
                if( (dr <= drmin) && (dg <= dgmin) && (db <= dbmin) ) {
                  drmin = dr; dgmin = dg; dbmin = db;
		  curind  = color_indexs[i];
		  if( !dr && !dg && !db ) break;
                }
	      }
	    }
#else
	    for( i=0 ; ((i<n) && (newdist != 0.0)) ; i++ ){
	      dr	= (float)colors[i].red/0xFFFF - r ;
	      dg	= (float)colors[i].green/0xFFFF - g ;
	      db	= (float)colors[i].blue/0xFFFF - b ;
	      newdist	= dr*dr + dg*dg + db*db ;
	      if( newdist < curdist ) {
		curind	= color_indexs[i] ;
		curdist	= newdist ;
	      }
	    }
#endif
	  }
	} else {
	  if( n > 0 ) {
//	    unsigned long mask = _CVISUAL->map_entries-1 ;
	    unsigned long red,green,blue,rmask,gmask,bmask ;
#ifdef PRO9517
	    {
	      unsigned long rred = (long unsigned int )( r * (1 << _CVISUAL->bits_per_rgb) - 1);
	      unsigned long rgreen = (long unsigned int )( g * (1 << _CVISUAL->bits_per_rgb) - 1);
	      unsigned long rblue = (long unsigned int )( b * (1 << _CVISUAL->bits_per_rgb) - 1);
	      int dr,dg,db;
	      for( i=0 ; i<n ; i++ ){
	        rmask = _CVISUAL->red_mask;
	        red = colors[i].pixel & rmask ;
                while ( !(rmask & 0x01) ) { rmask >>= 1; red >>= 1; }
 
	        gmask = _CVISUAL->green_mask;
	        green = colors[i].pixel & gmask ;
                while ( !(gmask & 0x01) ) { gmask >>= 1; green >>= 1; }

	        bmask = _CVISUAL->blue_mask;
	        blue = colors[i].pixel & bmask ;
                while ( !(bmask & 0x01) ) { bmask >>= 1; blue >>= 1; }
	
		//#if defined(_AIX) || defined(__hpux)
		int aLocalInt = rred - red ; dr = abs(aLocalInt);
		aLocalInt = rgreen - green ; dg = abs(aLocalInt);
		aLocalInt = rblue - blue   ; db = abs(aLocalInt);
		//#else
		//                dr = abs( rred - red );
		//                dg = abs( rgreen - green );
		//                db = abs( rblue - blue );
		//#endif
                if( (dr <= drmin) && (dg <= dgmin) && (db <= dbmin) ) {
                  drmin = dr; dgmin = dg; dbmin = db;
		  curind  = color_indexs[i];
		  if( !dr && !dg && !db ) break;
                }
	      }
	    }
#else
	    for( i=0 ; ((i<n) && (newdist != 0.0)) ; i++ ){
	      rmask = _CVISUAL->red_mask;
	      red = colors[i].pixel & rmask ;
              while ( !(rmask & 0x01) ) { rmask >>= 1; red >>= 1; }
 
	      gmask = _CVISUAL->green_mask;
	      green = colors[i].pixel & gmask ;
              while ( !(gmask & 0x01) ) { gmask >>= 1; green >>= 1; }

	      bmask = _CVISUAL->blue_mask;
	      blue = colors[i].pixel & bmask ;
              while ( !(bmask & 0x01) ) { bmask >>= 1; blue >>= 1; }
	
	      dr	= (float)red/mask - r ;
	      dg	= (float)green/mask - g ;
	      db	= (float)blue/mask - b ;
	      newdist	= dr*dr + dg*dg + db*db ;

	      if( newdist < curdist ) {
		curind	= color_indexs[i] ;
		curdist	= newdist ;
	      }
	    }
#endif
	  }
	}

#ifdef PRO9517
	if( drmin > 0 || dgmin > 0 || dbmin > 0 ) {
#else
	if( curdist > 0.0 ) {
#endif
	  if( freeind < 0 ) {
	    unsigned long pixel;
	    if( Xw_alloc_color(pcolormap,r,g,b,&pixel,&isapproximate) ) {
	      for( i=0 ; i<pcolormap->maxcolor ; i++ ) {
	        if( pixel ==  pcolormap->pixels[i] ) break; 
	      }
	      if( i < pcolormap->maxcolor ) {
	        curind = i;
	      }
	    }
	  } else {
	    if( Xw_def_color(pcolormap,freeind,r,g,b) ) {
	      curind = freeind ;
	    }
	  }
	}

	if( curind >= 0 ) {
	    status = XW_SUCCESS ;
#ifdef TEST
	    tab_ind[indice][0] = curind ;
	    tab_ind[indice][1] = r ;
	    tab_ind[indice][2] = g ;
	    tab_ind[indice][3] = b ;
	/*
	 * Une fois que le tableau est  plein, il faut le
	 * parcourir completement, donc on ne touche plus
	 * a indice_max.
	 */
	    indice ++ ;
	    if( indice < MAX_COLRS ) {
	        indice_max = max(indice_max,indice);
	    } else {
		indice = 0 ; /* on repart a 0 */
#ifdef TRACE_GET_COLOR_INDEX
			if( Xw_get_trace() > 1 ) {
			    printf(" ************************\n") ;
			    printf(" Xw_get_color_index, full array\n") ;
			    printf(" ************************\n") ;
			}
#endif
	    }
#endif
	}

    }

    *index = curind ;

#ifdef TRACE_GET_COLOR_INDEX
if( Xw_get_trace() > 3 ) {
    printf(" %d = Xw_get_color_index(%lx,%f,%f,%f,#%d/0x%lx)\n",
		status,(long ) pcolormap,r,g,b,curind,(long ) pcolormap->pixels[curind]) ;
}
#endif

	return( status ) ;
}