1 // Created on: 1999-10-14
3 // Copyright (c) 1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
21 // Updated GG 06/03/00 Xw_PixMap MUST creates a Pixmap and no more an Image !!!
22 // GG IMP100701 Add the "depth" field and method
23 // to the pixmap object.
31 #include <TCollection_AsciiString.hxx>
32 #include <Image_AlienPixMap.hxx>
37 #include <Xw_PixMap.ixx>
39 #include <Xw_Window.hxx>
40 #include <Xw_Extension.h>
42 XW_STATUS Xw_save_xwd_image ( void*, void*, char* );
43 XW_STATUS Xw_save_bmp_image ( void*, void*, char* );
44 XW_STATUS Xw_save_gif_image ( void*, void*, char* );
46 Standard_Integer Xw_PixMap::PreferedDepth(
47 const Handle(Aspect_Window)& aWindow,
48 const Standard_Integer aDepth) const {
49 Handle(Xw_Window) hwindow = Handle(Xw_Window)::DownCast(aWindow);
50 XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*) hwindow->ExtendedWindow();
51 if( aDepth <= 0 ) return _DEPTH;
52 // Gets the nearest depth from possible screen depths
54 Screen *scr = DefaultScreenOfDisplay(_DISPLAY);
55 for( i=j=0 ; i<scr->ndepths ; i++ ) {
56 if( Abs(aDepth - scr->depths[i].depth) <
57 Abs(aDepth - scr->depths[j].depth) ) {
61 return scr->depths[j].depth;
65 //////////////////////////////////////////////////////////////////////////////////////////
66 Xw_PixMap::Xw_PixMap (const Handle(Aspect_Window)& theWindow,
67 const Standard_Integer theWidth,
68 const Standard_Integer theHeight,
69 const Standard_Integer theDepth)
70 : myWindow (Handle(Xw_Window)::DownCast(theWindow)),
73 myDepth (PreferedDepth (theWindow, theDepth))
75 XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*) myWindow->ExtendedWindow();
78 if( !Xw_get_trace() ) Xw_set_synchronize(_DISPLAY,True);
79 myPixmap = XCreatePixmap( _DISPLAY, _WINDOW, myWidth, myHeight, myDepth);
80 if( !Xw_get_trace() ) Xw_set_synchronize(_DISPLAY,False);
82 if ( !myPixmap || Xw_print_error() ) {
84 sprintf(errstring," *** Xw_PixMap : Cann't allocates pixmap of size %d x %d, request failed with errno : '%s'",myWidth, myHeight,strerror(errno));
85 Aspect_PixmapDefinitionError::Raise ( errstring );
89 ///////////////////////////////
95 printf(" $$$ Xw_PixMap::Destroy() %x\n",myPixmap);
97 XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*) myWindow->ExtendedWindow();
98 XFreePixmap(_DISPLAY,myPixmap);
102 ////////////////////////////////////////////////////////////
103 Standard_Boolean Xw_PixMap::Dump (const Standard_CString theFileName,
104 const Standard_Real theGammaValue) const
107 XWindowAttributes winAttr;
108 XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*) myWindow->ExtendedWindow();
109 XGetWindowAttributes (_DISPLAY, _WINDOW, &winAttr);
110 if (winAttr.visual->c_class != TrueColor)
112 std::cerr << "Visual Type not supported!";
113 return Standard_False;
116 Image_AlienPixMap anImage;
117 bool isBigEndian = Image_PixMap::IsBigEndianHost();
118 const Standard_Size aSizeRowBytes = Standard_Size(winAttr.width) * 4;
119 if (!anImage.InitTrash (isBigEndian ? Image_PixMap::ImgRGB32 : Image_PixMap::ImgBGR32,
120 Standard_Size(winAttr.width), Standard_Size(winAttr.height), aSizeRowBytes))
122 return Standard_False;
124 anImage.SetTopDown (true);
126 XImage* anXImage = XCreateImage (_DISPLAY, winAttr.visual,
127 32, ZPixmap, 0, (char* )anImage.ChangeData(), winAttr.width, winAttr.height, 32, int(aSizeRowBytes));
128 anXImage->bitmap_bit_order = anXImage->byte_order = (isBigEndian ? MSBFirst : LSBFirst);
129 if (XGetSubImage (_DISPLAY, myPixmap,
130 0, 0, winAttr.width, winAttr.height,
131 AllPlanes, ZPixmap, anXImage, 0, 0) == NULL)
133 anXImage->data = NULL;
134 XDestroyImage (anXImage);
135 return Standard_False;
139 anXImage->data = NULL;
140 XDestroyImage (anXImage);
143 if (Abs (theGammaValue - 1.0) > 0.001)
145 anImage.AdjustGamma (theGammaValue);
149 return anImage.Save (theFileName);
152 ////////////////////////////////////////////////////////////
153 Aspect_Handle Xw_PixMap::PixmapID() const
158 Quantity_Color Xw_PixMap::PixelColor (const Standard_Integer theX,
159 const Standard_Integer theY) const
162 XWindowAttributes winAttr;
163 XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*) myWindow->ExtendedWindow();
164 XGetWindowAttributes (_DISPLAY, _WINDOW, &winAttr);
167 XImage* pximage = XGetImage (_DISPLAY, myPixmap,
168 0, 0, myWidth, myHeight,
172 return Quantity_Color (0.0, 0.0, 0.0, Quantity_TOC_RGB);
176 aColor.pixel = XGetPixel (pximage, theX, theY);
177 XDestroyImage (pximage);
179 XQueryColor (_DISPLAY, myWindow->XColorMap(), &aColor);
180 return Quantity_Color (Quantity_Parameter (aColor.red) / 65535.0,
181 Quantity_Parameter (aColor.green) / 65535.0,
182 Quantity_Parameter (aColor.blue) / 65535.0,