1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/bitmap.cpp
4 // Author: Julian Smart, Robert Roebling
7 // Copyright: (c) Julian Smart, Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // for compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #include "wx/bitmap.h"
19 #include "wx/dcmemory.h"
25 #include "wx/x11/private.h"
27 /* No point in using libXPM for NanoX */
30 #define wxHAVE_LIB_XPM 0
32 // Copy from the drawable to the wxImage
33 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
);
36 static WXPixmap
wxGetSubPixmap( WXDisplay
* xdisplay
, WXPixmap xpixmap
,
37 int x
, int y
, int width
, int height
,
44 #include "wx/xpmdecod.h"
45 #include "wx/wfstream.h"
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
61 wxMask::wxMask(const wxMask
& mask
)
63 m_display
= mask
.m_display
;
72 // Duplicate the mask bitmap using the existing wxGetSubPixmap() function.
73 // There are probably/surely better ways to do it.
74 m_bitmap
= wxGetSubPixmap(m_display
, mask
.m_bitmap
,
75 0, 0, m_size
.x
, m_size
.y
,
79 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
82 Create( bitmap
, colour
);
85 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
88 Create( bitmap
, paletteIndex
);
91 wxMask::wxMask( const wxBitmap
& bitmap
)
100 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
103 bool wxMask::Create( const wxBitmap
& bitmap
,
104 const wxColour
& colour
)
106 m_size
= bitmap
.GetSize();
111 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
115 m_display
= bitmap
.GetDisplay();
117 wxImage image
= bitmap
.ConvertToImage();
118 if (!image
.IsOk()) return false;
120 m_display
= bitmap
.GetDisplay();
122 Display
*xdisplay
= (Display
*) m_display
;
123 int xscreen
= DefaultScreen( xdisplay
);
124 Window xroot
= RootWindow( xdisplay
, xscreen
);
126 m_bitmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, image
.GetWidth(), image
.GetHeight(), 1 );
127 GC gc
= XCreateGC( xdisplay
, (Pixmap
) m_bitmap
, 0, NULL
);
129 XSetForeground( xdisplay
, gc
, WhitePixel(xdisplay
,xscreen
) );
130 XSetFillStyle( xdisplay
, gc
, FillSolid
);
131 XFillRectangle( xdisplay
, (Pixmap
) m_bitmap
, gc
, 0, 0, image
.GetWidth(), image
.GetHeight() );
133 unsigned char *data
= image
.GetData();
136 unsigned char red
= colour
.Red();
137 unsigned char green
= colour
.Green();
138 unsigned char blue
= colour
.Blue();
140 int bpp
= wxTheApp
->GetVisualInfo(m_display
)->m_visualDepth
;
161 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
163 int width
= image
.GetWidth();
164 int height
= image
.GetHeight();
165 for (int j
= 0; j
< height
; j
++)
169 for (i
= 0; i
< width
; i
++)
171 if ((data
[index
] == red
) &&
172 (data
[index
+1] == green
) &&
173 (data
[index
+2] == blue
))
182 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
189 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
192 XFreeGC( xdisplay
, gc
);
201 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
204 wxPalette
*pal
= bitmap
.GetPalette();
206 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
208 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
210 return Create(bitmap
, wxColour(r
, g
, b
));
213 bool wxMask::Create( const wxBitmap
& bitmap
)
218 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
222 if (!bitmap
.IsOk()) return false;
224 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
226 m_display
= bitmap
.GetDisplay();
228 int xscreen
= DefaultScreen( (Display
*) m_display
);
229 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
231 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
233 if (!m_bitmap
) return false;
235 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
237 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
238 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
240 XFreeGC( (Display
*) m_display
, gc
);
249 //-----------------------------------------------------------------------------
251 //-----------------------------------------------------------------------------
253 class wxBitmapRefData
: public wxGDIRefData
257 wxBitmapRefData(const wxBitmapRefData
& data
);
258 virtual ~wxBitmapRefData();
260 // shouldn't be called more than once as it doesn't free the existing data
261 bool Create(int width
, int height
, int depth
);
263 virtual bool IsOk() const { return m_pixmap
|| m_bitmap
; }
272 wxPalette
*m_palette
;
275 wxBitmapRefData::wxBitmapRefData()
287 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
291 m_display
= data
.m_display
;
292 m_mask
= NULL
; // FIXME: should copy
293 m_palette
= NULL
; // FIXME: should copy
295 Create(data
.m_width
, data
.m_height
, data
.m_bpp
);
298 bool wxBitmapRefData::Create(int width
, int height
, int depth
)
304 m_display
= wxGlobalDisplay();
306 wxCHECK_MSG( m_display
, false, wxT("No display") );
308 int xscreen
= DefaultScreen(m_display
);
309 int bpp
= DefaultDepth(m_display
, xscreen
);
313 wxCHECK_MSG( (depth
== bpp
) || (depth
== 1), false,
314 wxT("invalid bitmap depth") );
317 m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
318 #else // !wxUSE_NANOX
319 Window xroot
= RootWindow(m_display
, xscreen
);
321 *(depth
== 1 ? &m_bitmap
: &m_pixmap
) =
322 XCreatePixmap(m_display
, xroot
, width
, height
, depth
);
323 #endif // wxUSE_NANOX/!wxUSE_NANOX
325 wxCHECK_MSG( m_pixmap
|| m_bitmap
, false, wxT("Bitmap creation failed") );
330 wxBitmapRefData::~wxBitmapRefData()
333 XFreePixmap(m_display
, m_pixmap
);
335 XFreePixmap(m_display
, m_bitmap
);
340 //-----------------------------------------------------------------------------
344 static WXPixmap
wxGetSubPixmap( WXDisplay
* xdisplay
, WXPixmap xpixmap
,
345 int x
, int y
, int width
, int height
,
348 Display
* const dpy
= (Display
*)xdisplay
;
350 int xscreen
= DefaultScreen( dpy
);
351 Window xroot
= RootWindow( dpy
, xscreen
);
352 Visual
* xvisual
= DefaultVisual( dpy
, xscreen
);
354 XImage
* ximage
= XCreateImage( dpy
, xvisual
, depth
,
355 ZPixmap
, 0, 0, width
, height
, 32, 0 );
356 ximage
->data
= (char*)malloc( ximage
->bytes_per_line
* ximage
->height
);
357 ximage
= XGetSubImage( dpy
, (Pixmap
)xpixmap
,
359 AllPlanes
, ZPixmap
, ximage
, 0, 0 );
361 GC gc
= XCreateGC( dpy
, (Pixmap
)xpixmap
, 0, NULL
);
362 Pixmap ret
= XCreatePixmap( dpy
, xroot
,
363 width
, height
, depth
);
365 XPutImage( dpy
, ret
, gc
, ximage
,
366 0, 0, 0, 0, width
, height
);
367 XDestroyImage( ximage
);
370 return (WXPixmap
)ret
;
374 //-----------------------------------------------------------------------------
376 //-----------------------------------------------------------------------------
378 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
380 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
382 bool wxBitmap::Create( int width
, int height
, int depth
)
386 wxCHECK_MSG( (width
> 0) && (height
> 0), false, wxT("invalid bitmap size") );
388 m_refData
= new wxBitmapRefData();
390 return M_BMPDATA
->Create(width
, height
, depth
);
393 bool wxBitmap::Create(const void* data
, wxBitmapType type
,
394 int width
, int height
, int depth
)
398 wxBitmapHandler
*handler
= FindHandler(type
);
400 if ( handler
== NULL
) {
401 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
407 return handler
->Create(this, data
, type
, width
, height
, depth
);
410 bool wxBitmap::Create(WXPixmap pixmap
)
413 Pixmap xpixmap
= (Pixmap
)pixmap
;
414 Display
* xdisplay
= wxGlobalDisplay();
415 int xscreen
= DefaultScreen( xdisplay
);
416 Window xroot
= RootWindow( xdisplay
, xscreen
);
418 // make a copy of the Pixmap
421 unsigned width
, height
, border
, depth
;
423 XGetGeometry( xdisplay
, (Drawable
)xpixmap
, &root
, &x
, &y
,
424 &width
, &height
, &border
, &depth
);
425 Pixmap copy
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
427 GC gc
= XCreateGC( xdisplay
, copy
, 0, NULL
);
428 XCopyArea( xdisplay
, xpixmap
, copy
, gc
, 0, 0, width
, height
, 0, 0 );
429 XFreeGC( xdisplay
, gc
);
432 wxBitmapRefData
* ref
= new wxBitmapRefData();
435 ref
->m_bitmap
= copy
;
437 ref
->m_pixmap
= copy
;
439 ref
->m_display
= xdisplay
;
440 ref
->m_width
= width
;
441 ref
->m_height
= height
;
449 wxBitmap::wxBitmap(const char* const* bits
)
451 Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
454 wxGDIRefData
*wxBitmap::CreateGDIRefData() const
456 return new wxBitmapRefData
;
459 wxGDIRefData
*wxBitmap::CloneGDIRefData(const wxGDIRefData
*data
) const
461 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
464 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
469 wxASSERT_MSG(image
.IsOk(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
473 int w
= image
.GetWidth();
474 int h
= image
.GetHeight();
476 if (!Create(w
, h
, depth
))
479 // Unfortunately the mask has to be screen-depth since
480 // 1-bpp bitmaps don't seem to be supported
481 // TODO: implement transparent drawing, presumably
482 // by doing several blits as per the Windows
483 // implementation because Nano-X doesn't support
485 // TODO: could perhaps speed this function up
486 // by making a buffer of pixel values,
487 // and then calling GrArea to write that to the
488 // pixmap. See demos/nxroach.c.
490 bool hasMask
= image
.HasMask();
492 GC pixmapGC
= GrNewGC();
493 Pixmap pixmap
= (Pixmap
) GetPixmap();
496 Pixmap maskPixmap
= 0;
498 unsigned char maskR
= 0;
499 unsigned char maskG
= 0;
500 unsigned char maskB
= 0;
504 maskR
= image
.GetMaskRed();
505 maskG
= image
.GetMaskGreen();
506 maskB
= image
.GetMaskBlue();
509 maskPixmap
= GrNewPixmap(w
, h
, 0);
514 wxMask
* mask
= new wxMask
;
515 mask
->SetBitmap((WXPixmap
) maskPixmap
);
520 GR_COLOR lastPixmapColour
= 0;
521 GR_COLOR lastMaskColour
= 0;
524 for (i
= 0; i
< w
; i
++)
526 for (j
= 0; j
< h
; j
++)
528 unsigned char red
= image
.GetRed(i
, j
);
529 unsigned char green
= image
.GetGreen(i
, j
);
530 unsigned char blue
= image
.GetBlue(i
, j
);
532 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
534 // Efficiency measure
535 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
537 GrSetGCForeground(pixmapGC
, colour
);
538 lastPixmapColour
= colour
;
541 GrPoint(pixmap
, pixmapGC
, i
, j
);
545 // scan the bitmap for the transparent colour and set the corresponding
546 // pixels in the mask to BLACK and the rest to WHITE
547 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
549 colour
= GR_RGB(0, 0, 0);
553 colour
= GR_RGB(255, 255, 255);
555 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
557 GrSetGCForeground(maskGC
, colour
);
558 lastMaskColour
= colour
;
560 GrPoint(maskPixmap
, maskGC
, i
, j
);
565 GrDestroyGC(pixmapGC
);
575 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
576 wxCHECK_MSG( depth
== -1, false, wxT("invalid bitmap depth") );
578 m_refData
= new wxBitmapRefData();
580 M_BMPDATA
->m_display
= wxGlobalDisplay();
582 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
584 int xscreen
= DefaultScreen( xdisplay
);
585 Window xroot
= RootWindow( xdisplay
, xscreen
);
586 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
588 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
590 int width
= image
.GetWidth();
591 int height
= image
.GetHeight();
592 M_BMPDATA
->m_width
= width
;
593 M_BMPDATA
->m_height
= height
;
595 if (depth
!= 1) depth
= bpp
;
596 M_BMPDATA
->m_bpp
= depth
;
600 wxFAIL_MSG( wxT("mono images later") );
606 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
607 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
609 if (data_image
->data
== NULL
)
611 wxLogError( wxT("Out of memory.") ); // TODO clean
615 M_BMPDATA
->m_pixmap
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
617 // Create mask if necessary
618 const bool hasMask
= image
.HasMask();
620 XImage
*mask_image
= NULL
;
623 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
624 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
626 if (mask_image
->data
== NULL
)
628 wxLogError( wxT("Out of memory.") ); // TODO clean
632 wxMask
*mask
= new wxMask();
633 mask
->SetDisplay( xdisplay
);
634 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
639 if (bpp
< 8) bpp
= 8;
643 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
644 byte_order b_o
= RGB
;
646 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
647 unsigned long greenMask
= vi
->m_visualGreenMask
,
648 redMask
= vi
->m_visualRedMask
,
649 blueMask
= vi
->m_visualBlueMask
;
653 if ((redMask
> greenMask
) && (greenMask
> blueMask
)) b_o
= RGB
;
654 else if ((redMask
> blueMask
) && (blueMask
> greenMask
)) b_o
= RBG
;
655 else if ((blueMask
> redMask
) && (redMask
> greenMask
)) b_o
= BRG
;
656 else if ((blueMask
> greenMask
) && (greenMask
> redMask
))b_o
= BGR
;
657 else if ((greenMask
> redMask
) && (redMask
> blueMask
)) b_o
= GRB
;
658 else if ((greenMask
> blueMask
) && (blueMask
> redMask
)) b_o
= GBR
;
661 int r_mask
= image
.GetMaskRed();
662 int g_mask
= image
.GetMaskGreen();
663 int b_mask
= image
.GetMaskBlue();
665 unsigned char* data
= image
.GetData();
666 wxASSERT_MSG( data
, wxT("No image data") );
668 unsigned char *colorCube
=
669 wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_colorCube
;
672 for (int y
= 0; y
< height
; y
++)
674 for (int x
= 0; x
< width
; x
++)
685 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
686 XPutPixel( mask_image
, x
, y
, 0 );
688 XPutPixel( mask_image
, x
, y
, 1 );
696 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
697 XPutPixel( data_image
, x
, y
, pixel
);
705 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
706 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
707 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
708 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
709 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
710 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
712 XPutPixel( data_image
, x
, y
, pixel
);
720 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
721 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
722 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
723 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
724 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
725 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
727 XPutPixel( data_image
, x
, y
, pixel
);
732 // I actually don't know if for 16-bit displays, it is alway the green
733 // component or the second component which has 6 bits.
737 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
738 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
739 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
740 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
741 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
742 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
744 XPutPixel( data_image
, x
, y
, pixel
);
753 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
754 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
755 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
756 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
757 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
758 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
760 XPutPixel( data_image
, x
, y
, pixel
);
769 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
770 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
771 XDestroyImage( data_image
);
772 XFreeGC( xdisplay
, gc
);
778 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
779 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
781 XDestroyImage( mask_image
);
782 XFreeGC( xdisplay
, gc
);
791 wxImage
wxBitmap::ConvertToImage() const
795 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
797 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
798 wxASSERT_MSG( xdisplay
, wxT("No display") );
801 //int bpp = DefaultDepth(xdisplay, xscreen);
802 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
806 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
807 XImage
*x_image
= NULL
;
810 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
812 GetWidth(), GetHeight(),
813 AllPlanes
, ZPixmap
);
817 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
819 GetWidth(), GetHeight(),
820 AllPlanes
, ZPixmap
);
823 wxFAIL_MSG( wxT("Ill-formed bitmap") );
826 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
828 image
.Create( GetWidth(), GetHeight() );
829 char unsigned *data
= image
.GetData();
833 XDestroyImage( x_image
);
834 wxFAIL_MSG( wxT("couldn't create image") );
838 XImage
*x_image_mask
= NULL
;
841 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
843 GetWidth(), GetHeight(),
844 AllPlanes
, ZPixmap
);
846 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
849 int red_shift_right
= 0;
850 int green_shift_right
= 0;
851 int blue_shift_right
= 0;
852 int red_shift_left
= 0;
853 int green_shift_left
= 0;
854 int blue_shift_left
= 0;
855 bool use_shift
= false;
859 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
861 red_shift_right
= vi
->m_visualRedShift
;
862 red_shift_left
= 8 - vi
->m_visualRedPrec
;
863 green_shift_right
= vi
->m_visualGreenShift
;
864 green_shift_left
= 8 - vi
->m_visualGreenPrec
;
865 blue_shift_right
= vi
->m_visualBlueShift
;
866 blue_shift_left
= 8 - vi
->m_visualBluePrec
;
868 use_shift
= (vi
->m_visualType
== GrayScale
) ||
869 (vi
->m_visualType
!= PseudoColor
);
877 XColor
*colors
= (XColor
*)wxTheApp
->
878 GetVisualInfo(M_BMPDATA
->m_display
)->m_visualColormap
;
880 int width
= GetWidth();
881 int height
= GetHeight();
883 for (int j
= 0; j
< height
; j
++)
885 for (int i
= 0; i
< width
; i
++)
887 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
905 data
[pos
] = (unsigned char)((pixel
>> red_shift_right
) << red_shift_left
);
906 data
[pos
+1] = (unsigned char)((pixel
>> green_shift_right
) << green_shift_left
);
907 data
[pos
+2] = (unsigned char)((pixel
>> blue_shift_right
) << blue_shift_left
);
911 data
[pos
] = (unsigned char)(colors
[pixel
].red
>> 8);
912 data
[pos
+1] = (unsigned char)(colors
[pixel
].green
>> 8);
913 data
[pos
+2] = (unsigned char)(colors
[pixel
].blue
>> 8);
917 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
922 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
935 XDestroyImage( x_image
);
936 if (x_image_mask
) XDestroyImage( x_image_mask
);
942 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
944 LoadFile( filename
, type
);
947 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
949 m_refData
= new wxBitmapRefData
;
951 (void) Create(bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
954 wxBitmap::~wxBitmap()
958 int wxBitmap::GetHeight() const
960 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
962 return M_BMPDATA
->m_height
;
965 int wxBitmap::GetWidth() const
967 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
969 return M_BMPDATA
->m_width
;
972 int wxBitmap::GetDepth() const
974 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
976 return M_BMPDATA
->m_bpp
;
979 wxMask
*wxBitmap::GetMask() const
981 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
983 return M_BMPDATA
->m_mask
;
986 void wxBitmap::SetMask( wxMask
*mask
)
988 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
991 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
993 M_BMPDATA
->m_mask
= mask
;
996 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1002 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1004 wxCHECK_MSG( IsOk() &&
1005 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1006 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
1007 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1008 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1010 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1011 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1015 wxMask
* mask
= new wxMask();
1016 mask
->SetDisplay( GetMask()->GetDisplay() );
1017 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
1018 GetMask()->GetBitmap(),
1020 rect
.width
, rect
.height
,
1023 ret
.SetMask( mask
);
1028 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
1031 rect
.width
, rect
.height
,
1032 M_BMPDATA
->m_bpp
) );
1037 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1040 rect
.width
, rect
.height
,
1047 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1048 const wxPalette
*palette
) const
1050 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
1052 wxBitmapHandler
*handler
= FindHandler(type
);
1054 // Try to save the bitmap via wxImage handlers:
1055 if (handler
== NULL
)
1057 wxImage
image(this->ConvertToImage());
1058 if (image
.IsOk()) return image
.SaveFile( name
, type
);
1063 return handler
->SaveFile(this, name
, type
, palette
);
1066 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1070 if (!wxFileExists(name
)) return false;
1072 wxBitmapHandler
*handler
= FindHandler(type
);
1074 if (handler
== NULL
)
1077 if (!image
.LoadFile( name
, type
))
1082 *this = wxBitmap(image
);
1088 return handler
->LoadFile(this, name
, type
, -1, -1);
1091 void wxBitmap::SetPalette(const wxPalette
& palette
)
1093 wxCHECK_RET(IsOk(), wxT("invalid bitmap"));
1094 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1095 wxT("cannot set palette for bitmap of this depth"));
1098 wxDELETE(M_BMPDATA
->m_palette
);
1100 if (!palette
.IsOk()) return;
1102 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1105 wxPalette
*wxBitmap::GetPalette() const
1107 if (!IsOk()) return NULL
;
1109 return M_BMPDATA
->m_palette
;
1112 void wxBitmap::SetHeight( int height
)
1116 M_BMPDATA
->m_height
= height
;
1119 void wxBitmap::SetWidth( int width
)
1123 M_BMPDATA
->m_width
= width
;
1126 void wxBitmap::SetDepth( int depth
)
1130 M_BMPDATA
->m_bpp
= depth
;
1133 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1135 if (!m_refData
) m_refData
= new wxBitmapRefData();
1137 M_BMPDATA
->m_pixmap
= (Pixmap
)pixmap
;
1140 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1142 if (!m_refData
) m_refData
= new wxBitmapRefData();
1144 M_BMPDATA
->m_bitmap
= (Pixmap
)bitmap
;
1147 WXPixmap
wxBitmap::GetPixmap() const
1149 wxCHECK_MSG( IsOk(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1151 return (WXPixmap
)M_BMPDATA
->m_pixmap
;
1154 WXPixmap
wxBitmap::GetBitmap() const
1156 wxCHECK_MSG( IsOk(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1158 return (WXPixmap
)M_BMPDATA
->m_bitmap
;
1161 WXPixmap
wxBitmap::GetDrawable() const
1163 wxCHECK_MSG( IsOk(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1165 return (WXPixmap
)(M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
1166 : M_BMPDATA
->m_pixmap
);
1169 WXDisplay
*wxBitmap::GetDisplay() const
1171 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1173 return M_BMPDATA
->m_display
;
1177 // Copy from the drawable to the wxImage
1178 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1180 GR_SCREEN_INFO sinfo
;
1182 GR_PIXELVAL
*pixels
;
1183 GR_PALETTE
* palette
= NULL
;
1184 unsigned char rgb
[3], *pp
;
1186 GrGetScreenInfo(&sinfo
);
1188 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1189 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1192 GrGetSystemPalette(palette
);
1195 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1200 image
.Create(width
, height
);
1202 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1206 for(x
= 0; x
< sinfo
.cols
; x
++) {
1208 pp
= (unsigned char *)pixels
+
1209 ((x
+ (y
* sinfo
.cols
)) *
1210 sizeof(GR_PIXELVAL
));
1212 switch(sinfo
.pixtype
) {
1213 /* FIXME: These may need modifying on big endian. */
1214 case MWPF_TRUECOLOR0888
:
1215 case MWPF_TRUECOLOR888
:
1221 rgb
[0] = palette
->palette
[pp
[0]].r
;
1222 rgb
[1] = palette
->palette
[pp
[0]].g
;
1223 rgb
[2] = palette
->palette
[pp
[0]].b
;
1225 case MWPF_TRUECOLOR565
:
1226 rgb
[0] = pp
[1] & 0xf8;
1227 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1228 ((pp
[0] & 0xe0) >> 3);
1229 rgb
[2] = (pp
[0] & 0x1f) << 3;
1231 case MWPF_TRUECOLOR555
:
1232 rgb
[0] = (pp
[1] & 0x7c) << 1;
1233 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1234 ((pp
[0] & 0xe0) >> 2);
1235 rgb
[2] = (pp
[0] & 0x1f) << 3;
1237 case MWPF_TRUECOLOR332
:
1238 rgb
[0] = pp
[0] & 0xe0;
1239 rgb
[1] = (pp
[0] & 0x1c) << 3;
1240 rgb
[2] = (pp
[0] & 0x03) << 6;
1243 fprintf(stderr
, "Unsupported pixel "
1248 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1253 if(palette
) free(palette
);
1259 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1260 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1262 unsigned char rgb
[3], *pp
;
1264 pp
= (unsigned char*) & pixel
;
1266 switch (sinfo
.pixtype
)
1268 /* FIXME: These may need modifying on big endian. */
1269 case MWPF_TRUECOLOR0888
:
1270 case MWPF_TRUECOLOR888
:
1276 rgb
[0] = palette
->palette
[pp
[0]].r
;
1277 rgb
[1] = palette
->palette
[pp
[0]].g
;
1278 rgb
[2] = palette
->palette
[pp
[0]].b
;
1280 case MWPF_TRUECOLOR565
:
1281 rgb
[0] = pp
[1] & 0xf8;
1282 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1283 ((pp
[0] & 0xe0) >> 3);
1284 rgb
[2] = (pp
[0] & 0x1f) << 3;
1286 case MWPF_TRUECOLOR555
:
1287 rgb
[0] = (pp
[1] & 0x7c) << 1;
1288 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1289 ((pp
[0] & 0xe0) >> 2);
1290 rgb
[2] = (pp
[0] & 0x1f) << 3;
1292 case MWPF_TRUECOLOR332
:
1293 rgb
[0] = pp
[0] & 0xe0;
1294 rgb
[1] = (pp
[0] & 0x1c) << 3;
1295 rgb
[2] = (pp
[0] & 0x03) << 6;
1298 fprintf(stderr
, "Unsupported pixel format\n");
1313 // ============================================================================
1315 // ============================================================================
1317 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1321 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1323 // ----------------------------------------------------------------------------
1325 // ----------------------------------------------------------------------------
1327 class wxXPMFileHandler
: public wxBitmapHandler
1332 SetName( wxT("XPM file") );
1333 SetExtension( wxT("xpm") );
1334 SetType( wxBITMAP_TYPE_XPM
);
1337 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1339 int desiredWidth
, int desiredHeight
);
1341 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1343 const wxPalette
*palette
= NULL
) const;
1345 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
),
1346 const void* WXUNUSED(data
),
1347 wxBitmapType
WXUNUSED(flags
),
1348 int WXUNUSED(width
),
1349 int WXUNUSED(height
),
1350 int WXUNUSED(depth
) = 1)
1353 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1356 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1358 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
,
1359 const wxString
& name
,
1360 wxBitmapType
WXUNUSED(flags
),
1361 int WXUNUSED(desiredWidth
),
1362 int WXUNUSED(desiredHeight
))
1365 if (!bitmap
->GetRefData())
1366 bitmap
->SetRefData( new wxBitmapRefData() );
1368 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1370 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1372 int xscreen
= DefaultScreen( xdisplay
);
1373 Window xroot
= RootWindow( xdisplay
, xscreen
);
1375 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1377 XpmAttributes xpmAttr
;
1378 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1383 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1384 (char*) ((const char*) name
.c_str()),
1385 &pixmap
, &mask
, &xpmAttr
);
1387 if (ErrorStatus
== XpmSuccess
)
1389 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1390 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1392 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1394 XpmFreeAttributes(&xpmAttr
);
1396 M_BMPHANDLERDATA
->m_bitmap
= (Pixmap
) pixmap
;
1400 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1401 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1402 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1414 wxXPMDecoder decoder
;
1415 wxFileInputStream
stream(name
);
1418 wxImage
image(decoder
.ReadFile(stream
));
1419 return image
.IsOk() && bitmap
->CreateFromImage(image
);
1423 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1425 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1428 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
,
1429 const wxString
& name
,
1431 const wxPalette
*WXUNUSED(palette
)) const
1433 wxImage
image(bitmap
->ConvertToImage());
1435 return image
.SaveFile( name
, type
);
1440 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1442 // ----------------------------------------------------------------------------
1444 // ----------------------------------------------------------------------------
1446 class wxXPMDataHandler
: public wxBitmapHandler
1448 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1452 SetName( wxT("XPM data") );
1453 SetExtension( wxT("xpm") );
1454 SetType( wxBITMAP_TYPE_XPM_DATA
);
1457 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1458 const wxString
& WXUNUSED(name
),
1459 wxBitmapType
WXUNUSED(flags
),
1460 int WXUNUSED(desiredWidth
),
1461 int WXUNUSED(desiredHeight
))
1464 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1465 const wxString
& WXUNUSED(name
),
1466 wxBitmapType
WXUNUSED(type
),
1467 const wxPalette
*WXUNUSED(palette
) = NULL
) const
1470 virtual bool Create(wxBitmap
*bitmap
, const void* data
, wxBitmapType flags
,
1471 int width
, int height
, int depth
= 1);
1474 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1476 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, const void* bits
,
1477 wxBitmapType
WXUNUSED(flags
),
1478 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
))
1481 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
1483 if (!bitmap
->GetRefData())
1484 bitmap
->SetRefData( new wxBitmapRefData() );
1486 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1488 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1490 int xscreen
= DefaultScreen( xdisplay
);
1491 Window xroot
= RootWindow( xdisplay
, xscreen
);
1493 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1495 XpmAttributes xpmAttr
;
1496 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1501 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1502 &pixmap
, &mask
, &xpmAttr
);
1504 if (ErrorStatus
== XpmSuccess
)
1506 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1507 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1509 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1512 unsigned int depthRet
;
1514 unsigned int widthRet
, heightRet
, borderWidthRet
;
1515 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1516 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1518 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1519 #endif // wxDEBUG_LEVEL
1521 XpmFreeAttributes(&xpmAttr
);
1523 M_BMPHANDLERDATA
->m_pixmap
= (Pixmap
) pixmap
;
1527 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1528 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1529 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1539 #else // !wxHAVE_LIB_XPM
1540 wxXPMDecoder decoder
;
1541 wxImage
image(decoder
.ReadData((const char **)bits
));
1542 return image
.IsOk() && bitmap
->CreateFromImage(image
);
1543 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1548 // ----------------------------------------------------------------------------
1550 // ----------------------------------------------------------------------------
1552 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1554 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1556 inline wxXBMDataHandler()
1558 SetName( wxT("XBM data") );
1559 SetExtension( wxT("xbm") );
1560 SetType( wxBITMAP_TYPE_XBM_DATA
);
1563 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1564 const wxString
& WXUNUSED(name
),
1565 wxBitmapType
WXUNUSED(flags
),
1566 int WXUNUSED(desiredWidth
),
1567 int WXUNUSED(desiredHeight
))
1570 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1571 const wxString
& WXUNUSED(name
),
1572 wxBitmapType
WXUNUSED(type
),
1573 const wxPalette
*WXUNUSED(palette
) = NULL
) const
1576 virtual bool Create(wxBitmap
*bitmap
, const void* data
, wxBitmapType type
,
1577 int width
, int height
, int depth
= 1);
1580 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
1582 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, const void* bits
,
1583 wxBitmapType
WXUNUSED(type
),
1584 int width
, int height
, int WXUNUSED(depth
))
1587 if (!bitmap
->GetRefData())
1588 bitmap
->SetRefData( new wxBitmapRefData() );
1590 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1592 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1594 int xscreen
= DefaultScreen( xdisplay
);
1595 Window xroot
= RootWindow( xdisplay
, xscreen
);
1597 M_BMPHANDLERDATA
->m_mask
= NULL
;
1598 M_BMPHANDLERDATA
->m_bitmap
=
1599 XCreateBitmapFromData(xdisplay
, xroot
,
1600 (char *) bits
, width
, height
);
1601 M_BMPHANDLERDATA
->m_width
= width
;
1602 M_BMPHANDLERDATA
->m_height
= height
;
1603 M_BMPHANDLERDATA
->m_bpp
= 1;
1607 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, false,
1608 wxT("couldn't create bitmap") );
1612 void wxBitmap::InitStandardHandlers()
1614 AddHandler(new wxXBMDataHandler
);
1616 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1617 AddHandler(new wxXPMFileHandler
);
1619 AddHandler(new wxXPMDataHandler
);