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"
21 #include "wx/dcmemory.h"
24 #include "wx/x11/private.h"
26 /* No point in using libXPM for NanoX */
29 #define wxHAVE_LIB_XPM 0
31 // Copy from the drawable to the wxImage
32 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
);
39 #include "wx/xpmdecod.h"
40 #include "wx/wfstream.h"
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
57 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
60 Create( bitmap
, colour
);
63 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
66 Create( bitmap
, paletteIndex
);
69 wxMask::wxMask( const wxBitmap
& bitmap
)
78 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
81 bool wxMask::Create( const wxBitmap
& bitmap
,
82 const wxColour
& colour
)
87 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
91 m_display
= bitmap
.GetDisplay();
93 wxImage image
= bitmap
.ConvertToImage();
94 if (!image
.Ok()) return false;
96 m_display
= bitmap
.GetDisplay();
98 Display
*xdisplay
= (Display
*) m_display
;
99 int xscreen
= DefaultScreen( xdisplay
);
100 Window xroot
= RootWindow( xdisplay
, xscreen
);
102 m_bitmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, image
.GetWidth(), image
.GetHeight(), 1 );
103 GC gc
= XCreateGC( xdisplay
, (Pixmap
) m_bitmap
, 0, NULL
);
105 XSetForeground( xdisplay
, gc
, WhitePixel(xdisplay
,xscreen
) );
106 XSetFillStyle( xdisplay
, gc
, FillSolid
);
107 XFillRectangle( xdisplay
, (Pixmap
) m_bitmap
, gc
, 0, 0, image
.GetWidth(), image
.GetHeight() );
109 unsigned char *data
= image
.GetData();
112 unsigned char red
= colour
.Red();
113 unsigned char green
= colour
.Green();
114 unsigned char blue
= colour
.Blue();
116 int bpp
= wxTheApp
->GetVisualInfo(m_display
)->m_visualDepth
;
137 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
139 int width
= image
.GetWidth();
140 int height
= image
.GetHeight();
141 for (int j
= 0; j
< height
; j
++)
145 for (i
= 0; i
< width
; i
++)
147 if ((data
[index
] == red
) &&
148 (data
[index
+1] == green
) &&
149 (data
[index
+2] == blue
))
158 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
165 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
168 XFreeGC( xdisplay
, gc
);
177 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
180 wxPalette
*pal
= bitmap
.GetPalette();
182 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
184 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
186 return Create(bitmap
, wxColour(r
, g
, b
));
189 bool wxMask::Create( const wxBitmap
& bitmap
)
194 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
198 if (!bitmap
.Ok()) return false;
200 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
202 m_display
= bitmap
.GetDisplay();
204 int xscreen
= DefaultScreen( (Display
*) m_display
);
205 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
207 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
209 if (!m_bitmap
) return false;
211 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
213 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
214 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
216 XFreeGC( (Display
*) m_display
, gc
);
225 //-----------------------------------------------------------------------------
227 //-----------------------------------------------------------------------------
229 class wxBitmapRefData
: public wxObjectRefData
237 WXDisplay
*m_display
;
242 wxPalette
*m_palette
;
245 wxBitmapRefData::wxBitmapRefData()
250 m_mask
= (wxMask
*) NULL
;
254 m_palette
= (wxPalette
*) NULL
;
257 wxBitmapRefData::~wxBitmapRefData()
259 if (m_pixmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_pixmap
);
260 if (m_bitmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
261 if (m_mask
) delete m_mask
;
262 if (m_palette
) delete m_palette
;
265 //-----------------------------------------------------------------------------
269 static WXPixmap
wxGetSubPixmap( WXDisplay
* xdisplay
, WXPixmap xpixmap
,
270 int x
, int y
, int width
, int height
,
273 Display
* const dpy
= (Display
*)xdisplay
;
275 int xscreen
= DefaultScreen( dpy
);
276 Window xroot
= RootWindow( dpy
, xscreen
);
277 Visual
* xvisual
= DefaultVisual( dpy
, xscreen
);
279 XImage
* ximage
= XCreateImage( dpy
, xvisual
, depth
,
280 ZPixmap
, 0, 0, width
, height
, 32, 0 );
281 ximage
->data
= (char*)malloc( ximage
->bytes_per_line
* ximage
->height
);
282 ximage
= XGetSubImage( dpy
, (Pixmap
)xpixmap
,
284 AllPlanes
, ZPixmap
, ximage
, 0, 0 );
286 GC gc
= XCreateGC( dpy
, (Pixmap
)xpixmap
, 0, NULL
);
287 Pixmap ret
= XCreatePixmap( dpy
, xroot
,
288 width
, height
, depth
);
290 XPutImage( dpy
, ret
, gc
, ximage
,
291 0, 0, 0, 0, width
, height
);
292 XDestroyImage( ximage
);
295 return (WXPixmap
)ret
;
298 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
300 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
306 wxBitmap::wxBitmap( int width
, int height
, int depth
)
308 Create( width
, height
, depth
);
311 bool wxBitmap::Create( int width
, int height
, int depth
)
315 wxCHECK_MSG( (width
> 0) && (height
> 0), false, wxT("invalid bitmap size") )
317 m_refData
= new wxBitmapRefData();
319 M_BMPDATA
->m_display
= wxGlobalDisplay();
321 wxASSERT_MSG( M_BMPDATA
->m_display
, wxT("No display") );
323 int xscreen
= DefaultScreen( (Display
*) M_BMPDATA
->m_display
);
324 Window xroot
= RootWindow( (Display
*) M_BMPDATA
->m_display
, xscreen
);
326 int bpp
= DefaultDepth( (Display
*) M_BMPDATA
->m_display
, xscreen
);
327 if (depth
== -1) depth
= bpp
;
329 wxCHECK_MSG( (depth
== bpp
) ||
330 (depth
== 1), false, wxT("invalid bitmap depth") )
332 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
333 M_BMPDATA
->m_width
= width
;
334 M_BMPDATA
->m_height
= height
;
337 M_BMPDATA
->m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
338 M_BMPDATA
->m_bpp
= bpp
;
340 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Bitmap creation failed") );
344 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, 1 );
346 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
348 M_BMPDATA
->m_bpp
= 1;
352 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, depth
);
354 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Pixmap creation failed") );
356 M_BMPDATA
->m_bpp
= depth
;
362 bool wxBitmap::Create(void *data
, wxBitmapType type
,
363 int width
, int height
, int depth
)
367 wxBitmapHandler
*handler
= FindHandler(type
);
369 if ( handler
== NULL
) {
370 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
376 return handler
->Create(this, data
, type
, width
, height
, depth
);
379 bool wxBitmap::Create(WXPixmap pixmap
)
382 Pixmap xpixmap
= (Pixmap
)pixmap
;
383 Display
* xdisplay
= wxGlobalDisplay();
384 int xscreen
= DefaultScreen( xdisplay
);
385 Window xroot
= RootWindow( xdisplay
, xscreen
);
387 // make a copy of the Pixmap
391 unsigned width
, height
, border
, depth
;
393 XGetGeometry( xdisplay
, (Drawable
)xpixmap
, &root
, &x
, &y
,
394 &width
, &height
, &border
, &depth
);
395 copy
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
397 GC gc
= XCreateGC( xdisplay
, copy
, 0, NULL
);
398 XCopyArea( xdisplay
, xpixmap
, copy
, gc
, 0, 0, width
, height
, 0, 0 );
399 XFreeGC( xdisplay
, gc
);
402 wxBitmapRefData
* ref
= new wxBitmapRefData();
405 ref
->m_bitmap
= (WXPixmap
)copy
;
407 ref
->m_pixmap
= (WXPixmap
)copy
;
409 ref
->m_display
= (WXDisplay
*)xdisplay
;
410 ref
->m_width
= width
;
411 ref
->m_height
= height
;
419 bool wxBitmap::CreateFromXpm( const char **bits
)
421 wxCHECK_MSG( bits
, false, wxT("NULL pointer in wxBitmap::CreateFromXpm") );
423 return Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
426 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
431 wxASSERT_MSG(image
.Ok(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
435 int w
= image
.GetWidth();
436 int h
= image
.GetHeight();
438 if (!Create(w
, h
, depth
))
441 // Unfortunately the mask has to be screen-depth since
442 // 1-bpp bitmaps don't seem to be supported
443 // TODO: implement transparent drawing, presumably
444 // by doing several blits as per the Windows
445 // implementation because Nano-X doesn't support
447 // TODO: could perhaps speed this function up
448 // by making a buffer of pixel values,
449 // and then calling GrArea to write that to the
450 // pixmap. See demos/nxroach.c.
452 bool hasMask
= image
.HasMask();
454 GC pixmapGC
= GrNewGC();
455 Pixmap pixmap
= (Pixmap
) GetPixmap();
458 Pixmap maskPixmap
= 0;
460 unsigned char maskR
= 0;
461 unsigned char maskG
= 0;
462 unsigned char maskB
= 0;
466 maskR
= image
.GetMaskRed();
467 maskG
= image
.GetMaskGreen();
468 maskB
= image
.GetMaskBlue();
471 maskPixmap
= GrNewPixmap(w
, h
, 0);
476 wxMask
* mask
= new wxMask
;
477 mask
->SetBitmap((WXPixmap
) maskPixmap
);
482 GR_COLOR lastPixmapColour
= 0;
483 GR_COLOR lastMaskColour
= 0;
486 for (i
= 0; i
< w
; i
++)
488 for (j
= 0; j
< h
; j
++)
490 unsigned char red
= image
.GetRed(i
, j
);
491 unsigned char green
= image
.GetGreen(i
, j
);
492 unsigned char blue
= image
.GetBlue(i
, j
);
494 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
496 // Efficiency measure
497 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
499 GrSetGCForeground(pixmapGC
, colour
);
500 lastPixmapColour
= colour
;
503 GrPoint(pixmap
, pixmapGC
, i
, j
);
507 // scan the bitmap for the transparent colour and set the corresponding
508 // pixels in the mask to BLACK and the rest to WHITE
509 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
511 colour
= GR_RGB(0, 0, 0);
515 colour
= GR_RGB(255, 255, 255);
517 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
519 GrSetGCForeground(maskGC
, colour
);
520 lastMaskColour
= colour
;
522 GrPoint(maskPixmap
, maskGC
, i
, j
);
527 GrDestroyGC(pixmapGC
);
537 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") )
538 wxCHECK_MSG( depth
== -1, false, wxT("invalid bitmap depth") )
540 m_refData
= new wxBitmapRefData();
542 M_BMPDATA
->m_display
= wxGlobalDisplay();
544 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
546 int xscreen
= DefaultScreen( xdisplay
);
547 Window xroot
= RootWindow( xdisplay
, xscreen
);
548 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
550 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
552 int width
= image
.GetWidth();
553 int height
= image
.GetHeight();
554 M_BMPDATA
->m_width
= width
;
555 M_BMPDATA
->m_height
= height
;
557 if (depth
!= 1) depth
= bpp
;
558 M_BMPDATA
->m_bpp
= depth
;
562 wxFAIL_MSG( wxT("mono images later") );
568 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
569 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
571 if (data_image
->data
== NULL
)
573 wxLogError( wxT("Out of memory.") ); // TODO clean
577 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
581 XImage
*mask_image
= (XImage
*) NULL
;
584 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
585 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
587 if (mask_image
->data
== NULL
)
589 wxLogError( wxT("Out of memory.") ); // TODO clean
593 wxMask
*mask
= new wxMask();
594 mask
->SetDisplay( xdisplay
);
595 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
600 if (bpp
< 8) bpp
= 8;
604 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
605 byte_order b_o
= RGB
;
607 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
608 unsigned long greenMask
= vi
->m_visualGreenMask
,
609 redMask
= vi
->m_visualRedMask
,
610 blueMask
= vi
->m_visualBlueMask
;
614 if ((redMask
> greenMask
) && (greenMask
> blueMask
)) b_o
= RGB
;
615 else if ((redMask
> blueMask
) && (blueMask
> greenMask
)) b_o
= RBG
;
616 else if ((blueMask
> redMask
) && (redMask
> greenMask
)) b_o
= BRG
;
617 else if ((blueMask
> greenMask
) && (greenMask
> redMask
))b_o
= BGR
;
618 else if ((greenMask
> redMask
) && (redMask
> blueMask
)) b_o
= GRB
;
619 else if ((greenMask
> blueMask
) && (blueMask
> redMask
)) b_o
= GBR
;
622 int r_mask
= image
.GetMaskRed();
623 int g_mask
= image
.GetMaskGreen();
624 int b_mask
= image
.GetMaskBlue();
626 unsigned char* data
= image
.GetData();
627 wxASSERT_MSG( data
, wxT("No image data") );
629 unsigned char *colorCube
=
630 wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_colorCube
;
632 bool hasMask
= image
.HasMask();
635 for (int y
= 0; y
< height
; y
++)
637 for (int x
= 0; x
< width
; x
++)
648 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
649 XPutPixel( mask_image
, x
, y
, 0 );
651 XPutPixel( mask_image
, x
, y
, 1 );
659 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
660 XPutPixel( data_image
, x
, y
, pixel
);
668 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
669 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
670 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
671 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
672 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
673 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
675 XPutPixel( data_image
, x
, y
, pixel
);
683 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
684 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
685 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
686 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
687 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
688 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
690 XPutPixel( data_image
, x
, y
, pixel
);
695 // I actually don't know if for 16-bit displays, it is alway the green
696 // component or the second component which has 6 bits.
700 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
701 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
702 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
703 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
704 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
705 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
707 XPutPixel( data_image
, x
, y
, pixel
);
716 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
717 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
718 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
719 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
720 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
721 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
723 XPutPixel( data_image
, x
, y
, pixel
);
732 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
733 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
734 XDestroyImage( data_image
);
735 XFreeGC( xdisplay
, gc
);
741 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
742 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
744 XDestroyImage( mask_image
);
745 XFreeGC( xdisplay
, gc
);
754 wxImage
wxBitmap::ConvertToImage() const
758 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
760 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
761 wxASSERT_MSG( xdisplay
, wxT("No display") );
764 //int bpp = DefaultDepth(xdisplay, xscreen);
765 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
769 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
770 XImage
*x_image
= NULL
;
773 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
775 GetWidth(), GetHeight(),
776 AllPlanes
, ZPixmap
);
780 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
782 GetWidth(), GetHeight(),
783 AllPlanes
, ZPixmap
);
786 wxFAIL_MSG( wxT("Ill-formed bitmap") );
789 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
791 image
.Create( GetWidth(), GetHeight() );
792 char unsigned *data
= image
.GetData();
796 XDestroyImage( x_image
);
797 wxFAIL_MSG( wxT("couldn't create image") );
801 XImage
*x_image_mask
= NULL
;
804 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
806 GetWidth(), GetHeight(),
807 AllPlanes
, ZPixmap
);
809 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
812 int red_shift_right
= 0;
813 int green_shift_right
= 0;
814 int blue_shift_right
= 0;
815 int red_shift_left
= 0;
816 int green_shift_left
= 0;
817 int blue_shift_left
= 0;
818 bool use_shift
= false;
822 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
824 red_shift_right
= vi
->m_visualRedShift
;
825 red_shift_left
= 8 - vi
->m_visualRedPrec
;
826 green_shift_right
= vi
->m_visualGreenShift
;
827 green_shift_left
= 8 - vi
->m_visualGreenPrec
;
828 blue_shift_right
= vi
->m_visualBlueShift
;
829 blue_shift_left
= 8 - vi
->m_visualBluePrec
;
831 use_shift
= (vi
->m_visualType
== GrayScale
) ||
832 (vi
->m_visualType
!= PseudoColor
);
840 XColor
*colors
= (XColor
*)wxTheApp
->
841 GetVisualInfo(M_BMPDATA
->m_display
)->m_visualColormap
;
843 int width
= GetWidth();
844 int height
= GetHeight();
846 for (int j
= 0; j
< height
; j
++)
848 for (int i
= 0; i
< width
; i
++)
850 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
868 data
[pos
] = (unsigned char)((pixel
>> red_shift_right
) << red_shift_left
);
869 data
[pos
+1] = (unsigned char)((pixel
>> green_shift_right
) << green_shift_left
);
870 data
[pos
+2] = (unsigned char)((pixel
>> blue_shift_right
) << blue_shift_left
);
874 data
[pos
] = (unsigned char)(colors
[pixel
].red
>> 8);
875 data
[pos
+1] = (unsigned char)(colors
[pixel
].green
>> 8);
876 data
[pos
+2] = (unsigned char)(colors
[pixel
].blue
>> 8);
880 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
885 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
898 XDestroyImage( x_image
);
899 if (x_image_mask
) XDestroyImage( x_image_mask
);
905 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
910 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
912 LoadFile( filename
, type
);
915 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
917 m_refData
= new wxBitmapRefData
;
919 (void) Create((void*) bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
922 wxBitmap::~wxBitmap()
926 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
928 if ( m_refData
!= bmp
.m_refData
)
934 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
936 return m_refData
== bmp
.m_refData
;
939 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
941 return m_refData
!= bmp
.m_refData
;
944 bool wxBitmap::Ok() const
946 return (m_refData
!= NULL
);
949 int wxBitmap::GetHeight() const
951 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
953 return M_BMPDATA
->m_height
;
956 int wxBitmap::GetWidth() const
958 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
960 return M_BMPDATA
->m_width
;
963 int wxBitmap::GetDepth() const
965 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
967 return M_BMPDATA
->m_bpp
;
970 wxMask
*wxBitmap::GetMask() const
972 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
974 return M_BMPDATA
->m_mask
;
977 void wxBitmap::SetMask( wxMask
*mask
)
979 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
981 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
983 M_BMPDATA
->m_mask
= mask
;
986 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
992 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
995 (rect
.x
>= 0) && (rect
.y
>= 0) &&
996 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
997 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
998 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1000 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1001 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1005 wxMask
* mask
= new wxMask();
1006 mask
->SetDisplay( GetMask()->GetDisplay() );
1007 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
1008 GetMask()->GetBitmap(),
1010 rect
.width
, rect
.height
,
1013 ret
.SetMask( mask
);
1018 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
1021 rect
.width
, rect
.height
,
1022 M_BMPDATA
->m_bpp
) );
1027 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1030 rect
.width
, rect
.height
,
1037 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1038 const wxPalette
*palette
) const
1040 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1042 wxBitmapHandler
*handler
= FindHandler(type
);
1044 // Try to save the bitmap via wxImage handlers:
1045 if (handler
== NULL
)
1047 wxImage
image(this->ConvertToImage());
1048 if (image
.Ok()) return image
.SaveFile( name
, type
);
1053 return handler
->SaveFile(this, name
, type
, palette
);
1056 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1060 if (!wxFileExists(name
)) return false;
1062 wxBitmapHandler
*handler
= FindHandler(type
);
1064 if (handler
== NULL
)
1067 if (!image
.LoadFile( name
, type
))
1072 *this = wxBitmap(image
);
1078 return handler
->LoadFile(this, name
, type
, -1, -1);
1081 void wxBitmap::SetPalette(const wxPalette
& palette
)
1083 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1084 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1085 wxT("cannot set palette for bitmap of this depth"));
1087 delete M_BMPDATA
->m_palette
;
1088 M_BMPDATA
->m_palette
= NULL
;
1090 if (!palette
.Ok()) return;
1092 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1095 wxPalette
*wxBitmap::GetPalette() const
1097 if (!Ok()) return (wxPalette
*) NULL
;
1099 return M_BMPDATA
->m_palette
;
1102 void wxBitmap::SetHeight( int height
)
1104 if (!m_refData
) m_refData
= new wxBitmapRefData();
1106 M_BMPDATA
->m_height
= height
;
1109 void wxBitmap::SetWidth( int width
)
1111 if (!m_refData
) m_refData
= new wxBitmapRefData();
1113 M_BMPDATA
->m_width
= width
;
1116 void wxBitmap::SetDepth( int depth
)
1118 if (!m_refData
) m_refData
= new wxBitmapRefData();
1120 M_BMPDATA
->m_bpp
= depth
;
1123 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1125 if (!m_refData
) m_refData
= new wxBitmapRefData();
1127 M_BMPDATA
->m_pixmap
= pixmap
;
1130 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1132 if (!m_refData
) m_refData
= new wxBitmapRefData();
1134 M_BMPDATA
->m_bitmap
= bitmap
;
1137 WXPixmap
wxBitmap::GetPixmap() const
1139 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1141 return M_BMPDATA
->m_pixmap
;
1144 WXPixmap
wxBitmap::GetBitmap() const
1146 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1148 return M_BMPDATA
->m_bitmap
;
1151 WXPixmap
wxBitmap::GetDrawable() const
1153 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1155 return M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
: M_BMPDATA
->m_pixmap
;
1158 WXDisplay
*wxBitmap::GetDisplay() const
1160 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1162 return M_BMPDATA
->m_display
;
1166 // Copy from the drawable to the wxImage
1167 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1169 GR_SCREEN_INFO sinfo
;
1171 GR_PIXELVAL
*pixels
;
1172 GR_PALETTE
* palette
= NULL
;
1173 unsigned char rgb
[3], *pp
;
1175 GrGetScreenInfo(&sinfo
);
1177 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1178 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1181 GrGetSystemPalette(palette
);
1184 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1189 image
.Create(width
, height
);
1191 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1195 for(x
= 0; x
< sinfo
.cols
; x
++) {
1197 pp
= (unsigned char *)pixels
+
1198 ((x
+ (y
* sinfo
.cols
)) *
1199 sizeof(GR_PIXELVAL
));
1201 switch(sinfo
.pixtype
) {
1202 /* FIXME: These may need modifying on big endian. */
1203 case MWPF_TRUECOLOR0888
:
1204 case MWPF_TRUECOLOR888
:
1210 rgb
[0] = palette
->palette
[pp
[0]].r
;
1211 rgb
[1] = palette
->palette
[pp
[0]].g
;
1212 rgb
[2] = palette
->palette
[pp
[0]].b
;
1214 case MWPF_TRUECOLOR565
:
1215 rgb
[0] = pp
[1] & 0xf8;
1216 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1217 ((pp
[0] & 0xe0) >> 3);
1218 rgb
[2] = (pp
[0] & 0x1f) << 3;
1220 case MWPF_TRUECOLOR555
:
1221 rgb
[0] = (pp
[1] & 0x7c) << 1;
1222 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1223 ((pp
[0] & 0xe0) >> 2);
1224 rgb
[2] = (pp
[0] & 0x1f) << 3;
1226 case MWPF_TRUECOLOR332
:
1227 rgb
[0] = pp
[0] & 0xe0;
1228 rgb
[1] = (pp
[0] & 0x1c) << 3;
1229 rgb
[2] = (pp
[0] & 0x03) << 6;
1232 fprintf(stderr
, "Unsupported pixel "
1237 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1242 if(palette
) free(palette
);
1248 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1249 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1251 unsigned char rgb
[3], *pp
;
1253 pp
= (unsigned char*) & pixel
;
1255 switch (sinfo
.pixtype
)
1257 /* FIXME: These may need modifying on big endian. */
1258 case MWPF_TRUECOLOR0888
:
1259 case MWPF_TRUECOLOR888
:
1265 rgb
[0] = palette
->palette
[pp
[0]].r
;
1266 rgb
[1] = palette
->palette
[pp
[0]].g
;
1267 rgb
[2] = palette
->palette
[pp
[0]].b
;
1269 case MWPF_TRUECOLOR565
:
1270 rgb
[0] = pp
[1] & 0xf8;
1271 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1272 ((pp
[0] & 0xe0) >> 3);
1273 rgb
[2] = (pp
[0] & 0x1f) << 3;
1275 case MWPF_TRUECOLOR555
:
1276 rgb
[0] = (pp
[1] & 0x7c) << 1;
1277 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1278 ((pp
[0] & 0xe0) >> 2);
1279 rgb
[2] = (pp
[0] & 0x1f) << 3;
1281 case MWPF_TRUECOLOR332
:
1282 rgb
[0] = pp
[0] & 0xe0;
1283 rgb
[1] = (pp
[0] & 0x1c) << 3;
1284 rgb
[2] = (pp
[0] & 0x03) << 6;
1287 fprintf(stderr
, "Unsupported pixel format\n");
1302 // ============================================================================
1304 // ============================================================================
1306 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1308 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1312 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1314 // ----------------------------------------------------------------------------
1316 // ----------------------------------------------------------------------------
1318 class wxXPMFileHandler
: public wxBitmapHandler
1320 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1324 SetName( wxT("XPM file") );
1325 SetExtension( wxT("xpm") );
1326 SetType( wxBITMAP_TYPE_XPM
);
1329 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1330 int desiredWidth
, int desiredHeight
);
1332 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1333 int type
, const wxPalette
*palette
= NULL
);
1335 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
), void *WXUNUSED(data
), long WXUNUSED(flags
),
1336 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
) = 1)
1340 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1342 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1343 long WXUNUSED(flags
), int WXUNUSED(desiredWidth
),
1344 int WXUNUSED(desiredHeight
))
1347 if (!bitmap
->GetRefData())
1348 bitmap
->SetRefData( new wxBitmapRefData() );
1350 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1352 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1354 int xscreen
= DefaultScreen( xdisplay
);
1355 Window xroot
= RootWindow( xdisplay
, xscreen
);
1357 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1359 XpmAttributes xpmAttr
;
1360 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1365 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1366 (char*) name
.c_str(),
1367 &pixmap
, &mask
, &xpmAttr
);
1369 if (ErrorStatus
== XpmSuccess
)
1371 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1372 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1374 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1376 XpmFreeAttributes(&xpmAttr
);
1378 M_BMPHANDLERDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1382 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1383 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1384 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1396 wxXPMDecoder decoder
;
1397 wxFileInputStream
stream(name
);
1400 wxImage
image(decoder
.ReadFile(stream
));
1401 return image
.Ok() && bitmap
->CreateFromImage(image
);
1405 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1407 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1410 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1412 const wxPalette
*WXUNUSED(palette
))
1414 wxImage
image(bitmap
->ConvertToImage());
1415 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1420 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1422 // ----------------------------------------------------------------------------
1424 // ----------------------------------------------------------------------------
1426 class wxXPMDataHandler
: public wxBitmapHandler
1428 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1432 SetName( wxT("XPM data") );
1433 SetExtension( wxT("xpm") );
1434 SetType( wxBITMAP_TYPE_XPM_DATA
);
1437 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1438 const wxString
& WXUNUSED(name
),
1439 long WXUNUSED(flags
),
1440 int WXUNUSED(desiredWidth
),
1441 int WXUNUSED(desiredHeight
))
1444 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1445 const wxString
& WXUNUSED(name
),
1447 const wxPalette
*WXUNUSED(palette
) = NULL
)
1450 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1451 int width
, int height
, int depth
= 1);
1454 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1456 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *bits
,
1457 long WXUNUSED(flags
),
1458 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
))
1461 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") )
1463 if (!bitmap
->GetRefData())
1464 bitmap
->SetRefData( new wxBitmapRefData() );
1466 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1468 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1470 int xscreen
= DefaultScreen( xdisplay
);
1471 Window xroot
= RootWindow( xdisplay
, xscreen
);
1473 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1475 XpmAttributes xpmAttr
;
1476 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1481 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1482 &pixmap
, &mask
, &xpmAttr
);
1484 if (ErrorStatus
== XpmSuccess
)
1486 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1487 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1489 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1492 unsigned int depthRet
;
1494 unsigned int widthRet
, heightRet
, borderWidthRet
;
1495 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1496 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1498 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1501 XpmFreeAttributes(&xpmAttr
);
1503 M_BMPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
1507 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1508 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1509 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1519 #else // !wxHAVE_LIB_XPM
1520 wxXPMDecoder decoder
;
1521 wxImage
image(decoder
.ReadData((const char **)bits
));
1522 return image
.Ok() && bitmap
->CreateFromImage(image
);
1523 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1528 // ----------------------------------------------------------------------------
1530 // ----------------------------------------------------------------------------
1532 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1534 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1536 inline wxXBMDataHandler()
1538 SetName( wxT("XBM data") );
1539 SetExtension( wxT("xbm") );
1540 SetType( wxBITMAP_TYPE_XBM_DATA
);
1543 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1544 const wxString
& WXUNUSED(name
),
1545 long WXUNUSED(flags
),
1546 int WXUNUSED(desiredWidth
),
1547 int WXUNUSED(desiredHeight
))
1550 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1551 const wxString
& WXUNUSED(name
),
1553 const wxPalette
*WXUNUSED(palette
) = NULL
)
1556 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1557 int width
, int height
, int depth
= 1);
1560 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
1562 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, void *bits
,
1563 long WXUNUSED(flags
),
1564 int width
, int height
, int WXUNUSED(depth
))
1567 if (!bitmap
->GetRefData())
1568 bitmap
->SetRefData( new wxBitmapRefData() );
1570 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1572 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1574 int xscreen
= DefaultScreen( xdisplay
);
1575 Window xroot
= RootWindow( xdisplay
, xscreen
);
1577 M_BMPHANDLERDATA
->m_mask
= (wxMask
*) NULL
;
1578 M_BMPHANDLERDATA
->m_bitmap
=
1579 (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
,
1580 (char *) bits
, width
, height
);
1581 M_BMPHANDLERDATA
->m_width
= width
;
1582 M_BMPHANDLERDATA
->m_height
= height
;
1583 M_BMPHANDLERDATA
->m_bpp
= 1;
1587 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, false,
1588 wxT("couldn't create bitmap") );
1592 void wxBitmap::InitStandardHandlers()
1594 AddHandler(new wxXBMDataHandler
);
1596 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1597 AddHandler(new wxXPMFileHandler
);
1599 AddHandler(new wxXPMDataHandler
);