0023712: Remove dependency on Aspect_GraphicDevice from Aspect_Window
[occt.git] / src / Xw / Xw_ColorMap.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19 #define PRO7349 //GG_11/04/97
20 //              Modification de la methode PixelOfColor 
21 //              qui renvoie maintenant un numero de pixel negatif
22 //              lorsqu'il correspond a une couleur approximee.
23
24 #define PRO10676        //GG_151297
25 //              Permettre de reserver un index de couleur modifiable
26 //              dans la colormap.
27
28 #include <Xw_ColorMap.ixx>
29 #include <Quantity_Color.hxx>
30 #include <Standard_ShortReal.hxx>
31 #include <Standard_PCharacter.hxx>
32
33 // Routines C a declarer en extern
34 //extern "C" {
35 #include <Xw_Cextern.hxx>
36 //}
37
38 //-Static data definitions
39
40 static XW_STATUS status ;
41
42 void Xw_ColorMap::PrintError() {
43 Standard_CString ErrorMessag ;
44 Standard_Integer ErrorNumber ;
45 Standard_Integer ErrorGravity ;
46  
47         status = XW_SUCCESS ;
48         ErrorMessag = Xw_get_error(&ErrorNumber,&ErrorGravity) ;
49         if( ErrorGravity > 2 ) Aspect_ColorMapDefinitionError::Raise (ErrorMessag) ;
50         else Xw_print_error() ;
51 }
52
53 Xw_ColorMap::Xw_ColorMap () {
54 }
55
56 Xw_ColorMap::Xw_ColorMap (const Standard_CString Connexion, const Xw_TypeOfVisual Visual, const Xw_TypeOfMapping Mapping, const Standard_Integer Ncolors, const Standard_Boolean UseDefault) {
57 Aspect_Handle BaseMap = 0 ;
58
59         MyExtendedDisplay = Xw_open_display((Standard_PCharacter)Connexion) ;
60
61         if( !MyExtendedDisplay ) PrintError() ; 
62
63         if( UseDefault ) {
64             Aspect_Handle *display,root ;
65             Xw_TypeOfVisual vclass ;
66             int depth ;
67
68             status = Xw_get_display_info (MyExtendedDisplay,
69                                         &display,&root,&BaseMap,&vclass,&depth);
70             if( Visual != vclass ) BaseMap = 0;
71         }
72
73         MyExtendedColorMap = Xw_def_colormap(MyExtendedDisplay,
74                                         Visual,Ncolors,BaseMap,Mapping) ;
75         
76         if( !Xw_isdefine_colormap(MyExtendedColorMap) ) PrintError() ;
77
78         MyExtendedOverlayColorMap = Xw_def_colormap(MyExtendedDisplay,
79                         Xw_TOV_OVERLAY,0,0,Xw_TOM_READONLY) ;
80
81         SetHighlightColor(Quantity_NOC_WHITESMOKE) ;
82
83         MyMapping = Mapping ;
84 }
85
86 void Xw_ColorMap::SetEntry (const Standard_Integer Index,const Standard_Real Red, const Standard_Real Green, const Standard_Real Blue) {
87
88
89     switch ( MyMapping ) {
90         
91       case Xw_TOM_HARDRAMP: 
92       case Xw_TOM_SIMPLERAMP:
93       case Xw_TOM_BESTRAMP:
94         status = Xw_def_color(MyExtendedColorMap,
95                         (int)Index,(float)Red,(float)Green,(float)Blue) ;
96
97         if( !status ) PrintError() ;
98         break ;
99
100       case Xw_TOM_COLORCUBE:
101       case Xw_TOM_READONLY:
102         Aspect_BadAccess::Raise ("Try to write in a READ ONLY colormap");
103     }
104
105 }
106
107 void Xw_ColorMap::SetEntry (const Aspect_ColorMapEntry& Entry) {
108 Standard_Real r,g,b ;
109
110         (Entry.Color()).Values(r,g,b,Quantity_TOC_RGB) ;
111
112         SetEntry(Entry.Index(),r,g,b) ;
113
114 }
115
116 void Xw_ColorMap::SetEntries (const Handle(Aspect_ColorMap)& Colormap) {
117 Standard_Integer size = Colormap->Size() ;
118 Standard_Integer i ;
119
120         for( i=1 ; i<=size ; i++ ) {
121             SetEntry(Colormap->Entry(i)) ;
122         }
123 }
124
125 void Xw_ColorMap::SetHighlightColor ( const Quantity_Color& aColor) {
126 Standard_Real r,g,b ;
127
128         aColor.Values(r,g,b,Quantity_TOC_RGB) ;
129
130         status = Xw_def_highlight_color(MyExtendedColorMap,
131                                         (float)r,(float)g,(float)b) ;
132         if( !status ) PrintError() ;
133
134         if( MyExtendedOverlayColorMap ) {
135           status = Xw_def_highlight_color(MyExtendedOverlayColorMap,
136                                         (float)r,(float)g,(float)b) ;
137           if( !status ) PrintError() ;
138         }
139 }
140
141 void Xw_ColorMap::Destroy() {
142
143         if( MyExtendedColorMap ) {
144             Xw_close_colormap(MyExtendedColorMap) ;
145             MyExtendedColorMap = NULL ;
146         }
147
148         if( MyExtendedOverlayColorMap ) {
149             Xw_close_colormap(MyExtendedOverlayColorMap) ;
150             MyExtendedOverlayColorMap = NULL ;
151         }
152 }
153
154 Quantity_Color Xw_ColorMap::HighlightColor ( ) const {
155 float r,g,b ;
156
157         status = Xw_get_highlight_color (MyExtendedColorMap,&r,&g,&b);
158
159         Standard_Real Red = r;
160         Standard_Real Green = g;
161         Standard_Real Blue = b;
162
163         return Quantity_Color(Red,Green,Blue,Quantity_TOC_RGB) ;
164 }
165
166 Standard_Integer Xw_ColorMap::HighlightPixel ( ) const {
167 unsigned long pixel ;
168
169         status = Xw_get_highlight_pixel(MyExtendedColorMap,&pixel) ;
170
171         if( !status ) PrintError() ;
172
173         return (Standard_Integer(pixel)) ;
174 }
175
176 Standard_Integer Xw_ColorMap::PixelOfColor ( const Quantity_Color &aColor ) const {
177 Standard_Real Red,Green,Blue ;
178 unsigned long pixel ;
179 Standard_Integer isapproximate;
180
181         aColor.Values(Red,Green,Blue,Quantity_TOC_RGB) ;
182
183 #ifdef PRO7349
184         status = Xw_get_color_pixel(MyExtendedColorMap,
185                         (float)Red,(float)Green,(float)Blue,&pixel,&isapproximate) ;
186         if( !status ) PrintError() ;
187
188         if( isapproximate ) return (-Standard_Integer(pixel));
189
190         else return (Standard_Integer(pixel)) ;
191 #else
192         status = Xw_get_color_pixel(MyExtendedColorMap,
193                         (float)Red,(float)Green,(float)Blue,&pixel) ;
194
195         if( !status ) PrintError() ;
196
197         return (Standard_Integer(pixel)) ;
198 #endif
199 }
200
201 #ifdef PRO10676
202 Standard_Integer Xw_ColorMap::AllocatesPixelOfColor ( ) const {
203         unsigned long pixel;
204         if( Xw_alloc_pixel(MyExtendedColorMap,&pixel) ) 
205                                 return Standard_Integer(pixel);
206         else {
207           Xw_print_error();
208           return -1;
209         }
210 }
211
212 void Xw_ColorMap::FreePixelOfColor ( const Standard_Integer aPixel ) const {
213         unsigned long pixel = aPixel;
214         Xw_free_pixel(MyExtendedColorMap,pixel);
215 }
216
217 Standard_Boolean Xw_ColorMap::SetColorOfPixel ( const Standard_Integer aPixel,
218                                                 const Quantity_Color &aColor ) const {
219 Standard_Real Red,Green,Blue ;
220         aColor.Values(Red,Green,Blue,Quantity_TOC_RGB) ;
221         Standard_ShortReal r = Red,g = Green,b = Blue;
222         unsigned long pixel = aPixel;
223         return Xw_set_pixel(MyExtendedColorMap,pixel,r,g,b);
224 }
225 #endif
226
227 Standard_Integer Xw_ColorMap::Entry ( const Standard_Integer Index, Standard_Real &Red, Standard_Real &Green, Standard_Real &Blue ) const {
228 unsigned long pixel = 0 ;
229 float red,green,blue ;
230
231         status = Xw_get_color(MyExtendedColorMap,Index,
232                                         &red,&green,&blue,&pixel) ;
233
234         if( !status ) PrintError() ;
235
236         Red = red; Green = green; Blue = blue;
237
238         return (Standard_Integer(pixel)) ;
239 }
240
241 Xw_TypeOfVisual Xw_ColorMap::VisualClass( ) const {
242 Xw_TypeOfVisual vclass ;
243
244         vclass = Xw_get_colormap_visual(MyExtendedColorMap) ;
245
246         if( vclass == Xw_TOV_DEFAULT ) PrintError() ;
247
248         return (vclass) ;
249 }
250
251 Xw_TypeOfVisual Xw_ColorMap::OverlayVisualClass( ) const {
252 Xw_TypeOfVisual vclass = Xw_TOV_DEFAULT;
253
254         if( MyExtendedOverlayColorMap ) {
255           vclass = Xw_get_colormap_visual(MyExtendedOverlayColorMap) ;
256         }
257
258         return (vclass) ;
259 }
260
261 Standard_Integer Xw_ColorMap::VisualID( ) const {
262 Aspect_Handle *VisualInfo ;
263 Xw_TypeOfVisual VisualClass ;
264 int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid = 0 ;
265
266         status = Xw_get_colormap_info(MyExtendedColorMap,&VisualInfo,
267                 &VisualClass,&visualid,&MaxColor,&BasePixel,
268                 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
269
270         if( !status ) Xw_print_error();
271
272         return (visualid) ;
273 }
274
275 Standard_Integer Xw_ColorMap::OverlayVisualID( ) const {
276 Aspect_Handle *VisualInfo ;
277 Xw_TypeOfVisual VisualClass ;
278 int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid = 0;
279
280       if( MyExtendedOverlayColorMap ) { 
281         status = Xw_get_colormap_info(MyExtendedOverlayColorMap,&VisualInfo,
282                 &VisualClass,&visualid,&MaxColor,&BasePixel,
283                 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
284
285         if( !status ) Xw_print_error() ;
286       }
287       return (visualid) ;
288 }
289
290 Standard_Integer Xw_ColorMap::MaxColors( ) const {
291 Aspect_Handle *VisualInfo ;
292 Xw_TypeOfVisual VisualClass ;
293 int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid ;
294
295         status = Xw_get_colormap_info(MyExtendedColorMap,&VisualInfo,
296                 &VisualClass,&visualid,&MaxColor,&BasePixel,
297                 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
298
299         if( status ) {
300           if( VisualClass == Xw_TOV_TRUECOLOR ) return MaxColor ;
301           else return MaxUserColor;
302         } else {
303           Xw_print_error() ;
304           return 0 ;
305         }
306 }
307
308 Standard_Integer Xw_ColorMap::MaxOverlayColors( ) const {
309 Aspect_Handle *VisualInfo ;
310 Xw_TypeOfVisual VisualClass ;
311 int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid ;
312
313         status = XW_ERROR;
314         if( MyExtendedOverlayColorMap ) {
315           status = Xw_get_colormap_info(MyExtendedOverlayColorMap,&VisualInfo,
316                 &VisualClass,&visualid,&MaxColor,&BasePixel,
317                 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
318           if( !status ) Xw_print_error() ;
319         } 
320
321         return (status) ? MaxUserColor : 0;
322 }
323
324 Standard_Address Xw_ColorMap::XVisual( ) const {
325 Aspect_Handle *VisualInfo ;
326 Xw_TypeOfVisual VisualClass ;
327 int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid ;
328
329         status = Xw_get_colormap_info(MyExtendedColorMap,&VisualInfo,
330                 &VisualClass,&visualid,&MaxColor,&BasePixel,
331                 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
332
333         if( !status ) PrintError() ;
334
335         return (VisualInfo) ;
336 }
337
338 Standard_Address Xw_ColorMap::XOverlayVisual( ) const {
339 Aspect_Handle *VisualInfo = NULL ;
340 Xw_TypeOfVisual VisualClass ;
341 int MaxColor,BasePixel,MaxUserColor,MaxDefineColor,FirstFreeColorIndex,visualid ;
342
343         status = XW_ERROR;
344         if( MyExtendedOverlayColorMap ) {
345           status = Xw_get_colormap_info(MyExtendedOverlayColorMap,&VisualInfo,
346                 &VisualClass,&visualid,&MaxColor,&BasePixel,
347                 &MaxUserColor,&MaxDefineColor,&FirstFreeColorIndex) ;
348           if( !status ) Xw_print_error() ;
349         }
350
351         return (status) ? VisualInfo : NULL ;
352 }
353
354 Aspect_Handle Xw_ColorMap::XColorMap( ) const {
355 Aspect_Handle colormap ;
356
357         colormap = Xw_get_colormap_xid(MyExtendedColorMap) ;
358
359         if( !colormap ) PrintError() ;
360
361         return (colormap) ;
362 }
363
364 Aspect_Handle Xw_ColorMap::XOverlayColorMap( ) const {
365 Aspect_Handle colormap = 0;
366
367         if( MyExtendedOverlayColorMap ) {
368           colormap = Xw_get_colormap_xid(MyExtendedOverlayColorMap) ;
369         }
370
371         return (colormap);
372 }
373
374 Standard_Address Xw_ColorMap::ExtendedColorMap () const {
375
376         return (MyExtendedColorMap);
377
378 }
379
380 Standard_Address Xw_ColorMap::ExtendedOverlayColorMap () const {
381
382         return (MyExtendedOverlayColorMap);
383
384 }
385
386 Standard_Boolean Xw_ColorMap::XColorCube ( Aspect_Handle &ColormapID,
387                          Standard_Integer &VisualID,
388                          Standard_Integer &BasePixel,
389                          Standard_Integer &RedMax,
390                          Standard_Integer &RedMult,
391                          Standard_Integer &GreenMax,
392                          Standard_Integer &GreenMult,
393                          Standard_Integer &BlueMax,
394                          Standard_Integer &BlueMult ) const
395 {
396
397         status = Xw_get_colormap_colorcube(MyExtendedColorMap,
398                 &ColormapID,&VisualID,&BasePixel,
399                         &RedMax,&RedMult,&GreenMax,&GreenMult,&BlueMax,&BlueMult);
400
401         return status;
402 }
403
404
405 Standard_Boolean Xw_ColorMap::XGrayRamp ( Aspect_Handle &ColormapID,
406                          Standard_Integer &VisualID,
407                          Standard_Integer &BasePixel,
408                          Standard_Integer &GrayMax,
409                          Standard_Integer &GrayMult) const
410 {
411
412         status = Xw_get_colormap_grayramp(MyExtendedColorMap,
413                 &ColormapID,&VisualID,&BasePixel,&GrayMax,&GrayMult);
414
415         return status;
416 }
417