1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/bitmap.cpp
4 // Author: Julian Smart, Robert Roebling
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #include "wx/bitmap.h"
20 #include "wx/dcmemory.h"
26 #include "wx/x11/private.h"
28 /* No point in using libXPM for NanoX */
31 #define wxHAVE_LIB_XPM 0
33 // Copy from the drawable to the wxImage
34 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
);
41 #include "wx/xpmdecod.h"
42 #include "wx/wfstream.h"
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
58 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
61 Create( bitmap
, colour
);
64 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
67 Create( bitmap
, paletteIndex
);
70 wxMask::wxMask( const wxBitmap
& bitmap
)
79 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
82 bool wxMask::Create( const wxBitmap
& bitmap
,
83 const wxColour
& colour
)
88 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
92 m_display
= bitmap
.GetDisplay();
94 wxImage image
= bitmap
.ConvertToImage();
95 if (!image
.Ok()) return false;
97 m_display
= bitmap
.GetDisplay();
99 Display
*xdisplay
= (Display
*) m_display
;
100 int xscreen
= DefaultScreen( xdisplay
);
101 Window xroot
= RootWindow( xdisplay
, xscreen
);
103 m_bitmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, image
.GetWidth(), image
.GetHeight(), 1 );
104 GC gc
= XCreateGC( xdisplay
, (Pixmap
) m_bitmap
, 0, NULL
);
106 XSetForeground( xdisplay
, gc
, WhitePixel(xdisplay
,xscreen
) );
107 XSetFillStyle( xdisplay
, gc
, FillSolid
);
108 XFillRectangle( xdisplay
, (Pixmap
) m_bitmap
, gc
, 0, 0, image
.GetWidth(), image
.GetHeight() );
110 unsigned char *data
= image
.GetData();
113 unsigned char red
= colour
.Red();
114 unsigned char green
= colour
.Green();
115 unsigned char blue
= colour
.Blue();
117 int bpp
= wxTheApp
->GetVisualInfo(m_display
)->m_visualDepth
;
138 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
140 int width
= image
.GetWidth();
141 int height
= image
.GetHeight();
142 for (int j
= 0; j
< height
; j
++)
146 for (i
= 0; i
< width
; i
++)
148 if ((data
[index
] == red
) &&
149 (data
[index
+1] == green
) &&
150 (data
[index
+2] == blue
))
159 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
166 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
169 XFreeGC( xdisplay
, gc
);
178 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
181 wxPalette
*pal
= bitmap
.GetPalette();
183 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
185 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
187 return Create(bitmap
, wxColour(r
, g
, b
));
190 bool wxMask::Create( const wxBitmap
& bitmap
)
195 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
199 if (!bitmap
.Ok()) return false;
201 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
203 m_display
= bitmap
.GetDisplay();
205 int xscreen
= DefaultScreen( (Display
*) m_display
);
206 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
208 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
210 if (!m_bitmap
) return false;
212 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
214 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
215 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
217 XFreeGC( (Display
*) m_display
, gc
);
226 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
230 class wxBitmapRefData
: public wxGDIRefData
234 wxBitmapRefData(const wxBitmapRefData
& data
);
235 virtual ~wxBitmapRefData();
237 // shouldn't be called more than once as it doesn't free the existing data
238 bool Create(int width
, int height
, int depth
);
240 virtual bool IsOk() const { return m_pixmap
|| m_bitmap
; }
249 wxPalette
*m_palette
;
252 wxBitmapRefData::wxBitmapRefData()
264 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
268 m_display
= data
.m_display
;
269 m_mask
= NULL
; // FIXME: should copy
270 m_palette
= NULL
; // FIXME: should copy
272 Create(data
.m_width
, data
.m_height
, data
.m_bpp
);
275 bool wxBitmapRefData::Create(int width
, int height
, int depth
)
281 m_display
= wxGlobalDisplay();
283 wxCHECK_MSG( m_display
, false, wxT("No display") );
285 int xscreen
= DefaultScreen(m_display
);
286 int bpp
= DefaultDepth(m_display
, xscreen
);
290 wxCHECK_MSG( (depth
== bpp
) || (depth
== 1), false,
291 wxT("invalid bitmap depth") );
294 m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
295 #else // !wxUSE_NANOX
296 Window xroot
= RootWindow(m_display
, xscreen
);
298 *(depth
== 1 ? &m_bitmap
: &m_pixmap
) =
299 XCreatePixmap(m_display
, xroot
, width
, height
, depth
);
300 #endif // wxUSE_NANOX/!wxUSE_NANOX
302 wxCHECK_MSG( m_pixmap
|| m_bitmap
, false, wxT("Bitmap creation failed") );
307 wxBitmapRefData::~wxBitmapRefData()
310 XFreePixmap(m_display
, m_pixmap
);
312 XFreePixmap(m_display
, m_bitmap
);
317 //-----------------------------------------------------------------------------
321 static WXPixmap
wxGetSubPixmap( WXDisplay
* xdisplay
, WXPixmap xpixmap
,
322 int x
, int y
, int width
, int height
,
325 Display
* const dpy
= (Display
*)xdisplay
;
327 int xscreen
= DefaultScreen( dpy
);
328 Window xroot
= RootWindow( dpy
, xscreen
);
329 Visual
* xvisual
= DefaultVisual( dpy
, xscreen
);
331 XImage
* ximage
= XCreateImage( dpy
, xvisual
, depth
,
332 ZPixmap
, 0, 0, width
, height
, 32, 0 );
333 ximage
->data
= (char*)malloc( ximage
->bytes_per_line
* ximage
->height
);
334 ximage
= XGetSubImage( dpy
, (Pixmap
)xpixmap
,
336 AllPlanes
, ZPixmap
, ximage
, 0, 0 );
338 GC gc
= XCreateGC( dpy
, (Pixmap
)xpixmap
, 0, NULL
);
339 Pixmap ret
= XCreatePixmap( dpy
, xroot
,
340 width
, height
, depth
);
342 XPutImage( dpy
, ret
, gc
, ximage
,
343 0, 0, 0, 0, width
, height
);
344 XDestroyImage( ximage
);
347 return (WXPixmap
)ret
;
351 //-----------------------------------------------------------------------------
353 //-----------------------------------------------------------------------------
355 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
357 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
359 bool wxBitmap::Create( int width
, int height
, int depth
)
363 wxCHECK_MSG( (width
> 0) && (height
> 0), false, wxT("invalid bitmap size") );
365 m_refData
= new wxBitmapRefData();
367 return M_BMPDATA
->Create(width
, height
, depth
);
370 bool wxBitmap::Create(const void* data
, wxBitmapType type
,
371 int width
, int height
, int depth
)
375 wxBitmapHandler
*handler
= FindHandler(type
);
377 if ( handler
== NULL
) {
378 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
384 return handler
->Create(this, data
, type
, width
, height
, depth
);
387 bool wxBitmap::Create(WXPixmap pixmap
)
390 Pixmap xpixmap
= (Pixmap
)pixmap
;
391 Display
* xdisplay
= wxGlobalDisplay();
392 int xscreen
= DefaultScreen( xdisplay
);
393 Window xroot
= RootWindow( xdisplay
, xscreen
);
395 // make a copy of the Pixmap
398 unsigned width
, height
, border
, depth
;
400 XGetGeometry( xdisplay
, (Drawable
)xpixmap
, &root
, &x
, &y
,
401 &width
, &height
, &border
, &depth
);
402 Pixmap copy
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
404 GC gc
= XCreateGC( xdisplay
, copy
, 0, NULL
);
405 XCopyArea( xdisplay
, xpixmap
, copy
, gc
, 0, 0, width
, height
, 0, 0 );
406 XFreeGC( xdisplay
, gc
);
409 wxBitmapRefData
* ref
= new wxBitmapRefData();
412 ref
->m_bitmap
= copy
;
414 ref
->m_pixmap
= copy
;
416 ref
->m_display
= xdisplay
;
417 ref
->m_width
= width
;
418 ref
->m_height
= height
;
426 wxBitmap::wxBitmap(const char* const* bits
)
428 Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
431 wxGDIRefData
*wxBitmap::CreateGDIRefData() const
433 return new wxBitmapRefData
;
436 wxGDIRefData
*wxBitmap::CloneGDIRefData(const wxGDIRefData
*data
) const
438 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
441 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
446 wxASSERT_MSG(image
.Ok(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
450 int w
= image
.GetWidth();
451 int h
= image
.GetHeight();
453 if (!Create(w
, h
, depth
))
456 // Unfortunately the mask has to be screen-depth since
457 // 1-bpp bitmaps don't seem to be supported
458 // TODO: implement transparent drawing, presumably
459 // by doing several blits as per the Windows
460 // implementation because Nano-X doesn't support
462 // TODO: could perhaps speed this function up
463 // by making a buffer of pixel values,
464 // and then calling GrArea to write that to the
465 // pixmap. See demos/nxroach.c.
467 bool hasMask
= image
.HasMask();
469 GC pixmapGC
= GrNewGC();
470 Pixmap pixmap
= (Pixmap
) GetPixmap();
473 Pixmap maskPixmap
= 0;
475 unsigned char maskR
= 0;
476 unsigned char maskG
= 0;
477 unsigned char maskB
= 0;
481 maskR
= image
.GetMaskRed();
482 maskG
= image
.GetMaskGreen();
483 maskB
= image
.GetMaskBlue();
486 maskPixmap
= GrNewPixmap(w
, h
, 0);
491 wxMask
* mask
= new wxMask
;
492 mask
->SetBitmap((WXPixmap
) maskPixmap
);
497 GR_COLOR lastPixmapColour
= 0;
498 GR_COLOR lastMaskColour
= 0;
501 for (i
= 0; i
< w
; i
++)
503 for (j
= 0; j
< h
; j
++)
505 unsigned char red
= image
.GetRed(i
, j
);
506 unsigned char green
= image
.GetGreen(i
, j
);
507 unsigned char blue
= image
.GetBlue(i
, j
);
509 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
511 // Efficiency measure
512 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
514 GrSetGCForeground(pixmapGC
, colour
);
515 lastPixmapColour
= colour
;
518 GrPoint(pixmap
, pixmapGC
, i
, j
);
522 // scan the bitmap for the transparent colour and set the corresponding
523 // pixels in the mask to BLACK and the rest to WHITE
524 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
526 colour
= GR_RGB(0, 0, 0);
530 colour
= GR_RGB(255, 255, 255);
532 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
534 GrSetGCForeground(maskGC
, colour
);
535 lastMaskColour
= colour
;
537 GrPoint(maskPixmap
, maskGC
, i
, j
);
542 GrDestroyGC(pixmapGC
);
552 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
553 wxCHECK_MSG( depth
== -1, false, wxT("invalid bitmap depth") );
555 m_refData
= new wxBitmapRefData();
557 M_BMPDATA
->m_display
= wxGlobalDisplay();
559 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
561 int xscreen
= DefaultScreen( xdisplay
);
562 Window xroot
= RootWindow( xdisplay
, xscreen
);
563 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
565 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
567 int width
= image
.GetWidth();
568 int height
= image
.GetHeight();
569 M_BMPDATA
->m_width
= width
;
570 M_BMPDATA
->m_height
= height
;
572 if (depth
!= 1) depth
= bpp
;
573 M_BMPDATA
->m_bpp
= depth
;
577 wxFAIL_MSG( wxT("mono images later") );
583 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
584 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
586 if (data_image
->data
== NULL
)
588 wxLogError( wxT("Out of memory.") ); // TODO clean
592 M_BMPDATA
->m_pixmap
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
594 // Create mask if necessary
595 const bool hasMask
= image
.HasMask();
597 XImage
*mask_image
= NULL
;
600 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
601 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
603 if (mask_image
->data
== NULL
)
605 wxLogError( wxT("Out of memory.") ); // TODO clean
609 wxMask
*mask
= new wxMask();
610 mask
->SetDisplay( xdisplay
);
611 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
616 if (bpp
< 8) bpp
= 8;
620 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
621 byte_order b_o
= RGB
;
623 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
624 unsigned long greenMask
= vi
->m_visualGreenMask
,
625 redMask
= vi
->m_visualRedMask
,
626 blueMask
= vi
->m_visualBlueMask
;
630 if ((redMask
> greenMask
) && (greenMask
> blueMask
)) b_o
= RGB
;
631 else if ((redMask
> blueMask
) && (blueMask
> greenMask
)) b_o
= RBG
;
632 else if ((blueMask
> redMask
) && (redMask
> greenMask
)) b_o
= BRG
;
633 else if ((blueMask
> greenMask
) && (greenMask
> redMask
))b_o
= BGR
;
634 else if ((greenMask
> redMask
) && (redMask
> blueMask
)) b_o
= GRB
;
635 else if ((greenMask
> blueMask
) && (blueMask
> redMask
)) b_o
= GBR
;
638 int r_mask
= image
.GetMaskRed();
639 int g_mask
= image
.GetMaskGreen();
640 int b_mask
= image
.GetMaskBlue();
642 unsigned char* data
= image
.GetData();
643 wxASSERT_MSG( data
, wxT("No image data") );
645 unsigned char *colorCube
=
646 wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_colorCube
;
649 for (int y
= 0; y
< height
; y
++)
651 for (int x
= 0; x
< width
; x
++)
662 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
663 XPutPixel( mask_image
, x
, y
, 0 );
665 XPutPixel( mask_image
, x
, y
, 1 );
673 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
674 XPutPixel( data_image
, x
, y
, pixel
);
682 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
683 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
684 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
685 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
686 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
687 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
689 XPutPixel( data_image
, x
, y
, pixel
);
697 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
698 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
699 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
700 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
701 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
702 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
704 XPutPixel( data_image
, x
, y
, pixel
);
709 // I actually don't know if for 16-bit displays, it is alway the green
710 // component or the second component which has 6 bits.
714 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
715 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
716 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
717 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
718 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
719 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
721 XPutPixel( data_image
, x
, y
, pixel
);
730 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
731 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
732 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
733 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
734 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
735 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
737 XPutPixel( data_image
, x
, y
, pixel
);
746 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
747 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
748 XDestroyImage( data_image
);
749 XFreeGC( xdisplay
, gc
);
755 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
756 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
758 XDestroyImage( mask_image
);
759 XFreeGC( xdisplay
, gc
);
768 wxImage
wxBitmap::ConvertToImage() const
772 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
774 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
775 wxASSERT_MSG( xdisplay
, wxT("No display") );
778 //int bpp = DefaultDepth(xdisplay, xscreen);
779 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
783 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
784 XImage
*x_image
= NULL
;
787 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
789 GetWidth(), GetHeight(),
790 AllPlanes
, ZPixmap
);
794 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
796 GetWidth(), GetHeight(),
797 AllPlanes
, ZPixmap
);
800 wxFAIL_MSG( wxT("Ill-formed bitmap") );
803 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
805 image
.Create( GetWidth(), GetHeight() );
806 char unsigned *data
= image
.GetData();
810 XDestroyImage( x_image
);
811 wxFAIL_MSG( wxT("couldn't create image") );
815 XImage
*x_image_mask
= NULL
;
818 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
820 GetWidth(), GetHeight(),
821 AllPlanes
, ZPixmap
);
823 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
826 int red_shift_right
= 0;
827 int green_shift_right
= 0;
828 int blue_shift_right
= 0;
829 int red_shift_left
= 0;
830 int green_shift_left
= 0;
831 int blue_shift_left
= 0;
832 bool use_shift
= false;
836 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
838 red_shift_right
= vi
->m_visualRedShift
;
839 red_shift_left
= 8 - vi
->m_visualRedPrec
;
840 green_shift_right
= vi
->m_visualGreenShift
;
841 green_shift_left
= 8 - vi
->m_visualGreenPrec
;
842 blue_shift_right
= vi
->m_visualBlueShift
;
843 blue_shift_left
= 8 - vi
->m_visualBluePrec
;
845 use_shift
= (vi
->m_visualType
== GrayScale
) ||
846 (vi
->m_visualType
!= PseudoColor
);
854 XColor
*colors
= (XColor
*)wxTheApp
->
855 GetVisualInfo(M_BMPDATA
->m_display
)->m_visualColormap
;
857 int width
= GetWidth();
858 int height
= GetHeight();
860 for (int j
= 0; j
< height
; j
++)
862 for (int i
= 0; i
< width
; i
++)
864 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
882 data
[pos
] = (unsigned char)((pixel
>> red_shift_right
) << red_shift_left
);
883 data
[pos
+1] = (unsigned char)((pixel
>> green_shift_right
) << green_shift_left
);
884 data
[pos
+2] = (unsigned char)((pixel
>> blue_shift_right
) << blue_shift_left
);
888 data
[pos
] = (unsigned char)(colors
[pixel
].red
>> 8);
889 data
[pos
+1] = (unsigned char)(colors
[pixel
].green
>> 8);
890 data
[pos
+2] = (unsigned char)(colors
[pixel
].blue
>> 8);
894 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
899 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
912 XDestroyImage( x_image
);
913 if (x_image_mask
) XDestroyImage( x_image_mask
);
919 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
921 LoadFile( filename
, type
);
924 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
926 m_refData
= new wxBitmapRefData
;
928 (void) Create(bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
931 wxBitmap::~wxBitmap()
935 int wxBitmap::GetHeight() const
937 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
939 return M_BMPDATA
->m_height
;
942 int wxBitmap::GetWidth() const
944 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
946 return M_BMPDATA
->m_width
;
949 int wxBitmap::GetDepth() const
951 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
953 return M_BMPDATA
->m_bpp
;
956 wxMask
*wxBitmap::GetMask() const
958 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
960 return M_BMPDATA
->m_mask
;
963 void wxBitmap::SetMask( wxMask
*mask
)
965 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
968 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
970 M_BMPDATA
->m_mask
= mask
;
973 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
979 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
982 (rect
.x
>= 0) && (rect
.y
>= 0) &&
983 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
984 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
985 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
987 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
988 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
992 wxMask
* mask
= new wxMask();
993 mask
->SetDisplay( GetMask()->GetDisplay() );
994 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
995 GetMask()->GetBitmap(),
997 rect
.width
, rect
.height
,
1000 ret
.SetMask( mask
);
1005 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
1008 rect
.width
, rect
.height
,
1009 M_BMPDATA
->m_bpp
) );
1014 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1017 rect
.width
, rect
.height
,
1024 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1025 const wxPalette
*palette
) const
1027 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1029 wxBitmapHandler
*handler
= FindHandler(type
);
1031 // Try to save the bitmap via wxImage handlers:
1032 if (handler
== NULL
)
1034 wxImage
image(this->ConvertToImage());
1035 if (image
.Ok()) return image
.SaveFile( name
, type
);
1040 return handler
->SaveFile(this, name
, type
, palette
);
1043 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1047 if (!wxFileExists(name
)) return false;
1049 wxBitmapHandler
*handler
= FindHandler(type
);
1051 if (handler
== NULL
)
1054 if (!image
.LoadFile( name
, type
))
1059 *this = wxBitmap(image
);
1065 return handler
->LoadFile(this, name
, type
, -1, -1);
1068 void wxBitmap::SetPalette(const wxPalette
& palette
)
1070 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1071 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1072 wxT("cannot set palette for bitmap of this depth"));
1075 wxDELETE(M_BMPDATA
->m_palette
);
1077 if (!palette
.Ok()) return;
1079 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1082 wxPalette
*wxBitmap::GetPalette() const
1084 if (!Ok()) return NULL
;
1086 return M_BMPDATA
->m_palette
;
1089 void wxBitmap::SetHeight( int height
)
1093 M_BMPDATA
->m_height
= height
;
1096 void wxBitmap::SetWidth( int width
)
1100 M_BMPDATA
->m_width
= width
;
1103 void wxBitmap::SetDepth( int depth
)
1107 M_BMPDATA
->m_bpp
= depth
;
1110 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1112 if (!m_refData
) m_refData
= new wxBitmapRefData();
1114 M_BMPDATA
->m_pixmap
= (Pixmap
)pixmap
;
1117 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1119 if (!m_refData
) m_refData
= new wxBitmapRefData();
1121 M_BMPDATA
->m_bitmap
= (Pixmap
)bitmap
;
1124 WXPixmap
wxBitmap::GetPixmap() const
1126 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1128 return (WXPixmap
)M_BMPDATA
->m_pixmap
;
1131 WXPixmap
wxBitmap::GetBitmap() const
1133 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1135 return (WXPixmap
)M_BMPDATA
->m_bitmap
;
1138 WXPixmap
wxBitmap::GetDrawable() const
1140 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1142 return (WXPixmap
)(M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
1143 : M_BMPDATA
->m_pixmap
);
1146 WXDisplay
*wxBitmap::GetDisplay() const
1148 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1150 return M_BMPDATA
->m_display
;
1154 // Copy from the drawable to the wxImage
1155 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1157 GR_SCREEN_INFO sinfo
;
1159 GR_PIXELVAL
*pixels
;
1160 GR_PALETTE
* palette
= NULL
;
1161 unsigned char rgb
[3], *pp
;
1163 GrGetScreenInfo(&sinfo
);
1165 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1166 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1169 GrGetSystemPalette(palette
);
1172 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1177 image
.Create(width
, height
);
1179 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1183 for(x
= 0; x
< sinfo
.cols
; x
++) {
1185 pp
= (unsigned char *)pixels
+
1186 ((x
+ (y
* sinfo
.cols
)) *
1187 sizeof(GR_PIXELVAL
));
1189 switch(sinfo
.pixtype
) {
1190 /* FIXME: These may need modifying on big endian. */
1191 case MWPF_TRUECOLOR0888
:
1192 case MWPF_TRUECOLOR888
:
1198 rgb
[0] = palette
->palette
[pp
[0]].r
;
1199 rgb
[1] = palette
->palette
[pp
[0]].g
;
1200 rgb
[2] = palette
->palette
[pp
[0]].b
;
1202 case MWPF_TRUECOLOR565
:
1203 rgb
[0] = pp
[1] & 0xf8;
1204 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1205 ((pp
[0] & 0xe0) >> 3);
1206 rgb
[2] = (pp
[0] & 0x1f) << 3;
1208 case MWPF_TRUECOLOR555
:
1209 rgb
[0] = (pp
[1] & 0x7c) << 1;
1210 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1211 ((pp
[0] & 0xe0) >> 2);
1212 rgb
[2] = (pp
[0] & 0x1f) << 3;
1214 case MWPF_TRUECOLOR332
:
1215 rgb
[0] = pp
[0] & 0xe0;
1216 rgb
[1] = (pp
[0] & 0x1c) << 3;
1217 rgb
[2] = (pp
[0] & 0x03) << 6;
1220 fprintf(stderr
, "Unsupported pixel "
1225 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1230 if(palette
) free(palette
);
1236 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1237 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1239 unsigned char rgb
[3], *pp
;
1241 pp
= (unsigned char*) & pixel
;
1243 switch (sinfo
.pixtype
)
1245 /* FIXME: These may need modifying on big endian. */
1246 case MWPF_TRUECOLOR0888
:
1247 case MWPF_TRUECOLOR888
:
1253 rgb
[0] = palette
->palette
[pp
[0]].r
;
1254 rgb
[1] = palette
->palette
[pp
[0]].g
;
1255 rgb
[2] = palette
->palette
[pp
[0]].b
;
1257 case MWPF_TRUECOLOR565
:
1258 rgb
[0] = pp
[1] & 0xf8;
1259 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1260 ((pp
[0] & 0xe0) >> 3);
1261 rgb
[2] = (pp
[0] & 0x1f) << 3;
1263 case MWPF_TRUECOLOR555
:
1264 rgb
[0] = (pp
[1] & 0x7c) << 1;
1265 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1266 ((pp
[0] & 0xe0) >> 2);
1267 rgb
[2] = (pp
[0] & 0x1f) << 3;
1269 case MWPF_TRUECOLOR332
:
1270 rgb
[0] = pp
[0] & 0xe0;
1271 rgb
[1] = (pp
[0] & 0x1c) << 3;
1272 rgb
[2] = (pp
[0] & 0x03) << 6;
1275 fprintf(stderr
, "Unsupported pixel format\n");
1290 // ============================================================================
1292 // ============================================================================
1294 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1298 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1300 // ----------------------------------------------------------------------------
1302 // ----------------------------------------------------------------------------
1304 class wxXPMFileHandler
: public wxBitmapHandler
1309 SetName( wxT("XPM file") );
1310 SetExtension( wxT("xpm") );
1311 SetType( wxBITMAP_TYPE_XPM
);
1314 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1316 int desiredWidth
, int desiredHeight
);
1318 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1320 const wxPalette
*palette
= NULL
) const;
1322 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
),
1323 const void* WXUNUSED(data
),
1324 wxBitmapType
WXUNUSED(flags
),
1325 int WXUNUSED(width
),
1326 int WXUNUSED(height
),
1327 int WXUNUSED(depth
) = 1)
1330 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1333 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1335 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
,
1336 const wxString
& name
,
1337 wxBitmapType
WXUNUSED(flags
),
1338 int WXUNUSED(desiredWidth
),
1339 int WXUNUSED(desiredHeight
))
1342 if (!bitmap
->GetRefData())
1343 bitmap
->SetRefData( new wxBitmapRefData() );
1345 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1347 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1349 int xscreen
= DefaultScreen( xdisplay
);
1350 Window xroot
= RootWindow( xdisplay
, xscreen
);
1352 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1354 XpmAttributes xpmAttr
;
1355 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1360 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1361 (char*) ((const char*) name
.c_str()),
1362 &pixmap
, &mask
, &xpmAttr
);
1364 if (ErrorStatus
== XpmSuccess
)
1366 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1367 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1369 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1371 XpmFreeAttributes(&xpmAttr
);
1373 M_BMPHANDLERDATA
->m_bitmap
= (Pixmap
) pixmap
;
1377 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1378 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1379 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1391 wxXPMDecoder decoder
;
1392 wxFileInputStream
stream(name
);
1395 wxImage
image(decoder
.ReadFile(stream
));
1396 return image
.IsOk() && bitmap
->CreateFromImage(image
);
1400 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1402 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1405 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
,
1406 const wxString
& name
,
1408 const wxPalette
*WXUNUSED(palette
)) const
1410 wxImage
image(bitmap
->ConvertToImage());
1412 return image
.SaveFile( name
, type
);
1417 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1419 // ----------------------------------------------------------------------------
1421 // ----------------------------------------------------------------------------
1423 class wxXPMDataHandler
: public wxBitmapHandler
1425 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1429 SetName( wxT("XPM data") );
1430 SetExtension( wxT("xpm") );
1431 SetType( wxBITMAP_TYPE_XPM_DATA
);
1434 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1435 const wxString
& WXUNUSED(name
),
1436 wxBitmapType
WXUNUSED(flags
),
1437 int WXUNUSED(desiredWidth
),
1438 int WXUNUSED(desiredHeight
))
1441 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1442 const wxString
& WXUNUSED(name
),
1443 wxBitmapType
WXUNUSED(type
),
1444 const wxPalette
*WXUNUSED(palette
) = NULL
) const
1447 virtual bool Create(wxBitmap
*bitmap
, const void* data
, wxBitmapType flags
,
1448 int width
, int height
, int depth
= 1);
1451 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1453 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, const void* bits
,
1454 wxBitmapType
WXUNUSED(flags
),
1455 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
))
1458 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
1460 if (!bitmap
->GetRefData())
1461 bitmap
->SetRefData( new wxBitmapRefData() );
1463 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1465 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1467 int xscreen
= DefaultScreen( xdisplay
);
1468 Window xroot
= RootWindow( xdisplay
, xscreen
);
1470 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1472 XpmAttributes xpmAttr
;
1473 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1478 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1479 &pixmap
, &mask
, &xpmAttr
);
1481 if (ErrorStatus
== XpmSuccess
)
1483 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1484 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1486 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1489 unsigned int depthRet
;
1491 unsigned int widthRet
, heightRet
, borderWidthRet
;
1492 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1493 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1495 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1496 #endif // wxDEBUG_LEVEL
1498 XpmFreeAttributes(&xpmAttr
);
1500 M_BMPHANDLERDATA
->m_pixmap
= (Pixmap
) pixmap
;
1504 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1505 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1506 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1516 #else // !wxHAVE_LIB_XPM
1517 wxXPMDecoder decoder
;
1518 wxImage
image(decoder
.ReadData((const char **)bits
));
1519 return image
.Ok() && bitmap
->CreateFromImage(image
);
1520 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1525 // ----------------------------------------------------------------------------
1527 // ----------------------------------------------------------------------------
1529 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1531 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1533 inline wxXBMDataHandler()
1535 SetName( wxT("XBM data") );
1536 SetExtension( wxT("xbm") );
1537 SetType( wxBITMAP_TYPE_XBM_DATA
);
1540 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1541 const wxString
& WXUNUSED(name
),
1542 wxBitmapType
WXUNUSED(flags
),
1543 int WXUNUSED(desiredWidth
),
1544 int WXUNUSED(desiredHeight
))
1547 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1548 const wxString
& WXUNUSED(name
),
1549 wxBitmapType
WXUNUSED(type
),
1550 const wxPalette
*WXUNUSED(palette
) = NULL
) const
1553 virtual bool Create(wxBitmap
*bitmap
, const void* data
, wxBitmapType type
,
1554 int width
, int height
, int depth
= 1);
1557 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
1559 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, const void* bits
,
1560 wxBitmapType
WXUNUSED(type
),
1561 int width
, int height
, int WXUNUSED(depth
))
1564 if (!bitmap
->GetRefData())
1565 bitmap
->SetRefData( new wxBitmapRefData() );
1567 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1569 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1571 int xscreen
= DefaultScreen( xdisplay
);
1572 Window xroot
= RootWindow( xdisplay
, xscreen
);
1574 M_BMPHANDLERDATA
->m_mask
= NULL
;
1575 M_BMPHANDLERDATA
->m_bitmap
=
1576 XCreateBitmapFromData(xdisplay
, xroot
,
1577 (char *) bits
, width
, height
);
1578 M_BMPHANDLERDATA
->m_width
= width
;
1579 M_BMPHANDLERDATA
->m_height
= height
;
1580 M_BMPHANDLERDATA
->m_bpp
= 1;
1584 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, false,
1585 wxT("couldn't create bitmap") );
1589 void wxBitmap::InitStandardHandlers()
1591 AddHandler(new wxXBMDataHandler
);
1593 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1594 AddHandler(new wxXPMFileHandler
);
1596 AddHandler(new wxXPMDataHandler
);