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"
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
59 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
62 Create( bitmap
, colour
);
65 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
68 Create( bitmap
, paletteIndex
);
71 wxMask::wxMask( const wxBitmap
& bitmap
)
80 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
83 bool wxMask::Create( const wxBitmap
& bitmap
,
84 const wxColour
& colour
)
89 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
93 m_display
= bitmap
.GetDisplay();
95 wxImage image
= bitmap
.ConvertToImage();
96 if (!image
.Ok()) return false;
98 m_display
= bitmap
.GetDisplay();
100 Display
*xdisplay
= (Display
*) m_display
;
101 int xscreen
= DefaultScreen( xdisplay
);
102 Window xroot
= RootWindow( xdisplay
, xscreen
);
104 m_bitmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, image
.GetWidth(), image
.GetHeight(), 1 );
105 GC gc
= XCreateGC( xdisplay
, (Pixmap
) m_bitmap
, 0, NULL
);
107 XSetForeground( xdisplay
, gc
, WhitePixel(xdisplay
,xscreen
) );
108 XSetFillStyle( xdisplay
, gc
, FillSolid
);
109 XFillRectangle( xdisplay
, (Pixmap
) m_bitmap
, gc
, 0, 0, image
.GetWidth(), image
.GetHeight() );
111 unsigned char *data
= image
.GetData();
114 unsigned char red
= colour
.Red();
115 unsigned char green
= colour
.Green();
116 unsigned char blue
= colour
.Blue();
118 int bpp
= wxTheApp
->GetVisualInfo(m_display
)->m_visualDepth
;
139 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
141 int width
= image
.GetWidth();
142 int height
= image
.GetHeight();
143 for (int j
= 0; j
< height
; j
++)
147 for (i
= 0; i
< width
; i
++)
149 if ((data
[index
] == red
) &&
150 (data
[index
+1] == green
) &&
151 (data
[index
+2] == blue
))
160 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
167 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
170 XFreeGC( xdisplay
, gc
);
179 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
182 wxPalette
*pal
= bitmap
.GetPalette();
184 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
186 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
188 return Create(bitmap
, wxColour(r
, g
, b
));
191 bool wxMask::Create( const wxBitmap
& bitmap
)
196 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
200 if (!bitmap
.Ok()) return false;
202 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
204 m_display
= bitmap
.GetDisplay();
206 int xscreen
= DefaultScreen( (Display
*) m_display
);
207 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
209 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
211 if (!m_bitmap
) return false;
213 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
215 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
216 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
218 XFreeGC( (Display
*) m_display
, gc
);
227 //-----------------------------------------------------------------------------
229 //-----------------------------------------------------------------------------
231 class wxBitmapRefData
: public wxObjectRefData
239 WXDisplay
*m_display
;
244 wxPalette
*m_palette
;
247 wxBitmapRefData::wxBitmapRefData()
252 m_mask
= (wxMask
*) NULL
;
256 m_palette
= (wxPalette
*) NULL
;
259 wxBitmapRefData::~wxBitmapRefData()
261 if (m_pixmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_pixmap
);
262 if (m_bitmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
263 if (m_mask
) delete m_mask
;
264 if (m_palette
) delete m_palette
;
267 //-----------------------------------------------------------------------------
271 static WXPixmap
wxGetSubPixmap( WXDisplay
* xdisplay
, WXPixmap xpixmap
,
272 int x
, int y
, int width
, int height
,
275 Display
* const dpy
= (Display
*)xdisplay
;
277 int xscreen
= DefaultScreen( dpy
);
278 Window xroot
= RootWindow( dpy
, xscreen
);
279 Visual
* xvisual
= DefaultVisual( dpy
, xscreen
);
281 XImage
* ximage
= XCreateImage( dpy
, xvisual
, depth
,
282 ZPixmap
, 0, 0, width
, height
, 32, 0 );
283 ximage
->data
= (char*)malloc( ximage
->bytes_per_line
* ximage
->height
);
284 ximage
= XGetSubImage( dpy
, (Pixmap
)xpixmap
,
286 AllPlanes
, ZPixmap
, ximage
, 0, 0 );
288 GC gc
= XCreateGC( dpy
, (Pixmap
)xpixmap
, 0, NULL
);
289 Pixmap ret
= XCreatePixmap( dpy
, xroot
,
290 width
, height
, depth
);
292 XPutImage( dpy
, ret
, gc
, ximage
,
293 0, 0, 0, 0, width
, height
);
294 XDestroyImage( ximage
);
297 return (WXPixmap
)ret
;
300 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
302 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
308 wxBitmap::wxBitmap( int width
, int height
, int depth
)
310 Create( width
, height
, depth
);
313 bool wxBitmap::Create( int width
, int height
, int depth
)
317 wxCHECK_MSG( (width
> 0) && (height
> 0), false, wxT("invalid bitmap size") );
319 m_refData
= new wxBitmapRefData();
321 M_BMPDATA
->m_display
= wxGlobalDisplay();
323 wxASSERT_MSG( M_BMPDATA
->m_display
, wxT("No display") );
325 int xscreen
= DefaultScreen( (Display
*) M_BMPDATA
->m_display
);
326 Window xroot
= RootWindow( (Display
*) M_BMPDATA
->m_display
, xscreen
);
328 int bpp
= DefaultDepth( (Display
*) M_BMPDATA
->m_display
, xscreen
);
329 if (depth
== -1) depth
= bpp
;
331 wxCHECK_MSG( (depth
== bpp
) ||
332 (depth
== 1), false, wxT("invalid bitmap depth") );
334 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
335 M_BMPDATA
->m_width
= width
;
336 M_BMPDATA
->m_height
= height
;
339 M_BMPDATA
->m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
340 M_BMPDATA
->m_bpp
= bpp
;
342 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Bitmap creation failed") );
346 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, 1 );
348 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
350 M_BMPDATA
->m_bpp
= 1;
354 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, depth
);
356 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Pixmap creation failed") );
358 M_BMPDATA
->m_bpp
= depth
;
364 bool wxBitmap::Create(void *data
, wxBitmapType type
,
365 int width
, int height
, int depth
)
369 wxBitmapHandler
*handler
= FindHandler(type
);
371 if ( handler
== NULL
) {
372 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
378 return handler
->Create(this, data
, type
, width
, height
, depth
);
381 bool wxBitmap::Create(WXPixmap pixmap
)
384 Pixmap xpixmap
= (Pixmap
)pixmap
;
385 Display
* xdisplay
= wxGlobalDisplay();
386 int xscreen
= DefaultScreen( xdisplay
);
387 Window xroot
= RootWindow( xdisplay
, xscreen
);
389 // make a copy of the Pixmap
393 unsigned width
, height
, border
, depth
;
395 XGetGeometry( xdisplay
, (Drawable
)xpixmap
, &root
, &x
, &y
,
396 &width
, &height
, &border
, &depth
);
397 copy
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
399 GC gc
= XCreateGC( xdisplay
, copy
, 0, NULL
);
400 XCopyArea( xdisplay
, xpixmap
, copy
, gc
, 0, 0, width
, height
, 0, 0 );
401 XFreeGC( xdisplay
, gc
);
404 wxBitmapRefData
* ref
= new wxBitmapRefData();
407 ref
->m_bitmap
= (WXPixmap
)copy
;
409 ref
->m_pixmap
= (WXPixmap
)copy
;
411 ref
->m_display
= (WXDisplay
*)xdisplay
;
412 ref
->m_width
= width
;
413 ref
->m_height
= height
;
421 bool wxBitmap::CreateFromXpm( const char **bits
)
423 wxCHECK_MSG( bits
, false, wxT("NULL pointer in wxBitmap::CreateFromXpm") );
425 return Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
428 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
433 wxASSERT_MSG(image
.Ok(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
437 int w
= image
.GetWidth();
438 int h
= image
.GetHeight();
440 if (!Create(w
, h
, depth
))
443 // Unfortunately the mask has to be screen-depth since
444 // 1-bpp bitmaps don't seem to be supported
445 // TODO: implement transparent drawing, presumably
446 // by doing several blits as per the Windows
447 // implementation because Nano-X doesn't support
449 // TODO: could perhaps speed this function up
450 // by making a buffer of pixel values,
451 // and then calling GrArea to write that to the
452 // pixmap. See demos/nxroach.c.
454 bool hasMask
= image
.HasMask();
456 GC pixmapGC
= GrNewGC();
457 Pixmap pixmap
= (Pixmap
) GetPixmap();
460 Pixmap maskPixmap
= 0;
462 unsigned char maskR
= 0;
463 unsigned char maskG
= 0;
464 unsigned char maskB
= 0;
468 maskR
= image
.GetMaskRed();
469 maskG
= image
.GetMaskGreen();
470 maskB
= image
.GetMaskBlue();
473 maskPixmap
= GrNewPixmap(w
, h
, 0);
478 wxMask
* mask
= new wxMask
;
479 mask
->SetBitmap((WXPixmap
) maskPixmap
);
484 GR_COLOR lastPixmapColour
= 0;
485 GR_COLOR lastMaskColour
= 0;
488 for (i
= 0; i
< w
; i
++)
490 for (j
= 0; j
< h
; j
++)
492 unsigned char red
= image
.GetRed(i
, j
);
493 unsigned char green
= image
.GetGreen(i
, j
);
494 unsigned char blue
= image
.GetBlue(i
, j
);
496 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
498 // Efficiency measure
499 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
501 GrSetGCForeground(pixmapGC
, colour
);
502 lastPixmapColour
= colour
;
505 GrPoint(pixmap
, pixmapGC
, i
, j
);
509 // scan the bitmap for the transparent colour and set the corresponding
510 // pixels in the mask to BLACK and the rest to WHITE
511 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
513 colour
= GR_RGB(0, 0, 0);
517 colour
= GR_RGB(255, 255, 255);
519 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
521 GrSetGCForeground(maskGC
, colour
);
522 lastMaskColour
= colour
;
524 GrPoint(maskPixmap
, maskGC
, i
, j
);
529 GrDestroyGC(pixmapGC
);
539 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
540 wxCHECK_MSG( depth
== -1, false, wxT("invalid bitmap depth") );
542 m_refData
= new wxBitmapRefData();
544 M_BMPDATA
->m_display
= wxGlobalDisplay();
546 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
548 int xscreen
= DefaultScreen( xdisplay
);
549 Window xroot
= RootWindow( xdisplay
, xscreen
);
550 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
552 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
554 int width
= image
.GetWidth();
555 int height
= image
.GetHeight();
556 M_BMPDATA
->m_width
= width
;
557 M_BMPDATA
->m_height
= height
;
559 if (depth
!= 1) depth
= bpp
;
560 M_BMPDATA
->m_bpp
= depth
;
564 wxFAIL_MSG( wxT("mono images later") );
570 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
571 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
573 if (data_image
->data
== NULL
)
575 wxLogError( wxT("Out of memory.") ); // TODO clean
579 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
583 XImage
*mask_image
= (XImage
*) NULL
;
586 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
587 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
589 if (mask_image
->data
== NULL
)
591 wxLogError( wxT("Out of memory.") ); // TODO clean
595 wxMask
*mask
= new wxMask();
596 mask
->SetDisplay( xdisplay
);
597 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
602 if (bpp
< 8) bpp
= 8;
606 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
607 byte_order b_o
= RGB
;
609 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
610 unsigned long greenMask
= vi
->m_visualGreenMask
,
611 redMask
= vi
->m_visualRedMask
,
612 blueMask
= vi
->m_visualBlueMask
;
616 if ((redMask
> greenMask
) && (greenMask
> blueMask
)) b_o
= RGB
;
617 else if ((redMask
> blueMask
) && (blueMask
> greenMask
)) b_o
= RBG
;
618 else if ((blueMask
> redMask
) && (redMask
> greenMask
)) b_o
= BRG
;
619 else if ((blueMask
> greenMask
) && (greenMask
> redMask
))b_o
= BGR
;
620 else if ((greenMask
> redMask
) && (redMask
> blueMask
)) b_o
= GRB
;
621 else if ((greenMask
> blueMask
) && (blueMask
> redMask
)) b_o
= GBR
;
624 int r_mask
= image
.GetMaskRed();
625 int g_mask
= image
.GetMaskGreen();
626 int b_mask
= image
.GetMaskBlue();
628 unsigned char* data
= image
.GetData();
629 wxASSERT_MSG( data
, wxT("No image data") );
631 unsigned char *colorCube
=
632 wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_colorCube
;
634 bool hasMask
= image
.HasMask();
637 for (int y
= 0; y
< height
; y
++)
639 for (int x
= 0; x
< width
; x
++)
650 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
651 XPutPixel( mask_image
, x
, y
, 0 );
653 XPutPixel( mask_image
, x
, y
, 1 );
661 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
662 XPutPixel( data_image
, x
, y
, pixel
);
670 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
671 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
672 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
673 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
674 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
675 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
677 XPutPixel( data_image
, x
, y
, pixel
);
685 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
686 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
687 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
688 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
689 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
690 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
692 XPutPixel( data_image
, x
, y
, pixel
);
697 // I actually don't know if for 16-bit displays, it is alway the green
698 // component or the second component which has 6 bits.
702 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
703 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
704 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
705 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
706 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
707 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
709 XPutPixel( data_image
, x
, y
, pixel
);
718 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
719 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
720 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
721 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
722 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
723 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
725 XPutPixel( data_image
, x
, y
, pixel
);
734 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
735 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
736 XDestroyImage( data_image
);
737 XFreeGC( xdisplay
, gc
);
743 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
744 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
746 XDestroyImage( mask_image
);
747 XFreeGC( xdisplay
, gc
);
756 wxImage
wxBitmap::ConvertToImage() const
760 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
762 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
763 wxASSERT_MSG( xdisplay
, wxT("No display") );
766 //int bpp = DefaultDepth(xdisplay, xscreen);
767 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
771 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
772 XImage
*x_image
= NULL
;
775 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
777 GetWidth(), GetHeight(),
778 AllPlanes
, ZPixmap
);
782 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
784 GetWidth(), GetHeight(),
785 AllPlanes
, ZPixmap
);
788 wxFAIL_MSG( wxT("Ill-formed bitmap") );
791 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
793 image
.Create( GetWidth(), GetHeight() );
794 char unsigned *data
= image
.GetData();
798 XDestroyImage( x_image
);
799 wxFAIL_MSG( wxT("couldn't create image") );
803 XImage
*x_image_mask
= NULL
;
806 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
808 GetWidth(), GetHeight(),
809 AllPlanes
, ZPixmap
);
811 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
814 int red_shift_right
= 0;
815 int green_shift_right
= 0;
816 int blue_shift_right
= 0;
817 int red_shift_left
= 0;
818 int green_shift_left
= 0;
819 int blue_shift_left
= 0;
820 bool use_shift
= false;
824 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
826 red_shift_right
= vi
->m_visualRedShift
;
827 red_shift_left
= 8 - vi
->m_visualRedPrec
;
828 green_shift_right
= vi
->m_visualGreenShift
;
829 green_shift_left
= 8 - vi
->m_visualGreenPrec
;
830 blue_shift_right
= vi
->m_visualBlueShift
;
831 blue_shift_left
= 8 - vi
->m_visualBluePrec
;
833 use_shift
= (vi
->m_visualType
== GrayScale
) ||
834 (vi
->m_visualType
!= PseudoColor
);
842 XColor
*colors
= (XColor
*)wxTheApp
->
843 GetVisualInfo(M_BMPDATA
->m_display
)->m_visualColormap
;
845 int width
= GetWidth();
846 int height
= GetHeight();
848 for (int j
= 0; j
< height
; j
++)
850 for (int i
= 0; i
< width
; i
++)
852 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
870 data
[pos
] = (unsigned char)((pixel
>> red_shift_right
) << red_shift_left
);
871 data
[pos
+1] = (unsigned char)((pixel
>> green_shift_right
) << green_shift_left
);
872 data
[pos
+2] = (unsigned char)((pixel
>> blue_shift_right
) << blue_shift_left
);
876 data
[pos
] = (unsigned char)(colors
[pixel
].red
>> 8);
877 data
[pos
+1] = (unsigned char)(colors
[pixel
].green
>> 8);
878 data
[pos
+2] = (unsigned char)(colors
[pixel
].blue
>> 8);
882 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
887 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
900 XDestroyImage( x_image
);
901 if (x_image_mask
) XDestroyImage( x_image_mask
);
907 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
909 LoadFile( filename
, type
);
912 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
914 m_refData
= new wxBitmapRefData
;
916 (void) Create((void*) bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
919 wxBitmap::~wxBitmap()
923 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
925 return m_refData
== bmp
.m_refData
;
928 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
930 return m_refData
!= bmp
.m_refData
;
933 bool wxBitmap::Ok() const
935 return (m_refData
!= NULL
);
938 int wxBitmap::GetHeight() const
940 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
942 return M_BMPDATA
->m_height
;
945 int wxBitmap::GetWidth() const
947 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
949 return M_BMPDATA
->m_width
;
952 int wxBitmap::GetDepth() const
954 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
956 return M_BMPDATA
->m_bpp
;
959 wxMask
*wxBitmap::GetMask() const
961 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
963 return M_BMPDATA
->m_mask
;
966 void wxBitmap::SetMask( wxMask
*mask
)
968 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
970 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
972 M_BMPDATA
->m_mask
= mask
;
975 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
981 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
984 (rect
.x
>= 0) && (rect
.y
>= 0) &&
985 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
986 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
987 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
989 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
990 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
994 wxMask
* mask
= new wxMask();
995 mask
->SetDisplay( GetMask()->GetDisplay() );
996 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
997 GetMask()->GetBitmap(),
999 rect
.width
, rect
.height
,
1002 ret
.SetMask( mask
);
1007 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
1010 rect
.width
, rect
.height
,
1011 M_BMPDATA
->m_bpp
) );
1016 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1019 rect
.width
, rect
.height
,
1026 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1027 const wxPalette
*palette
) const
1029 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1031 wxBitmapHandler
*handler
= FindHandler(type
);
1033 // Try to save the bitmap via wxImage handlers:
1034 if (handler
== NULL
)
1036 wxImage
image(this->ConvertToImage());
1037 if (image
.Ok()) return image
.SaveFile( name
, type
);
1042 return handler
->SaveFile(this, name
, type
, palette
);
1045 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1049 if (!wxFileExists(name
)) return false;
1051 wxBitmapHandler
*handler
= FindHandler(type
);
1053 if (handler
== NULL
)
1056 if (!image
.LoadFile( name
, type
))
1061 *this = wxBitmap(image
);
1067 return handler
->LoadFile(this, name
, type
, -1, -1);
1070 void wxBitmap::SetPalette(const wxPalette
& palette
)
1072 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1073 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1074 wxT("cannot set palette for bitmap of this depth"));
1076 delete M_BMPDATA
->m_palette
;
1077 M_BMPDATA
->m_palette
= NULL
;
1079 if (!palette
.Ok()) return;
1081 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1084 wxPalette
*wxBitmap::GetPalette() const
1086 if (!Ok()) return (wxPalette
*) NULL
;
1088 return M_BMPDATA
->m_palette
;
1091 void wxBitmap::SetHeight( int height
)
1093 if (!m_refData
) m_refData
= new wxBitmapRefData();
1095 M_BMPDATA
->m_height
= height
;
1098 void wxBitmap::SetWidth( int width
)
1100 if (!m_refData
) m_refData
= new wxBitmapRefData();
1102 M_BMPDATA
->m_width
= width
;
1105 void wxBitmap::SetDepth( int depth
)
1107 if (!m_refData
) m_refData
= new wxBitmapRefData();
1109 M_BMPDATA
->m_bpp
= depth
;
1112 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1114 if (!m_refData
) m_refData
= new wxBitmapRefData();
1116 M_BMPDATA
->m_pixmap
= pixmap
;
1119 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1121 if (!m_refData
) m_refData
= new wxBitmapRefData();
1123 M_BMPDATA
->m_bitmap
= bitmap
;
1126 WXPixmap
wxBitmap::GetPixmap() const
1128 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1130 return M_BMPDATA
->m_pixmap
;
1133 WXPixmap
wxBitmap::GetBitmap() const
1135 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1137 return M_BMPDATA
->m_bitmap
;
1140 WXPixmap
wxBitmap::GetDrawable() const
1142 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1144 return M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
: M_BMPDATA
->m_pixmap
;
1147 WXDisplay
*wxBitmap::GetDisplay() const
1149 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1151 return M_BMPDATA
->m_display
;
1155 // Copy from the drawable to the wxImage
1156 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1158 GR_SCREEN_INFO sinfo
;
1160 GR_PIXELVAL
*pixels
;
1161 GR_PALETTE
* palette
= NULL
;
1162 unsigned char rgb
[3], *pp
;
1164 GrGetScreenInfo(&sinfo
);
1166 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1167 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1170 GrGetSystemPalette(palette
);
1173 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1178 image
.Create(width
, height
);
1180 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1184 for(x
= 0; x
< sinfo
.cols
; x
++) {
1186 pp
= (unsigned char *)pixels
+
1187 ((x
+ (y
* sinfo
.cols
)) *
1188 sizeof(GR_PIXELVAL
));
1190 switch(sinfo
.pixtype
) {
1191 /* FIXME: These may need modifying on big endian. */
1192 case MWPF_TRUECOLOR0888
:
1193 case MWPF_TRUECOLOR888
:
1199 rgb
[0] = palette
->palette
[pp
[0]].r
;
1200 rgb
[1] = palette
->palette
[pp
[0]].g
;
1201 rgb
[2] = palette
->palette
[pp
[0]].b
;
1203 case MWPF_TRUECOLOR565
:
1204 rgb
[0] = pp
[1] & 0xf8;
1205 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1206 ((pp
[0] & 0xe0) >> 3);
1207 rgb
[2] = (pp
[0] & 0x1f) << 3;
1209 case MWPF_TRUECOLOR555
:
1210 rgb
[0] = (pp
[1] & 0x7c) << 1;
1211 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1212 ((pp
[0] & 0xe0) >> 2);
1213 rgb
[2] = (pp
[0] & 0x1f) << 3;
1215 case MWPF_TRUECOLOR332
:
1216 rgb
[0] = pp
[0] & 0xe0;
1217 rgb
[1] = (pp
[0] & 0x1c) << 3;
1218 rgb
[2] = (pp
[0] & 0x03) << 6;
1221 fprintf(stderr
, "Unsupported pixel "
1226 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1231 if(palette
) free(palette
);
1237 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1238 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1240 unsigned char rgb
[3], *pp
;
1242 pp
= (unsigned char*) & pixel
;
1244 switch (sinfo
.pixtype
)
1246 /* FIXME: These may need modifying on big endian. */
1247 case MWPF_TRUECOLOR0888
:
1248 case MWPF_TRUECOLOR888
:
1254 rgb
[0] = palette
->palette
[pp
[0]].r
;
1255 rgb
[1] = palette
->palette
[pp
[0]].g
;
1256 rgb
[2] = palette
->palette
[pp
[0]].b
;
1258 case MWPF_TRUECOLOR565
:
1259 rgb
[0] = pp
[1] & 0xf8;
1260 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1261 ((pp
[0] & 0xe0) >> 3);
1262 rgb
[2] = (pp
[0] & 0x1f) << 3;
1264 case MWPF_TRUECOLOR555
:
1265 rgb
[0] = (pp
[1] & 0x7c) << 1;
1266 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1267 ((pp
[0] & 0xe0) >> 2);
1268 rgb
[2] = (pp
[0] & 0x1f) << 3;
1270 case MWPF_TRUECOLOR332
:
1271 rgb
[0] = pp
[0] & 0xe0;
1272 rgb
[1] = (pp
[0] & 0x1c) << 3;
1273 rgb
[2] = (pp
[0] & 0x03) << 6;
1276 fprintf(stderr
, "Unsupported pixel format\n");
1291 // ============================================================================
1293 // ============================================================================
1295 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1297 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1301 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1303 // ----------------------------------------------------------------------------
1305 // ----------------------------------------------------------------------------
1307 class wxXPMFileHandler
: public wxBitmapHandler
1309 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1313 SetName( wxT("XPM file") );
1314 SetExtension( wxT("xpm") );
1315 SetType( wxBITMAP_TYPE_XPM
);
1318 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1319 int desiredWidth
, int desiredHeight
);
1321 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1322 int type
, const wxPalette
*palette
= NULL
);
1324 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
), void *WXUNUSED(data
), long WXUNUSED(flags
),
1325 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
) = 1)
1329 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1331 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1332 long WXUNUSED(flags
), int WXUNUSED(desiredWidth
),
1333 int WXUNUSED(desiredHeight
))
1336 if (!bitmap
->GetRefData())
1337 bitmap
->SetRefData( new wxBitmapRefData() );
1339 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1341 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1343 int xscreen
= DefaultScreen( xdisplay
);
1344 Window xroot
= RootWindow( xdisplay
, xscreen
);
1346 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1348 XpmAttributes xpmAttr
;
1349 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1354 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1355 (char*) name
.c_str(),
1356 &pixmap
, &mask
, &xpmAttr
);
1358 if (ErrorStatus
== XpmSuccess
)
1360 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1361 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1363 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1365 XpmFreeAttributes(&xpmAttr
);
1367 M_BMPHANDLERDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1371 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1372 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1373 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1385 wxXPMDecoder decoder
;
1386 wxFileInputStream
stream(name
);
1389 wxImage
image(decoder
.ReadFile(stream
));
1390 return image
.Ok() && bitmap
->CreateFromImage(image
);
1394 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1396 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1399 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1401 const wxPalette
*WXUNUSED(palette
))
1403 wxImage
image(bitmap
->ConvertToImage());
1404 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1409 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1411 // ----------------------------------------------------------------------------
1413 // ----------------------------------------------------------------------------
1415 class wxXPMDataHandler
: public wxBitmapHandler
1417 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1421 SetName( wxT("XPM data") );
1422 SetExtension( wxT("xpm") );
1423 SetType( wxBITMAP_TYPE_XPM_DATA
);
1426 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1427 const wxString
& WXUNUSED(name
),
1428 long WXUNUSED(flags
),
1429 int WXUNUSED(desiredWidth
),
1430 int WXUNUSED(desiredHeight
))
1433 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1434 const wxString
& WXUNUSED(name
),
1436 const wxPalette
*WXUNUSED(palette
) = NULL
)
1439 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1440 int width
, int height
, int depth
= 1);
1443 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1445 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *bits
,
1446 long WXUNUSED(flags
),
1447 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
))
1450 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
1452 if (!bitmap
->GetRefData())
1453 bitmap
->SetRefData( new wxBitmapRefData() );
1455 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1457 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1459 int xscreen
= DefaultScreen( xdisplay
);
1460 Window xroot
= RootWindow( xdisplay
, xscreen
);
1462 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1464 XpmAttributes xpmAttr
;
1465 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1470 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1471 &pixmap
, &mask
, &xpmAttr
);
1473 if (ErrorStatus
== XpmSuccess
)
1475 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1476 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1478 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1481 unsigned int depthRet
;
1483 unsigned int widthRet
, heightRet
, borderWidthRet
;
1484 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1485 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1487 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1490 XpmFreeAttributes(&xpmAttr
);
1492 M_BMPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
1496 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1497 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1498 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1508 #else // !wxHAVE_LIB_XPM
1509 wxXPMDecoder decoder
;
1510 wxImage
image(decoder
.ReadData((const char **)bits
));
1511 return image
.Ok() && bitmap
->CreateFromImage(image
);
1512 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1517 // ----------------------------------------------------------------------------
1519 // ----------------------------------------------------------------------------
1521 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1523 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1525 inline wxXBMDataHandler()
1527 SetName( wxT("XBM data") );
1528 SetExtension( wxT("xbm") );
1529 SetType( wxBITMAP_TYPE_XBM_DATA
);
1532 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1533 const wxString
& WXUNUSED(name
),
1534 long WXUNUSED(flags
),
1535 int WXUNUSED(desiredWidth
),
1536 int WXUNUSED(desiredHeight
))
1539 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1540 const wxString
& WXUNUSED(name
),
1542 const wxPalette
*WXUNUSED(palette
) = NULL
)
1545 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1546 int width
, int height
, int depth
= 1);
1549 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
1551 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, void *bits
,
1552 long WXUNUSED(flags
),
1553 int width
, int height
, int WXUNUSED(depth
))
1556 if (!bitmap
->GetRefData())
1557 bitmap
->SetRefData( new wxBitmapRefData() );
1559 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1561 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1563 int xscreen
= DefaultScreen( xdisplay
);
1564 Window xroot
= RootWindow( xdisplay
, xscreen
);
1566 M_BMPHANDLERDATA
->m_mask
= (wxMask
*) NULL
;
1567 M_BMPHANDLERDATA
->m_bitmap
=
1568 (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
,
1569 (char *) bits
, width
, height
);
1570 M_BMPHANDLERDATA
->m_width
= width
;
1571 M_BMPHANDLERDATA
->m_height
= height
;
1572 M_BMPHANDLERDATA
->m_bpp
= 1;
1576 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, false,
1577 wxT("couldn't create bitmap") );
1581 void wxBitmap::InitStandardHandlers()
1583 AddHandler(new wxXBMDataHandler
);
1585 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1586 AddHandler(new wxXPMFileHandler
);
1588 AddHandler(new wxXPMDataHandler
);