1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
19 // Modified 27/12/98 : FMN ; PERF: OPTIMISATION LOADER (LOPTIM)
21 #include <ImageUtility_X11Dump.ixx>
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>
35 ImageUtility_X11Dump::ImageUtility_X11Dump(
36 const TCollection_AsciiString& DisplayName ,
37 const Handle(Image_Image)& aImage )
41 myDisplay = XOpenDisplay( DisplayName.ToCString() ) ;
43 XVisualInfo VInfo, *PVinfo ;
45 XSetWindowAttributes WinAttr ;
47 Visual *theVisual = DefaultVisual( myDisplay, DefaultScreen( myDisplay ) );
49 VInfo.visualid = theVisual->visualid ;
51 PVinfo = XGetVisualInfo( myDisplay, VisualIDMask, &VInfo, &Nitems ) ;
53 if ( aImage->Type() == Image_TOI_PseudoColorImage ) {
54 if ( PVinfo->depth != 8 || PVinfo->c_class != PseudoColor ) {
56 VInfo.c_class = PseudoColor ;
57 PVinfo = XGetVisualInfo( myDisplay,
58 VisualDepthMask|VisualClassMask,
63 if ( PVinfo->depth != 24 || PVinfo->c_class != TrueColor ) {
65 VInfo.c_class = TrueColor ;
66 PVinfo = XGetVisualInfo( myDisplay,
67 VisualDepthMask|VisualClassMask,
72 WinAttr.background_pixel = 0 ;
73 WinAttr.border_pixel = 0 ;
75 WinAttr.colormap = XCreateColormap( myDisplay,
76 RootWindow( myDisplay,
77 DefaultScreen( myDisplay ) ),
79 (PVinfo->c_class==TrueColor)?
80 AllocNone:AllocAll ) ;
83 myWindow = XCreateWindow( myDisplay,
84 RootWindow( myDisplay, DefaultScreen( myDisplay ) ),
86 ( unsigned int ) aImage->Width(),
87 ( unsigned int ) aImage->Height(),
92 CWBackPixel | CWBorderPixel | CWColormap, &WinAttr ) ;
94 myXImage = XCreateImage( myDisplay,
100 ( unsigned int ) aImage->Width(),
101 ( unsigned int ) aImage->Height(),
102 BitmapPad( myDisplay ),
105 myGC = XCreateGC( myDisplay, myWindow, 0, NULL ) ;
107 XSetPlaneMask( myDisplay, myGC, AllPlanes ) ;
108 XSetFunction( myDisplay, myGC, GXcopy ) ;
110 UpdateX11Colormap() ;
113 XMapWindow( myDisplay, myWindow ) ;
115 XFlush( myDisplay ) ;
119 ImageUtility_X11Dump::ImageUtility_X11Dump(
120 const ImageUtility_X11Dump& anOtherX11Dump ,
121 const Handle(Image_Image)& aImage )
125 myDisplay = anOtherX11Dump.X11Display() ;
127 myWindow = anOtherX11Dump.X11Window() ;
129 XWindowAttributes WinAttr ;
131 XGetWindowAttributes( myDisplay, myWindow, &WinAttr ) ;
133 myXImage = XCreateImage( myDisplay,
139 ( unsigned int ) aImage->Width(),
140 ( unsigned int ) aImage->Height(),
141 BitmapPad( myDisplay ),
144 myGC = anOtherX11Dump.X11GC() ;
148 XFlush( myDisplay ) ;
151 ImageUtility_X11GC ImageUtility_X11Dump::X11GC() const
154 ImageUtility_X11Window ImageUtility_X11Dump::X11Window() const
155 { return myWindow ; }
157 ImageUtility_X11Display ImageUtility_X11Dump::X11Display() const
158 { return myDisplay ; }
160 ImageUtility_X11XImage ImageUtility_X11Dump::X11XImage() const
161 { return myXImage ; }
163 void ImageUtility_X11Dump::UpdateX11Colormap() const
166 if ( myImage->Type() != Image_TOI_PseudoColorImage ) return ;
168 Handle(Image_PseudoColorImage) aPImage =
169 Handle(Image_PseudoColorImage)::DownCast( myImage ) ;
170 Handle(Aspect_ColorMap) TheColorMap = aPImage->ColorMap() ;
171 Aspect_ColorMapEntry aEntry ;
174 XWindowAttributes WinAttr ;
176 XGetWindowAttributes( myDisplay, myWindow, &WinAttr ) ;
178 for ( i = 0 ; i < TheColorMap->Size() ; i++ ) {
179 aEntry = TheColorMap->Entry( i+1 ) ;
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 ;
187 XStoreColor( myDisplay, WinAttr.colormap, &aColor ) ;
191 static unsigned int get_range( unsigned long col_mask )
194 while ( !(col_mask & 1) ) col_mask >>= 1 ;
199 static unsigned int get_shift( unsigned long col_mask , unsigned long rgb_bit )
203 if ( ( col_mask >> rgb_bit ) == 0 ) {
206 else if ( ( col_mask >> ( 2 * rgb_bit ) ) == 0 ) {
210 shift = 2 * rgb_bit ;
217 void ImageUtility_X11Dump::UpdateX11XImage()
219 { Standard_Integer x,y, width, height, X, Y, bits_per_line;
221 width = myImage->Width() ;
222 height = myImage->Height() ;
224 X = myImage->LowerX() ;
225 Y = myImage->LowerY() ;
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 ;
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 ;
243 myXImage->bytes_per_line = bits_per_line / 8 ;
245 myXImage->data = ( char * )
246 Standard::Allocate(myXImage->bytes_per_line*myXImage->height);
249 if ( myImage->Type() == Image_TOI_PseudoColorImage ) {
251 Handle(Image_PseudoColorImage) aPImage =
252 Handle(Image_PseudoColorImage)::DownCast( myImage ) ;
253 Image_PixelRowOfDIndexedImage aRow( 0, myImage->Width()-1 ) ;
255 for ( y = 0 , pl = myXImage->data ;
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 ) ;
265 unsigned int *pl, *p ;
266 Handle(Image_ColorImage) aCImage =
267 Handle(Image_ColorImage)::DownCast( myImage ) ;
268 Image_PixelRowOfDColorImage aRow( 0, myImage->Width()-1 ) ;
270 XWindowAttributes WinAttr ;
271 Standard_Real r,g,b ;
272 unsigned int lr,lg,lb, rgb_range, r_shift, g_shift, b_shift ;
274 XGetWindowAttributes( myDisplay, myWindow, &WinAttr ) ;
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 ) ;
284 for ( y = 0 , pl = ( unsigned int *) myXImage->data ;
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) ;
300 void ImageUtility_X11Dump::DisplayX11XImage() const
302 { XPutImage( myDisplay,
308 myXImage->width,myXImage->height ) ;