0023309: The 'then' statement is equivalent to the 'else' statement in TopOpeBRep_Edg...
[occt.git] / src / ImageUtility / ImageUtility_X11Dump.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 // Modified     27/12/98 : FMN ; PERF: OPTIMISATION LOADER (LOPTIM)
20
21 #include <ImageUtility_X11Dump.ixx>
22
23 #include <Standard.hxx>
24 #include <Image_PseudoColorImage.hxx>
25 #include <Image_ColorImage.hxx>
26 #include <Aspect_ColorMap.hxx>
27 #include <X11/Xutil.h>
28 #include <Aspect_ColorMapEntry.hxx>
29 #include <Image_PixelRowOfDIndexedImage.hxx>
30 #include <Image_PixelRowOfDColorImage.hxx>
31
32
33
34
35 ImageUtility_X11Dump::ImageUtility_X11Dump(
36         const TCollection_AsciiString& DisplayName ,
37         const Handle(Image_Image)& aImage )
38
39 { myImage = aImage ;
40
41   myDisplay = XOpenDisplay( DisplayName.ToCString() ) ;
42
43   XVisualInfo VInfo, *PVinfo ;
44   int Nitems ;
45   XSetWindowAttributes WinAttr ;
46
47   Visual *theVisual = DefaultVisual( myDisplay, DefaultScreen( myDisplay ) );
48
49   VInfo.visualid = theVisual->visualid ;
50
51   PVinfo = XGetVisualInfo( myDisplay, VisualIDMask, &VInfo, &Nitems ) ;
52
53   if ( aImage->Type() == Image_TOI_PseudoColorImage ) {
54         if ( PVinfo->depth != 8 || PVinfo->c_class != PseudoColor ) {
55                 VInfo.depth   = 8 ;
56                 VInfo.c_class = PseudoColor ;
57                 PVinfo = XGetVisualInfo( myDisplay,
58                                          VisualDepthMask|VisualClassMask,
59                                          &VInfo, &Nitems ) ;
60         }
61   }
62   else {
63         if ( PVinfo->depth != 24 || PVinfo->c_class != TrueColor ) {
64                 VInfo.depth   = 24 ;
65                 VInfo.c_class = TrueColor ;
66                 PVinfo = XGetVisualInfo( myDisplay,
67                                          VisualDepthMask|VisualClassMask,
68                                          &VInfo, &Nitems ) ;
69         }
70   }
71
72   WinAttr.background_pixel = 0 ;
73   WinAttr.border_pixel     = 0 ;
74
75   WinAttr.colormap = XCreateColormap( myDisplay,
76                                      RootWindow( myDisplay, 
77                                         DefaultScreen( myDisplay ) ),
78                                         PVinfo->visual,
79                                         (PVinfo->c_class==TrueColor)?
80                                                 AllocNone:AllocAll ) ;
81
82
83   myWindow = XCreateWindow( myDisplay,
84                           RootWindow( myDisplay, DefaultScreen( myDisplay ) ),
85                           0, 0,
86                           ( unsigned int ) aImage->Width(),
87                           ( unsigned int ) aImage->Height(),
88                           0,
89                           PVinfo->depth,
90                           InputOutput,
91                           PVinfo->visual,
92                           CWBackPixel | CWBorderPixel | CWColormap, &WinAttr ) ;
93
94   myXImage = XCreateImage( myDisplay,
95                            PVinfo->visual,
96                            PVinfo->depth,
97                            ZPixmap,
98                            0,
99                            NULL,
100                            ( unsigned int ) aImage->Width(),
101                            ( unsigned int ) aImage->Height(),
102                            BitmapPad( myDisplay ),
103                            0 ) ;
104
105   myGC = XCreateGC( myDisplay, myWindow, 0, NULL ) ;
106
107   XSetPlaneMask( myDisplay, myGC, AllPlanes ) ;
108   XSetFunction( myDisplay, myGC, GXcopy ) ;
109
110   UpdateX11Colormap() ;
111   UpdateX11XImage() ;
112
113   XMapWindow( myDisplay, myWindow ) ;
114
115   XFlush( myDisplay ) ;           
116
117 }
118
119 ImageUtility_X11Dump::ImageUtility_X11Dump(
120         const ImageUtility_X11Dump& anOtherX11Dump ,
121         const Handle(Image_Image)& aImage )
122
123 { myImage = aImage ;
124
125   myDisplay = anOtherX11Dump.X11Display() ;
126
127   myWindow = anOtherX11Dump.X11Window() ;
128
129   XWindowAttributes WinAttr ;
130
131   XGetWindowAttributes( myDisplay, myWindow, &WinAttr ) ;
132
133   myXImage = XCreateImage( myDisplay,
134                            WinAttr.visual,
135                            WinAttr.depth,
136                            ZPixmap,
137                            0,
138                            NULL,
139                            ( unsigned int ) aImage->Width(),
140                            ( unsigned int ) aImage->Height(),
141                            BitmapPad( myDisplay ),
142                            0 ) ;
143
144   myGC = anOtherX11Dump.X11GC() ;
145
146   UpdateX11XImage() ;
147
148   XFlush( myDisplay ) ;           
149 }
150
151 ImageUtility_X11GC      ImageUtility_X11Dump::X11GC() const 
152 { return myGC ; }
153
154 ImageUtility_X11Window  ImageUtility_X11Dump::X11Window() const 
155 { return myWindow ; }
156
157 ImageUtility_X11Display ImageUtility_X11Dump::X11Display() const 
158 { return myDisplay ; }
159
160 ImageUtility_X11XImage  ImageUtility_X11Dump::X11XImage() const 
161 { return myXImage ; }
162
163 void ImageUtility_X11Dump::UpdateX11Colormap() const
164
165
166   if ( myImage->Type() != Image_TOI_PseudoColorImage ) return ;
167
168   Handle(Image_PseudoColorImage) aPImage = 
169                 Handle(Image_PseudoColorImage)::DownCast( myImage ) ;
170   Handle(Aspect_ColorMap) TheColorMap = aPImage->ColorMap() ;
171   Aspect_ColorMapEntry aEntry ;
172   XColor aColor ;
173   Standard_Integer i ;
174   XWindowAttributes WinAttr ;
175
176   XGetWindowAttributes( myDisplay, myWindow, &WinAttr ) ;
177
178   for ( i = 0 ; i < TheColorMap->Size() ; i++ ) {
179         aEntry = TheColorMap->Entry( i+1 ) ;
180
181         aColor.pixel = aEntry.Index() % WinAttr.visual->map_entries ;
182         aColor.red   = (unsigned short)(aEntry.Color().Red()   * 0xffff + 0.5 );
183         aColor.green = (unsigned short)(aEntry.Color().Green() * 0xffff + 0.5 );
184         aColor.blue  = (unsigned short)(aEntry.Color().Blue()  * 0xffff + 0.5 );
185         aColor.flags = DoRed | DoGreen | DoBlue ;
186
187         XStoreColor( myDisplay, WinAttr.colormap, &aColor ) ;
188   }
189 }
190
191 static unsigned int get_range( unsigned long col_mask )
192
193 {
194   while ( !(col_mask & 1) ) col_mask >>= 1 ;
195
196   return( col_mask ) ;
197 }
198
199 static unsigned int get_shift( unsigned long col_mask , unsigned long rgb_bit )
200
201 { long shift ;
202
203   if ( ( col_mask >> rgb_bit ) == 0 ) {
204     shift = 0 ;
205   }
206   else if ( ( col_mask >> ( 2 * rgb_bit ) ) == 0 ) {
207     shift = rgb_bit ;
208   }
209   else {
210     shift = 2 * rgb_bit ;
211   }
212
213   return( shift ) ;
214 }
215
216
217 void ImageUtility_X11Dump::UpdateX11XImage()
218
219 { Standard_Integer x,y, width, height, X, Y, bits_per_line;
220
221   width  = myImage->Width() ;   
222   height = myImage->Height() ;
223
224   X = myImage->LowerX() ;
225   Y = myImage->LowerY() ;
226
227   if (  myXImage->data == NULL || 
228         myXImage->width != width || 
229         myXImage->height != height ) {
230         if ( myXImage->data != NULL ) {
231           Standard_Address ToBeFree = ( Standard_Address ) myXImage->data ;
232           Standard::Free( ToBeFree); 
233           myXImage->data = NULL ;
234         }
235
236         myXImage->width          = width  ;
237         myXImage->height         = height ;
238         bits_per_line = width * myXImage->bits_per_pixel ;
239         if ( bits_per_line % myXImage->bitmap_pad ) {
240                 bits_per_line += myXImage->bitmap_pad ;
241         }
242
243         myXImage->bytes_per_line =  bits_per_line / 8 ;
244
245         myXImage->data = ( char * ) 
246           Standard::Allocate(myXImage->bytes_per_line*myXImage->height);
247   }
248
249   if ( myImage->Type() == Image_TOI_PseudoColorImage ) {
250         char *pl, *p ;
251         Handle(Image_PseudoColorImage) aPImage = 
252                 Handle(Image_PseudoColorImage)::DownCast( myImage ) ;
253         Image_PixelRowOfDIndexedImage aRow( 0, myImage->Width()-1 ) ;
254
255         for ( y = 0 , pl = myXImage->data ; 
256               y < height ; 
257               y++, pl += myXImage->bytes_per_line ) {
258                 aPImage->Row( X, Y+y, aRow ) ;
259                 for ( x = 0 , p = pl ; x < width ; x++, p++ ) {
260                         *p = ( char ) ( aRow(x).Value() & 0xff ) ;
261                 }
262         }
263   }
264   else {
265         unsigned int *pl, *p ;
266         Handle(Image_ColorImage) aCImage = 
267                 Handle(Image_ColorImage)::DownCast( myImage ) ;
268         Image_PixelRowOfDColorImage aRow( 0, myImage->Width()-1 ) ;
269
270         XWindowAttributes WinAttr ;
271         Standard_Real r,g,b ;
272         unsigned int lr,lg,lb, rgb_range, r_shift, g_shift, b_shift ;
273
274         XGetWindowAttributes( myDisplay, myWindow, &WinAttr ) ;
275
276         rgb_range = get_range( WinAttr.visual->red_mask ) ;
277         r_shift   = get_shift( WinAttr.visual->red_mask, 
278                                WinAttr.visual->bits_per_rgb ) ;
279         g_shift   = get_shift( WinAttr.visual->green_mask, 
280                                WinAttr.visual->bits_per_rgb ) ;
281         b_shift   = get_shift( WinAttr.visual->blue_mask, 
282                                WinAttr.visual->bits_per_rgb ) ;
283
284         for ( y = 0 , pl = ( unsigned int *) myXImage->data ; 
285               y < height ; 
286               y++, pl += ( myXImage->bytes_per_line/sizeof( unsigned int ) ) ) {
287                 aCImage->Row( X, Y+y, aRow ) ;
288                 for ( x = 0 , p = pl ; x < width ; x++, p++ ) {
289                   aRow(x).Value().Values( r,g,b , Quantity_TOC_RGB ) ;
290                   lr = ( unsigned int ) ( r * ( rgb_range ) +0.5) ;
291                   lg = ( unsigned int ) ( g * ( rgb_range ) +0.5) ;
292                   lb = ( unsigned int ) ( b * ( rgb_range ) +0.5) ;
293                   *p =  (lr<<r_shift) | (lg<<g_shift) | (lb<<b_shift) ;
294                 }
295         }
296   }
297 }
298
299
300 void ImageUtility_X11Dump::DisplayX11XImage() const
301
302 { XPutImage(    myDisplay, 
303                 myWindow, 
304                 myGC,
305                 myXImage,
306                 0,0,
307                 0,0,
308                 myXImage->width,myXImage->height ) ;
309
310 }
311