0023830: BRepExtrema_DistShapeShape does not find intersection of face with edge
[occt.git] / src / Xw / Xw_get_color_pixel.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18
19 #include <Xw_Extension.h>
20
21 /* ifdef then trace on */
22 #ifdef TRACE
23 #define TRACE_GET_COLOR_PIXEL
24 #endif
25
26 /*
27    STATUS Xw_get_color_pixel(acolormap,r,g,b,pixel,isapproximate):
28    XW_EXT_COLORMAP *acolormap   Colormap extension structure
29    float r,g,b ;        Red,Green,Blue color value 0. >= x <= 1.
30    unsigned long *pixel ;       Return Color pixel depending of Visual Class 
31    bool isapproximate ;
32
33         Gets the nearest color pixel from R,G,B Color values.
34
35         Returns ERROR if Colormap is not defined properly
36         Returns SUCCESS if Successful
37
38 */
39
40 #define TEST 1
41 #ifdef TEST     /* Optim. no more necessary with XQueryColors */
42 #define MAX_COLRS 256
43 static int indice = 0;
44 static int indice_max = 0;
45 static XW_EXT_COLORMAP *qcolormap = NULL;
46 static float tab_ind[MAX_COLRS][4];
47 #endif
48
49 #ifdef XW_PROTOTYPE
50 XW_STATUS Xw_get_color_pixel(void* acolormap,
51                 float r,float g,float b,unsigned long* pixel,int *isapproximate)
52 #else
53 XW_STATUS Xw_get_color_pixel(acolormap,r,g,b,pixel,isapproximate)
54 void *acolormap ;
55 float r,g,b ;
56 unsigned long *pixel ;
57 int *isapproximate;
58 #endif /*XW_PROTOTYPE*/
59 {
60 XW_EXT_COLORMAP *pcolormap = (XW_EXT_COLORMAP*)acolormap ;
61 XW_STATUS status = XW_ERROR ;
62 //XColor colors[MAXCOLOR] ;
63 //int color_indexs[MAXCOLOR] ;
64 //register i,j,n ;
65 register int i ;
66
67     *pixel = 0;
68     if( !pcolormap ) {
69         /*ERROR*Bad EXT_COLORMAP Address*/
70         Xw_set_error( 42,"Xw_get_color_pixel",pcolormap ) ;
71         return( XW_ERROR ) ;
72     }
73
74     *isapproximate = False;
75     if( _CCLASS == TrueColor ) {
76         unsigned long mask = _CVISUAL->map_entries-1 ;
77         unsigned long red   = (unsigned long) (r * mask) ;
78         unsigned long green = (unsigned long) (g * mask) ;
79         unsigned long blue  = (unsigned long) (b * mask) ;
80
81         mask = _CVISUAL->red_mask;
82         while ( !(mask & 0x01) ) { mask >>= 1; red <<= 1; }
83         mask = _CVISUAL->green_mask;
84         while ( !(mask & 0x01) ) { mask >>= 1; green <<= 1; }
85         mask = _CVISUAL->blue_mask;
86         while ( !(mask & 0x01) ) { mask >>= 1; blue <<= 1; }
87         *pixel = red|green|blue ;
88         status = XW_SUCCESS ;
89     } else if( pcolormap->mapping == Xw_TOM_COLORCUBE ) {
90         int curind = 0;
91         if( (_CGINFO.red_mult > 0) && (fabs(r-g) < 0.01) && (fabs(r-b) < 0.01) ) {
92           curind = (int)(0.5+r*_CGINFO.red_max)*_CGINFO.red_mult;
93           if( _CINFO.red_max > 0 ) curind += (_CINFO.red_max+1)*(_CINFO.green_max+1)*(_CINFO.blue_max+1);
94         } else if( _CINFO.red_mult > 0 ) {
95           curind = ((int)(0.5+r*_CINFO.red_max))*_CINFO.red_mult+
96                             ((int)(0.5+g*_CINFO.green_max))*_CINFO.green_mult+
97                             ((int)(0.5+b*_CINFO.blue_max))*_CINFO.blue_mult;
98         } else if( _CGINFO.red_mult > 0 ) {
99           float l = (r+g+b)/3.;
100           curind = (int)(0.5+l*_CGINFO.red_max)*_CGINFO.red_mult;
101         }
102         *pixel = _CINFO.base_pixel + curind ;
103         if( pcolormap->pixels[curind] == *pixel ) status = XW_SUCCESS ;
104     } 
105
106     if( !status ) {
107
108 #ifdef TEST
109       if (pcolormap != qcolormap) {
110         for (i=0; i<MAX_COLRS; i++) tab_ind[i][0] = -1.;
111         qcolormap = pcolormap;
112         indice_max = 0;
113         indice = 0;
114       }
115
116       for (i=0; i<indice_max; i++) {
117         if (tab_ind[i][0] >= 0.) {
118                 if ((tab_ind[i][1] == r) &&
119                             (tab_ind[i][2] == g) &&
120                             (tab_ind[i][3] == b)) {
121                   *pixel        = (int) tab_ind[i][0];
122                   status        = XW_SUCCESS;
123                   break;
124                 }
125         }
126       }
127 #endif
128
129       if( !status ) {
130         status = Xw_alloc_color(pcolormap,r,g,b,pixel,isapproximate) ; 
131
132 #ifdef TEST
133         if( status ) {
134           tab_ind[indice][0] = *pixel ;
135           tab_ind[indice][1] = r ;
136           tab_ind[indice][2] = g ;
137           tab_ind[indice][3] = b ;
138         /*
139          * Une fois que le tableau est  plein, il faut le
140          * parcourir completement, donc on ne touche plus
141          * a indice_max.
142          */
143           indice ++ ;
144           if( indice < MAX_COLRS ) {
145               indice_max = max(indice_max,indice);
146           } else {
147               indice = 0 ; /* on repart a 0 */
148 #ifdef TRACE_GET_COLOR_PIXEL
149                         if( Xw_get_trace() > 1 ) {
150                             printf(" ************************\n") ;
151                             printf(" Xw_get_color_pixel, full array\n") ;
152                             printf(" ************************\n") ;
153                         }
154 #endif
155           }
156         }
157 #endif
158       }
159     }
160
161
162 #ifdef TRACE_GET_COLOR_PIXEL
163 if( Xw_get_trace() > 3 ) {
164     printf(" %d = Xw_get_color_pixel(%lx,%f,%f,%f,%lx,%d)\n",
165                         status,(long ) pcolormap,r,g,b,*pixel,*isapproximate) ;
166 }
167 #endif
168
169     return( status ) ;
170 }