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"
25 #include "wx/dcmemory.h"
28 #include "wx/x11/private.h"
30 /* No point in using libXPM for NanoX */
33 #define wxHAVE_LIB_XPM 0
35 // Copy from the drawable to the wxImage
36 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
);
43 #include "wx/xpmdecod.h"
44 #include "wx/wfstream.h"
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
61 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
64 Create( bitmap
, colour
);
67 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
70 Create( bitmap
, paletteIndex
);
73 wxMask::wxMask( const wxBitmap
& bitmap
)
82 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
85 bool wxMask::Create( const wxBitmap
& bitmap
,
86 const wxColour
& colour
)
91 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
95 m_display
= bitmap
.GetDisplay();
97 wxImage image
= bitmap
.ConvertToImage();
98 if (!image
.Ok()) return false;
100 m_display
= bitmap
.GetDisplay();
102 Display
*xdisplay
= (Display
*) m_display
;
103 int xscreen
= DefaultScreen( xdisplay
);
104 Window xroot
= RootWindow( xdisplay
, xscreen
);
106 m_bitmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, image
.GetWidth(), image
.GetHeight(), 1 );
107 GC gc
= XCreateGC( xdisplay
, (Pixmap
) m_bitmap
, 0, NULL
);
109 XSetForeground( xdisplay
, gc
, WhitePixel(xdisplay
,xscreen
) );
110 XSetFillStyle( xdisplay
, gc
, FillSolid
);
111 XFillRectangle( xdisplay
, (Pixmap
) m_bitmap
, gc
, 0, 0, image
.GetWidth(), image
.GetHeight() );
113 unsigned char *data
= image
.GetData();
116 unsigned char red
= colour
.Red();
117 unsigned char green
= colour
.Green();
118 unsigned char blue
= colour
.Blue();
120 int bpp
= wxTheApp
->GetVisualInfo(m_display
)->m_visualDepth
;
141 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
143 int width
= image
.GetWidth();
144 int height
= image
.GetHeight();
145 for (int j
= 0; j
< height
; j
++)
149 for (i
= 0; i
< width
; i
++)
151 if ((data
[index
] == red
) &&
152 (data
[index
+1] == green
) &&
153 (data
[index
+2] == blue
))
162 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
169 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
172 XFreeGC( xdisplay
, gc
);
181 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
184 wxPalette
*pal
= bitmap
.GetPalette();
186 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
188 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
190 return Create(bitmap
, wxColour(r
, g
, b
));
193 bool wxMask::Create( const wxBitmap
& bitmap
)
198 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
202 if (!bitmap
.Ok()) return false;
204 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
206 m_display
= bitmap
.GetDisplay();
208 int xscreen
= DefaultScreen( (Display
*) m_display
);
209 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
211 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
213 if (!m_bitmap
) return false;
215 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
217 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
218 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
220 XFreeGC( (Display
*) m_display
, gc
);
229 //-----------------------------------------------------------------------------
231 //-----------------------------------------------------------------------------
233 class wxBitmapRefData
: public wxObjectRefData
241 WXDisplay
*m_display
;
246 wxPalette
*m_palette
;
249 wxBitmapRefData::wxBitmapRefData()
254 m_mask
= (wxMask
*) NULL
;
258 m_palette
= (wxPalette
*) NULL
;
261 wxBitmapRefData::~wxBitmapRefData()
263 if (m_pixmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_pixmap
);
264 if (m_bitmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
265 if (m_mask
) delete m_mask
;
266 if (m_palette
) delete m_palette
;
269 //-----------------------------------------------------------------------------
273 static WXPixmap
wxGetSubPixmap( WXDisplay
* xdisplay
, WXPixmap xpixmap
,
274 int x
, int y
, int width
, int height
,
277 Display
* const dpy
= (Display
*)xdisplay
;
279 int xscreen
= DefaultScreen( dpy
);
280 Window xroot
= RootWindow( dpy
, xscreen
);
281 Visual
* xvisual
= DefaultVisual( dpy
, xscreen
);
283 XImage
* ximage
= XCreateImage( dpy
, xvisual
, depth
,
284 ZPixmap
, 0, 0, width
, height
, 32, 0 );
285 ximage
->data
= (char*)malloc( ximage
->bytes_per_line
* ximage
->height
);
286 ximage
= XGetSubImage( dpy
, (Pixmap
)xpixmap
,
288 AllPlanes
, ZPixmap
, ximage
, 0, 0 );
290 GC gc
= XCreateGC( dpy
, (Pixmap
)xpixmap
, 0, NULL
);
291 Pixmap ret
= XCreatePixmap( dpy
, xroot
,
292 width
, height
, depth
);
294 XPutImage( dpy
, ret
, gc
, ximage
,
295 0, 0, 0, 0, width
, height
);
296 XDestroyImage( ximage
);
299 return (WXPixmap
)ret
;
302 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
304 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
310 wxBitmap::wxBitmap( int width
, int height
, int depth
)
312 Create( width
, height
, depth
);
315 bool wxBitmap::Create( int width
, int height
, int depth
)
319 wxCHECK_MSG( (width
> 0) && (height
> 0), false, wxT("invalid bitmap size") );
321 m_refData
= new wxBitmapRefData();
323 M_BMPDATA
->m_display
= wxGlobalDisplay();
325 wxASSERT_MSG( M_BMPDATA
->m_display
, wxT("No display") );
327 int xscreen
= DefaultScreen( (Display
*) M_BMPDATA
->m_display
);
328 Window xroot
= RootWindow( (Display
*) M_BMPDATA
->m_display
, xscreen
);
330 int bpp
= DefaultDepth( (Display
*) M_BMPDATA
->m_display
, xscreen
);
331 if (depth
== -1) depth
= bpp
;
333 wxCHECK_MSG( (depth
== bpp
) ||
334 (depth
== 1), false, wxT("invalid bitmap depth") );
336 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
337 M_BMPDATA
->m_width
= width
;
338 M_BMPDATA
->m_height
= height
;
341 M_BMPDATA
->m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
342 M_BMPDATA
->m_bpp
= bpp
;
344 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Bitmap creation failed") );
348 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, 1 );
350 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
352 M_BMPDATA
->m_bpp
= 1;
356 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, depth
);
358 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Pixmap creation failed") );
360 M_BMPDATA
->m_bpp
= depth
;
366 bool wxBitmap::Create(void *data
, wxBitmapType type
,
367 int width
, int height
, int depth
)
371 wxBitmapHandler
*handler
= FindHandler(type
);
373 if ( handler
== NULL
) {
374 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
380 return handler
->Create(this, data
, type
, width
, height
, depth
);
383 bool wxBitmap::Create(WXPixmap pixmap
)
386 Pixmap xpixmap
= (Pixmap
)pixmap
;
387 Display
* xdisplay
= wxGlobalDisplay();
388 int xscreen
= DefaultScreen( xdisplay
);
389 Window xroot
= RootWindow( xdisplay
, xscreen
);
391 // make a copy of the Pixmap
395 unsigned width
, height
, border
, depth
;
397 XGetGeometry( xdisplay
, (Drawable
)xpixmap
, &root
, &x
, &y
,
398 &width
, &height
, &border
, &depth
);
399 copy
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
401 GC gc
= XCreateGC( xdisplay
, copy
, 0, NULL
);
402 XCopyArea( xdisplay
, xpixmap
, copy
, gc
, 0, 0, width
, height
, 0, 0 );
403 XFreeGC( xdisplay
, gc
);
406 wxBitmapRefData
* ref
= new wxBitmapRefData();
409 ref
->m_bitmap
= (WXPixmap
)copy
;
411 ref
->m_pixmap
= (WXPixmap
)copy
;
413 ref
->m_display
= (WXDisplay
*)xdisplay
;
414 ref
->m_width
= width
;
415 ref
->m_height
= height
;
423 bool wxBitmap::CreateFromXpm( const char **bits
)
425 wxCHECK_MSG( bits
, false, wxT("NULL pointer in wxBitmap::CreateFromXpm") );
427 return Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
430 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
435 wxASSERT_MSG(image
.Ok(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
439 int w
= image
.GetWidth();
440 int h
= image
.GetHeight();
442 if (!Create(w
, h
, depth
))
445 // Unfortunately the mask has to be screen-depth since
446 // 1-bpp bitmaps don't seem to be supported
447 // TODO: implement transparent drawing, presumably
448 // by doing several blits as per the Windows
449 // implementation because Nano-X doesn't support
451 // TODO: could perhaps speed this function up
452 // by making a buffer of pixel values,
453 // and then calling GrArea to write that to the
454 // pixmap. See demos/nxroach.c.
456 bool hasMask
= image
.HasMask();
458 GC pixmapGC
= GrNewGC();
459 Pixmap pixmap
= (Pixmap
) GetPixmap();
462 Pixmap maskPixmap
= 0;
464 unsigned char maskR
= 0;
465 unsigned char maskG
= 0;
466 unsigned char maskB
= 0;
470 maskR
= image
.GetMaskRed();
471 maskG
= image
.GetMaskGreen();
472 maskB
= image
.GetMaskBlue();
475 maskPixmap
= GrNewPixmap(w
, h
, 0);
480 wxMask
* mask
= new wxMask
;
481 mask
->SetBitmap((WXPixmap
) maskPixmap
);
486 GR_COLOR lastPixmapColour
= 0;
487 GR_COLOR lastMaskColour
= 0;
490 for (i
= 0; i
< w
; i
++)
492 for (j
= 0; j
< h
; j
++)
494 unsigned char red
= image
.GetRed(i
, j
);
495 unsigned char green
= image
.GetGreen(i
, j
);
496 unsigned char blue
= image
.GetBlue(i
, j
);
498 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
500 // Efficiency measure
501 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
503 GrSetGCForeground(pixmapGC
, colour
);
504 lastPixmapColour
= colour
;
507 GrPoint(pixmap
, pixmapGC
, i
, j
);
511 // scan the bitmap for the transparent colour and set the corresponding
512 // pixels in the mask to BLACK and the rest to WHITE
513 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
515 colour
= GR_RGB(0, 0, 0);
519 colour
= GR_RGB(255, 255, 255);
521 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
523 GrSetGCForeground(maskGC
, colour
);
524 lastMaskColour
= colour
;
526 GrPoint(maskPixmap
, maskGC
, i
, j
);
531 GrDestroyGC(pixmapGC
);
541 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
542 wxCHECK_MSG( depth
== -1, false, wxT("invalid bitmap depth") );
544 m_refData
= new wxBitmapRefData();
546 M_BMPDATA
->m_display
= wxGlobalDisplay();
548 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
550 int xscreen
= DefaultScreen( xdisplay
);
551 Window xroot
= RootWindow( xdisplay
, xscreen
);
552 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
554 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
556 int width
= image
.GetWidth();
557 int height
= image
.GetHeight();
558 M_BMPDATA
->m_width
= width
;
559 M_BMPDATA
->m_height
= height
;
561 if (depth
!= 1) depth
= bpp
;
562 M_BMPDATA
->m_bpp
= depth
;
566 wxFAIL_MSG( wxT("mono images later") );
572 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
573 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
575 if (data_image
->data
== NULL
)
577 wxLogError( wxT("Out of memory.") ); // TODO clean
581 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
585 XImage
*mask_image
= (XImage
*) NULL
;
588 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
589 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
591 if (mask_image
->data
== NULL
)
593 wxLogError( wxT("Out of memory.") ); // TODO clean
597 wxMask
*mask
= new wxMask();
598 mask
->SetDisplay( xdisplay
);
599 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
604 if (bpp
< 8) bpp
= 8;
608 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
609 byte_order b_o
= RGB
;
611 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
612 unsigned long greenMask
= vi
->m_visualGreenMask
,
613 redMask
= vi
->m_visualRedMask
,
614 blueMask
= vi
->m_visualBlueMask
;
618 if ((redMask
> greenMask
) && (greenMask
> blueMask
)) b_o
= RGB
;
619 else if ((redMask
> blueMask
) && (blueMask
> greenMask
)) b_o
= RBG
;
620 else if ((blueMask
> redMask
) && (redMask
> greenMask
)) b_o
= BRG
;
621 else if ((blueMask
> greenMask
) && (greenMask
> redMask
))b_o
= BGR
;
622 else if ((greenMask
> redMask
) && (redMask
> blueMask
)) b_o
= GRB
;
623 else if ((greenMask
> blueMask
) && (blueMask
> redMask
)) b_o
= GBR
;
626 int r_mask
= image
.GetMaskRed();
627 int g_mask
= image
.GetMaskGreen();
628 int b_mask
= image
.GetMaskBlue();
630 unsigned char* data
= image
.GetData();
631 wxASSERT_MSG( data
, wxT("No image data") );
633 unsigned char *colorCube
=
634 wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_colorCube
;
636 bool hasMask
= image
.HasMask();
639 for (int y
= 0; y
< height
; y
++)
641 for (int x
= 0; x
< width
; x
++)
652 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
653 XPutPixel( mask_image
, x
, y
, 0 );
655 XPutPixel( mask_image
, x
, y
, 1 );
663 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
664 XPutPixel( data_image
, x
, y
, pixel
);
672 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
673 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
674 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
675 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
676 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
677 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
679 XPutPixel( data_image
, x
, y
, pixel
);
687 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
688 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
689 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
690 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
691 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
692 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
694 XPutPixel( data_image
, x
, y
, pixel
);
699 // I actually don't know if for 16-bit displays, it is alway the green
700 // component or the second component which has 6 bits.
704 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
705 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
706 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
707 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
708 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
709 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
711 XPutPixel( data_image
, x
, y
, pixel
);
720 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
721 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
722 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
723 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
724 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
725 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
727 XPutPixel( data_image
, x
, y
, pixel
);
736 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
737 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
738 XDestroyImage( data_image
);
739 XFreeGC( xdisplay
, gc
);
745 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
746 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
748 XDestroyImage( mask_image
);
749 XFreeGC( xdisplay
, gc
);
758 wxImage
wxBitmap::ConvertToImage() const
762 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
764 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
765 wxASSERT_MSG( xdisplay
, wxT("No display") );
768 //int bpp = DefaultDepth(xdisplay, xscreen);
769 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
773 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
774 XImage
*x_image
= NULL
;
777 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
779 GetWidth(), GetHeight(),
780 AllPlanes
, ZPixmap
);
784 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
786 GetWidth(), GetHeight(),
787 AllPlanes
, ZPixmap
);
790 wxFAIL_MSG( wxT("Ill-formed bitmap") );
793 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
795 image
.Create( GetWidth(), GetHeight() );
796 char unsigned *data
= image
.GetData();
800 XDestroyImage( x_image
);
801 wxFAIL_MSG( wxT("couldn't create image") );
805 XImage
*x_image_mask
= NULL
;
808 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
810 GetWidth(), GetHeight(),
811 AllPlanes
, ZPixmap
);
813 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
816 int red_shift_right
= 0;
817 int green_shift_right
= 0;
818 int blue_shift_right
= 0;
819 int red_shift_left
= 0;
820 int green_shift_left
= 0;
821 int blue_shift_left
= 0;
822 bool use_shift
= false;
826 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
828 red_shift_right
= vi
->m_visualRedShift
;
829 red_shift_left
= 8 - vi
->m_visualRedPrec
;
830 green_shift_right
= vi
->m_visualGreenShift
;
831 green_shift_left
= 8 - vi
->m_visualGreenPrec
;
832 blue_shift_right
= vi
->m_visualBlueShift
;
833 blue_shift_left
= 8 - vi
->m_visualBluePrec
;
835 use_shift
= (vi
->m_visualType
== GrayScale
) ||
836 (vi
->m_visualType
!= PseudoColor
);
844 XColor
*colors
= (XColor
*)wxTheApp
->
845 GetVisualInfo(M_BMPDATA
->m_display
)->m_visualColormap
;
847 int width
= GetWidth();
848 int height
= GetHeight();
850 for (int j
= 0; j
< height
; j
++)
852 for (int i
= 0; i
< width
; i
++)
854 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
872 data
[pos
] = (unsigned char)((pixel
>> red_shift_right
) << red_shift_left
);
873 data
[pos
+1] = (unsigned char)((pixel
>> green_shift_right
) << green_shift_left
);
874 data
[pos
+2] = (unsigned char)((pixel
>> blue_shift_right
) << blue_shift_left
);
878 data
[pos
] = (unsigned char)(colors
[pixel
].red
>> 8);
879 data
[pos
+1] = (unsigned char)(colors
[pixel
].green
>> 8);
880 data
[pos
+2] = (unsigned char)(colors
[pixel
].blue
>> 8);
884 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
889 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
902 XDestroyImage( x_image
);
903 if (x_image_mask
) XDestroyImage( x_image_mask
);
909 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
911 LoadFile( filename
, type
);
914 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
916 m_refData
= new wxBitmapRefData
;
918 (void) Create((void*) bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
921 wxBitmap::~wxBitmap()
925 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
927 return m_refData
== bmp
.m_refData
;
930 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
932 return m_refData
!= bmp
.m_refData
;
935 bool wxBitmap::Ok() const
937 return (m_refData
!= NULL
);
940 int wxBitmap::GetHeight() const
942 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
944 return M_BMPDATA
->m_height
;
947 int wxBitmap::GetWidth() const
949 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
951 return M_BMPDATA
->m_width
;
954 int wxBitmap::GetDepth() const
956 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
958 return M_BMPDATA
->m_bpp
;
961 wxMask
*wxBitmap::GetMask() const
963 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
965 return M_BMPDATA
->m_mask
;
968 void wxBitmap::SetMask( wxMask
*mask
)
970 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
972 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
974 M_BMPDATA
->m_mask
= mask
;
977 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
983 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
986 (rect
.x
>= 0) && (rect
.y
>= 0) &&
987 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
988 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
989 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
991 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
992 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
996 wxMask
* mask
= new wxMask();
997 mask
->SetDisplay( GetMask()->GetDisplay() );
998 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
999 GetMask()->GetBitmap(),
1001 rect
.width
, rect
.height
,
1004 ret
.SetMask( mask
);
1009 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
1012 rect
.width
, rect
.height
,
1013 M_BMPDATA
->m_bpp
) );
1018 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1021 rect
.width
, rect
.height
,
1028 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1029 const wxPalette
*palette
) const
1031 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1033 wxBitmapHandler
*handler
= FindHandler(type
);
1035 // Try to save the bitmap via wxImage handlers:
1036 if (handler
== NULL
)
1038 wxImage
image(this->ConvertToImage());
1039 if (image
.Ok()) return image
.SaveFile( name
, type
);
1044 return handler
->SaveFile(this, name
, type
, palette
);
1047 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1051 if (!wxFileExists(name
)) return false;
1053 wxBitmapHandler
*handler
= FindHandler(type
);
1055 if (handler
== NULL
)
1058 if (!image
.LoadFile( name
, type
))
1063 *this = wxBitmap(image
);
1069 return handler
->LoadFile(this, name
, type
, -1, -1);
1072 void wxBitmap::SetPalette(const wxPalette
& palette
)
1074 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1075 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1076 wxT("cannot set palette for bitmap of this depth"));
1078 delete M_BMPDATA
->m_palette
;
1079 M_BMPDATA
->m_palette
= NULL
;
1081 if (!palette
.Ok()) return;
1083 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1086 wxPalette
*wxBitmap::GetPalette() const
1088 if (!Ok()) return (wxPalette
*) NULL
;
1090 return M_BMPDATA
->m_palette
;
1093 void wxBitmap::SetHeight( int height
)
1095 if (!m_refData
) m_refData
= new wxBitmapRefData();
1097 M_BMPDATA
->m_height
= height
;
1100 void wxBitmap::SetWidth( int width
)
1102 if (!m_refData
) m_refData
= new wxBitmapRefData();
1104 M_BMPDATA
->m_width
= width
;
1107 void wxBitmap::SetDepth( int depth
)
1109 if (!m_refData
) m_refData
= new wxBitmapRefData();
1111 M_BMPDATA
->m_bpp
= depth
;
1114 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1116 if (!m_refData
) m_refData
= new wxBitmapRefData();
1118 M_BMPDATA
->m_pixmap
= pixmap
;
1121 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1123 if (!m_refData
) m_refData
= new wxBitmapRefData();
1125 M_BMPDATA
->m_bitmap
= bitmap
;
1128 WXPixmap
wxBitmap::GetPixmap() const
1130 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1132 return M_BMPDATA
->m_pixmap
;
1135 WXPixmap
wxBitmap::GetBitmap() const
1137 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1139 return M_BMPDATA
->m_bitmap
;
1142 WXPixmap
wxBitmap::GetDrawable() const
1144 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1146 return M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
: M_BMPDATA
->m_pixmap
;
1149 WXDisplay
*wxBitmap::GetDisplay() const
1151 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1153 return M_BMPDATA
->m_display
;
1157 // Copy from the drawable to the wxImage
1158 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1160 GR_SCREEN_INFO sinfo
;
1162 GR_PIXELVAL
*pixels
;
1163 GR_PALETTE
* palette
= NULL
;
1164 unsigned char rgb
[3], *pp
;
1166 GrGetScreenInfo(&sinfo
);
1168 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1169 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1172 GrGetSystemPalette(palette
);
1175 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1180 image
.Create(width
, height
);
1182 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1186 for(x
= 0; x
< sinfo
.cols
; x
++) {
1188 pp
= (unsigned char *)pixels
+
1189 ((x
+ (y
* sinfo
.cols
)) *
1190 sizeof(GR_PIXELVAL
));
1192 switch(sinfo
.pixtype
) {
1193 /* FIXME: These may need modifying on big endian. */
1194 case MWPF_TRUECOLOR0888
:
1195 case MWPF_TRUECOLOR888
:
1201 rgb
[0] = palette
->palette
[pp
[0]].r
;
1202 rgb
[1] = palette
->palette
[pp
[0]].g
;
1203 rgb
[2] = palette
->palette
[pp
[0]].b
;
1205 case MWPF_TRUECOLOR565
:
1206 rgb
[0] = pp
[1] & 0xf8;
1207 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1208 ((pp
[0] & 0xe0) >> 3);
1209 rgb
[2] = (pp
[0] & 0x1f) << 3;
1211 case MWPF_TRUECOLOR555
:
1212 rgb
[0] = (pp
[1] & 0x7c) << 1;
1213 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1214 ((pp
[0] & 0xe0) >> 2);
1215 rgb
[2] = (pp
[0] & 0x1f) << 3;
1217 case MWPF_TRUECOLOR332
:
1218 rgb
[0] = pp
[0] & 0xe0;
1219 rgb
[1] = (pp
[0] & 0x1c) << 3;
1220 rgb
[2] = (pp
[0] & 0x03) << 6;
1223 fprintf(stderr
, "Unsupported pixel "
1228 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1233 if(palette
) free(palette
);
1239 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1240 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1242 unsigned char rgb
[3], *pp
;
1244 pp
= (unsigned char*) & pixel
;
1246 switch (sinfo
.pixtype
)
1248 /* FIXME: These may need modifying on big endian. */
1249 case MWPF_TRUECOLOR0888
:
1250 case MWPF_TRUECOLOR888
:
1256 rgb
[0] = palette
->palette
[pp
[0]].r
;
1257 rgb
[1] = palette
->palette
[pp
[0]].g
;
1258 rgb
[2] = palette
->palette
[pp
[0]].b
;
1260 case MWPF_TRUECOLOR565
:
1261 rgb
[0] = pp
[1] & 0xf8;
1262 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1263 ((pp
[0] & 0xe0) >> 3);
1264 rgb
[2] = (pp
[0] & 0x1f) << 3;
1266 case MWPF_TRUECOLOR555
:
1267 rgb
[0] = (pp
[1] & 0x7c) << 1;
1268 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1269 ((pp
[0] & 0xe0) >> 2);
1270 rgb
[2] = (pp
[0] & 0x1f) << 3;
1272 case MWPF_TRUECOLOR332
:
1273 rgb
[0] = pp
[0] & 0xe0;
1274 rgb
[1] = (pp
[0] & 0x1c) << 3;
1275 rgb
[2] = (pp
[0] & 0x03) << 6;
1278 fprintf(stderr
, "Unsupported pixel format\n");
1293 // ============================================================================
1295 // ============================================================================
1297 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1299 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1303 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1305 // ----------------------------------------------------------------------------
1307 // ----------------------------------------------------------------------------
1309 class wxXPMFileHandler
: public wxBitmapHandler
1311 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1315 SetName( wxT("XPM file") );
1316 SetExtension( wxT("xpm") );
1317 SetType( wxBITMAP_TYPE_XPM
);
1320 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1321 int desiredWidth
, int desiredHeight
);
1323 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1324 int type
, const wxPalette
*palette
= NULL
);
1326 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
), void *WXUNUSED(data
), long WXUNUSED(flags
),
1327 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
) = 1)
1331 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1333 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1334 long WXUNUSED(flags
), int WXUNUSED(desiredWidth
),
1335 int WXUNUSED(desiredHeight
))
1338 if (!bitmap
->GetRefData())
1339 bitmap
->SetRefData( new wxBitmapRefData() );
1341 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1343 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1345 int xscreen
= DefaultScreen( xdisplay
);
1346 Window xroot
= RootWindow( xdisplay
, xscreen
);
1348 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1350 XpmAttributes xpmAttr
;
1351 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1356 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1357 (char*) name
.c_str(),
1358 &pixmap
, &mask
, &xpmAttr
);
1360 if (ErrorStatus
== XpmSuccess
)
1362 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1363 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1365 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1367 XpmFreeAttributes(&xpmAttr
);
1369 M_BMPHANDLERDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1373 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1374 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1375 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1387 wxXPMDecoder decoder
;
1388 wxFileInputStream
stream(name
);
1391 wxImage
image(decoder
.ReadFile(stream
));
1392 return image
.Ok() && bitmap
->CreateFromImage(image
);
1396 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1398 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1401 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1403 const wxPalette
*WXUNUSED(palette
))
1405 wxImage
image(bitmap
->ConvertToImage());
1406 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1411 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1413 // ----------------------------------------------------------------------------
1415 // ----------------------------------------------------------------------------
1417 class wxXPMDataHandler
: public wxBitmapHandler
1419 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1423 SetName( wxT("XPM data") );
1424 SetExtension( wxT("xpm") );
1425 SetType( wxBITMAP_TYPE_XPM_DATA
);
1428 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1429 const wxString
& WXUNUSED(name
),
1430 long WXUNUSED(flags
),
1431 int WXUNUSED(desiredWidth
),
1432 int WXUNUSED(desiredHeight
))
1435 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1436 const wxString
& WXUNUSED(name
),
1438 const wxPalette
*WXUNUSED(palette
) = NULL
)
1441 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1442 int width
, int height
, int depth
= 1);
1445 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1447 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *bits
,
1448 long WXUNUSED(flags
),
1449 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
))
1452 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
1454 if (!bitmap
->GetRefData())
1455 bitmap
->SetRefData( new wxBitmapRefData() );
1457 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1459 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1461 int xscreen
= DefaultScreen( xdisplay
);
1462 Window xroot
= RootWindow( xdisplay
, xscreen
);
1464 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1466 XpmAttributes xpmAttr
;
1467 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1472 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1473 &pixmap
, &mask
, &xpmAttr
);
1475 if (ErrorStatus
== XpmSuccess
)
1477 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1478 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1480 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1483 unsigned int depthRet
;
1485 unsigned int widthRet
, heightRet
, borderWidthRet
;
1486 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1487 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1489 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1492 XpmFreeAttributes(&xpmAttr
);
1494 M_BMPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
1498 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1499 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1500 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1510 #else // !wxHAVE_LIB_XPM
1511 wxXPMDecoder decoder
;
1512 wxImage
image(decoder
.ReadData((const char **)bits
));
1513 return image
.Ok() && bitmap
->CreateFromImage(image
);
1514 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1519 // ----------------------------------------------------------------------------
1521 // ----------------------------------------------------------------------------
1523 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1525 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1527 inline wxXBMDataHandler()
1529 SetName( wxT("XBM data") );
1530 SetExtension( wxT("xbm") );
1531 SetType( wxBITMAP_TYPE_XBM_DATA
);
1534 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1535 const wxString
& WXUNUSED(name
),
1536 long WXUNUSED(flags
),
1537 int WXUNUSED(desiredWidth
),
1538 int WXUNUSED(desiredHeight
))
1541 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1542 const wxString
& WXUNUSED(name
),
1544 const wxPalette
*WXUNUSED(palette
) = NULL
)
1547 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1548 int width
, int height
, int depth
= 1);
1551 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
1553 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, void *bits
,
1554 long WXUNUSED(flags
),
1555 int width
, int height
, int WXUNUSED(depth
))
1558 if (!bitmap
->GetRefData())
1559 bitmap
->SetRefData( new wxBitmapRefData() );
1561 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1563 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1565 int xscreen
= DefaultScreen( xdisplay
);
1566 Window xroot
= RootWindow( xdisplay
, xscreen
);
1568 M_BMPHANDLERDATA
->m_mask
= (wxMask
*) NULL
;
1569 M_BMPHANDLERDATA
->m_bitmap
=
1570 (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
,
1571 (char *) bits
, width
, height
);
1572 M_BMPHANDLERDATA
->m_width
= width
;
1573 M_BMPHANDLERDATA
->m_height
= height
;
1574 M_BMPHANDLERDATA
->m_bpp
= 1;
1578 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, false,
1579 wxT("couldn't create bitmap") );
1583 void wxBitmap::InitStandardHandlers()
1585 AddHandler(new wxXBMDataHandler
);
1587 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1588 AddHandler(new wxXPMFileHandler
);
1590 AddHandler(new wxXPMDataHandler
);