-// Created on: 1999-10-14
-// Created by: VKH
-// Copyright (c) 1999 Matra Datavision
-// Copyright (c) 1999-2012 OPEN CASCADE SAS
-//
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
-//
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
-//
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
-
-// Updated GG 06/03/00 Xw_PixMap MUST creates a Pixmap and no more an Image !!!
-// GG IMP100701 Add the "depth" field and method
-// to the pixmap object.
-
-
-
-//-Version
-
-#define xTRACE 1
-
-#include <TCollection_AsciiString.hxx>
-#include <Image_AlienPixMap.hxx>
-
-#include <errno.h>
-#include <stdio.h>
-
-#include <Xw_PixMap.ixx>
-
-#include <Xw_Window.hxx>
-#include <Xw_Extension.h>
-
-XW_STATUS Xw_save_xwd_image ( void*, void*, char* );
-XW_STATUS Xw_save_bmp_image ( void*, void*, char* );
-XW_STATUS Xw_save_gif_image ( void*, void*, char* );
-
-Standard_Integer Xw_PixMap::PreferedDepth(
- const Handle(Aspect_Window)& aWindow,
- const Standard_Integer aDepth) const {
- Handle(Xw_Window) hwindow = Handle(Xw_Window)::DownCast(aWindow);
- XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*) hwindow->ExtendedWindow();
- if( aDepth <= 0 ) return _DEPTH;
-// Gets the nearest depth from possible screen depths
- Standard_Integer i,j;
- Screen *scr = DefaultScreenOfDisplay(_DISPLAY);
- for( i=j=0 ; i<scr->ndepths ; i++ ) {
- if( Abs(aDepth - scr->depths[i].depth) <
- Abs(aDepth - scr->depths[j].depth) ) {
- j = i;
- }
- }
- return scr->depths[j].depth;
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////////
-Xw_PixMap::Xw_PixMap (const Handle(Aspect_Window)& theWindow,
- const Standard_Integer theWidth,
- const Standard_Integer theHeight,
- const Standard_Integer theDepth)
-: myWindow (Handle(Xw_Window)::DownCast(theWindow)),
- myWidth (theWidth),
- myHeight (theHeight),
- myDepth (PreferedDepth (theWindow, theDepth))
-{
- XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*) myWindow->ExtendedWindow();
-
- Xw_print_error();
- if( !Xw_get_trace() ) Xw_set_synchronize(_DISPLAY,True);
- myPixmap = XCreatePixmap( _DISPLAY, _WINDOW, myWidth, myHeight, myDepth);
- if( !Xw_get_trace() ) Xw_set_synchronize(_DISPLAY,False);
-
- if ( !myPixmap || Xw_print_error() ) {
- char errstring[256];
- sprintf(errstring," *** Xw_PixMap : Cann't allocates pixmap of size %d x %d, request failed with errno : '%s'",myWidth, myHeight,strerror(errno));
- Aspect_PixmapDefinitionError::Raise ( errstring );
- }
-}
-
-///////////////////////////////
-void
-Xw_PixMap::Destroy ()
-{
- if( myPixmap ) {
-#ifdef TRACE
- printf(" $$$ Xw_PixMap::Destroy() %x\n",myPixmap);
-#endif
- XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*) myWindow->ExtendedWindow();
- XFreePixmap(_DISPLAY,myPixmap);
- }
-}
-
-////////////////////////////////////////////////////////////
-Standard_Boolean Xw_PixMap::Dump (const Standard_CString theFileName,
- const Standard_Real theGammaValue) const
-{
- // the attributes
- XWindowAttributes winAttr;
- XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*) myWindow->ExtendedWindow();
- XGetWindowAttributes (_DISPLAY, _WINDOW, &winAttr);
- if (winAttr.visual->c_class != TrueColor)
- {
- std::cerr << "Visual Type not supported!";
- return Standard_False;
- }
-
- Image_AlienPixMap anImage;
- bool isBigEndian = Image_PixMap::IsBigEndianHost();
- const Standard_Size aSizeRowBytes = Standard_Size(winAttr.width) * 4;
- if (!anImage.InitTrash (isBigEndian ? Image_PixMap::ImgRGB32 : Image_PixMap::ImgBGR32,
- Standard_Size(winAttr.width), Standard_Size(winAttr.height), aSizeRowBytes))
- {
- return Standard_False;
- }
- anImage.SetTopDown (true);
-
- XImage* anXImage = XCreateImage (_DISPLAY, winAttr.visual,
- 32, ZPixmap, 0, (char* )anImage.ChangeData(), winAttr.width, winAttr.height, 32, int(aSizeRowBytes));
- anXImage->bitmap_bit_order = anXImage->byte_order = (isBigEndian ? MSBFirst : LSBFirst);
- if (XGetSubImage (_DISPLAY, myPixmap,
- 0, 0, winAttr.width, winAttr.height,
- AllPlanes, ZPixmap, anXImage, 0, 0) == NULL)
- {
- anXImage->data = NULL;
- XDestroyImage (anXImage);
- return Standard_False;
- }
-
- // destroy the image
- anXImage->data = NULL;
- XDestroyImage (anXImage);
-
- // save the image
- if (Abs (theGammaValue - 1.0) > 0.001)
- {
- anImage.AdjustGamma (theGammaValue);
- }
-
- // save the image
- return anImage.Save (theFileName);
-}
-
-////////////////////////////////////////////////////////////
-Aspect_Handle Xw_PixMap::PixmapID() const
-{
- return myPixmap;
-}
-
-Quantity_Color Xw_PixMap::PixelColor (const Standard_Integer theX,
- const Standard_Integer theY) const
-{
- // the attributes
- XWindowAttributes winAttr;
- XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW*) myWindow->ExtendedWindow();
- XGetWindowAttributes (_DISPLAY, _WINDOW, &winAttr);
-
- // find the image
- XImage* pximage = XGetImage (_DISPLAY, myPixmap,
- 0, 0, myWidth, myHeight,
- AllPlanes, ZPixmap);
- if (pximage == NULL)
- {
- return Quantity_Color (0.0, 0.0, 0.0, Quantity_TOC_RGB);
- }
-
- XColor aColor;
- aColor.pixel = XGetPixel (pximage, theX, theY);
- XDestroyImage (pximage);
-
- XQueryColor (_DISPLAY, myWindow->XColorMap(), &aColor);
- return Quantity_Color (Quantity_Parameter (aColor.red) / 65535.0,
- Quantity_Parameter (aColor.green) / 65535.0,
- Quantity_Parameter (aColor.blue) / 65535.0,
- Quantity_TOC_RGB);
-}