1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/bitmap.cpp
4 // Author: Julian Smart, Robert Roebling
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #include "wx/bitmap.h"
20 #include "wx/dcmemory.h"
26 #include "wx/x11/private.h"
28 /* No point in using libXPM for NanoX */
31 #define wxHAVE_LIB_XPM 0
33 // Copy from the drawable to the wxImage
34 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
);
41 #include "wx/xpmdecod.h"
42 #include "wx/wfstream.h"
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
58 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
61 Create( bitmap
, colour
);
64 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
67 Create( bitmap
, paletteIndex
);
70 wxMask::wxMask( const wxBitmap
& bitmap
)
79 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
82 bool wxMask::Create( const wxBitmap
& bitmap
,
83 const wxColour
& colour
)
88 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
92 m_display
= bitmap
.GetDisplay();
94 wxImage image
= bitmap
.ConvertToImage();
95 if (!image
.Ok()) return false;
97 m_display
= bitmap
.GetDisplay();
99 Display
*xdisplay
= (Display
*) m_display
;
100 int xscreen
= DefaultScreen( xdisplay
);
101 Window xroot
= RootWindow( xdisplay
, xscreen
);
103 m_bitmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, image
.GetWidth(), image
.GetHeight(), 1 );
104 GC gc
= XCreateGC( xdisplay
, (Pixmap
) m_bitmap
, 0, NULL
);
106 XSetForeground( xdisplay
, gc
, WhitePixel(xdisplay
,xscreen
) );
107 XSetFillStyle( xdisplay
, gc
, FillSolid
);
108 XFillRectangle( xdisplay
, (Pixmap
) m_bitmap
, gc
, 0, 0, image
.GetWidth(), image
.GetHeight() );
110 unsigned char *data
= image
.GetData();
113 unsigned char red
= colour
.Red();
114 unsigned char green
= colour
.Green();
115 unsigned char blue
= colour
.Blue();
117 int bpp
= wxTheApp
->GetVisualInfo(m_display
)->m_visualDepth
;
138 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
140 int width
= image
.GetWidth();
141 int height
= image
.GetHeight();
142 for (int j
= 0; j
< height
; j
++)
146 for (i
= 0; i
< width
; i
++)
148 if ((data
[index
] == red
) &&
149 (data
[index
+1] == green
) &&
150 (data
[index
+2] == blue
))
159 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
166 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
169 XFreeGC( xdisplay
, gc
);
178 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
181 wxPalette
*pal
= bitmap
.GetPalette();
183 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
185 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
187 return Create(bitmap
, wxColour(r
, g
, b
));
190 bool wxMask::Create( const wxBitmap
& bitmap
)
195 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
199 if (!bitmap
.Ok()) return false;
201 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
203 m_display
= bitmap
.GetDisplay();
205 int xscreen
= DefaultScreen( (Display
*) m_display
);
206 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
208 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
210 if (!m_bitmap
) return false;
212 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
214 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
215 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
217 XFreeGC( (Display
*) m_display
, gc
);
226 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
230 class wxBitmapRefData
: public wxObjectRefData
234 wxBitmapRefData(const wxBitmapRefData
& data
);
235 virtual ~wxBitmapRefData();
237 // shouldn't be called more than once as it doesn't free the existing data
238 bool Create(int width
, int height
, int depth
);
247 wxPalette
*m_palette
;
250 wxBitmapRefData::wxBitmapRefData()
259 m_palette
= (wxPalette
*) NULL
;
262 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
266 m_display
= data
.m_display
;
267 m_mask
= NULL
; // FIXME: should copy
268 m_palette
= NULL
; // FIXME: should copy
270 Create(data
.m_width
, data
.m_height
, data
.m_bpp
);
273 bool wxBitmapRefData::Create(int width
, int height
, int depth
)
279 m_display
= wxGlobalDisplay();
281 wxCHECK_MSG( m_display
, false, wxT("No display") );
283 int xscreen
= DefaultScreen(m_display
);
284 int bpp
= DefaultDepth(m_display
, xscreen
);
288 wxCHECK_MSG( (depth
== bpp
) || (depth
== 1), false,
289 wxT("invalid bitmap depth") );
292 m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
293 #else // !wxUSE_NANOX
294 Window xroot
= RootWindow(m_display
, xscreen
);
296 *(depth
== 1 ? &m_bitmap
: &m_pixmap
) =
297 XCreatePixmap(m_display
, xroot
, width
, height
, depth
);
298 #endif // wxUSE_NANOX/!wxUSE_NANOX
300 wxCHECK_MSG( m_pixmap
|| m_bitmap
, false, wxT("Bitmap creation failed") );
305 wxBitmapRefData::~wxBitmapRefData()
308 XFreePixmap(m_display
, m_pixmap
);
310 XFreePixmap(m_display
, m_bitmap
);
315 //-----------------------------------------------------------------------------
319 static WXPixmap
wxGetSubPixmap( WXDisplay
* xdisplay
, WXPixmap xpixmap
,
320 int x
, int y
, int width
, int height
,
323 Display
* const dpy
= (Display
*)xdisplay
;
325 int xscreen
= DefaultScreen( dpy
);
326 Window xroot
= RootWindow( dpy
, xscreen
);
327 Visual
* xvisual
= DefaultVisual( dpy
, xscreen
);
329 XImage
* ximage
= XCreateImage( dpy
, xvisual
, depth
,
330 ZPixmap
, 0, 0, width
, height
, 32, 0 );
331 ximage
->data
= (char*)malloc( ximage
->bytes_per_line
* ximage
->height
);
332 ximage
= XGetSubImage( dpy
, (Pixmap
)xpixmap
,
334 AllPlanes
, ZPixmap
, ximage
, 0, 0 );
336 GC gc
= XCreateGC( dpy
, (Pixmap
)xpixmap
, 0, NULL
);
337 Pixmap ret
= XCreatePixmap( dpy
, xroot
,
338 width
, height
, depth
);
340 XPutImage( dpy
, ret
, gc
, ximage
,
341 0, 0, 0, 0, width
, height
);
342 XDestroyImage( ximage
);
345 return (WXPixmap
)ret
;
348 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
350 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
356 wxBitmap::wxBitmap( int width
, int height
, int depth
)
358 Create( width
, height
, depth
);
361 bool wxBitmap::Create( int width
, int height
, int depth
)
365 wxCHECK_MSG( (width
> 0) && (height
> 0), false, wxT("invalid bitmap size") );
367 m_refData
= new wxBitmapRefData();
369 return M_BMPDATA
->Create(width
, height
, depth
);
372 bool wxBitmap::Create(const void* data
, wxBitmapType type
,
373 int width
, int height
, int depth
)
377 wxBitmapHandler
*handler
= FindHandler(type
);
379 if ( handler
== NULL
) {
380 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
386 return handler
->Create(this, data
, type
, width
, height
, depth
);
389 bool wxBitmap::Create(WXPixmap pixmap
)
392 Pixmap xpixmap
= (Pixmap
)pixmap
;
393 Display
* xdisplay
= wxGlobalDisplay();
394 int xscreen
= DefaultScreen( xdisplay
);
395 Window xroot
= RootWindow( xdisplay
, xscreen
);
397 // make a copy of the Pixmap
400 unsigned width
, height
, border
, depth
;
402 XGetGeometry( xdisplay
, (Drawable
)xpixmap
, &root
, &x
, &y
,
403 &width
, &height
, &border
, &depth
);
404 Pixmap copy
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
406 GC gc
= XCreateGC( xdisplay
, copy
, 0, NULL
);
407 XCopyArea( xdisplay
, xpixmap
, copy
, gc
, 0, 0, width
, height
, 0, 0 );
408 XFreeGC( xdisplay
, gc
);
411 wxBitmapRefData
* ref
= new wxBitmapRefData();
414 ref
->m_bitmap
= copy
;
416 ref
->m_pixmap
= copy
;
418 ref
->m_display
= xdisplay
;
419 ref
->m_width
= width
;
420 ref
->m_height
= height
;
428 wxBitmap::wxBitmap(const char* const* bits
)
430 Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
433 wxObjectRefData
*wxBitmap::CreateRefData() const
435 return new wxBitmapRefData
;
438 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*data
) const
440 return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData
*, data
));
443 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
448 wxASSERT_MSG(image
.Ok(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
452 int w
= image
.GetWidth();
453 int h
= image
.GetHeight();
455 if (!Create(w
, h
, depth
))
458 // Unfortunately the mask has to be screen-depth since
459 // 1-bpp bitmaps don't seem to be supported
460 // TODO: implement transparent drawing, presumably
461 // by doing several blits as per the Windows
462 // implementation because Nano-X doesn't support
464 // TODO: could perhaps speed this function up
465 // by making a buffer of pixel values,
466 // and then calling GrArea to write that to the
467 // pixmap. See demos/nxroach.c.
469 bool hasMask
= image
.HasMask();
471 GC pixmapGC
= GrNewGC();
472 Pixmap pixmap
= (Pixmap
) GetPixmap();
475 Pixmap maskPixmap
= 0;
477 unsigned char maskR
= 0;
478 unsigned char maskG
= 0;
479 unsigned char maskB
= 0;
483 maskR
= image
.GetMaskRed();
484 maskG
= image
.GetMaskGreen();
485 maskB
= image
.GetMaskBlue();
488 maskPixmap
= GrNewPixmap(w
, h
, 0);
493 wxMask
* mask
= new wxMask
;
494 mask
->SetBitmap((WXPixmap
) maskPixmap
);
499 GR_COLOR lastPixmapColour
= 0;
500 GR_COLOR lastMaskColour
= 0;
503 for (i
= 0; i
< w
; i
++)
505 for (j
= 0; j
< h
; j
++)
507 unsigned char red
= image
.GetRed(i
, j
);
508 unsigned char green
= image
.GetGreen(i
, j
);
509 unsigned char blue
= image
.GetBlue(i
, j
);
511 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
513 // Efficiency measure
514 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
516 GrSetGCForeground(pixmapGC
, colour
);
517 lastPixmapColour
= colour
;
520 GrPoint(pixmap
, pixmapGC
, i
, j
);
524 // scan the bitmap for the transparent colour and set the corresponding
525 // pixels in the mask to BLACK and the rest to WHITE
526 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
528 colour
= GR_RGB(0, 0, 0);
532 colour
= GR_RGB(255, 255, 255);
534 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
536 GrSetGCForeground(maskGC
, colour
);
537 lastMaskColour
= colour
;
539 GrPoint(maskPixmap
, maskGC
, i
, j
);
544 GrDestroyGC(pixmapGC
);
554 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
555 wxCHECK_MSG( depth
== -1, false, wxT("invalid bitmap depth") );
557 m_refData
= new wxBitmapRefData();
559 M_BMPDATA
->m_display
= wxGlobalDisplay();
561 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
563 int xscreen
= DefaultScreen( xdisplay
);
564 Window xroot
= RootWindow( xdisplay
, xscreen
);
565 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
567 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
569 int width
= image
.GetWidth();
570 int height
= image
.GetHeight();
571 M_BMPDATA
->m_width
= width
;
572 M_BMPDATA
->m_height
= height
;
574 if (depth
!= 1) depth
= bpp
;
575 M_BMPDATA
->m_bpp
= depth
;
579 wxFAIL_MSG( wxT("mono images later") );
585 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
586 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
588 if (data_image
->data
== NULL
)
590 wxLogError( wxT("Out of memory.") ); // TODO clean
594 M_BMPDATA
->m_pixmap
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
598 XImage
*mask_image
= (XImage
*) NULL
;
601 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
602 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
604 if (mask_image
->data
== NULL
)
606 wxLogError( wxT("Out of memory.") ); // TODO clean
610 wxMask
*mask
= new wxMask();
611 mask
->SetDisplay( xdisplay
);
612 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
617 if (bpp
< 8) bpp
= 8;
621 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
622 byte_order b_o
= RGB
;
624 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
625 unsigned long greenMask
= vi
->m_visualGreenMask
,
626 redMask
= vi
->m_visualRedMask
,
627 blueMask
= vi
->m_visualBlueMask
;
631 if ((redMask
> greenMask
) && (greenMask
> blueMask
)) b_o
= RGB
;
632 else if ((redMask
> blueMask
) && (blueMask
> greenMask
)) b_o
= RBG
;
633 else if ((blueMask
> redMask
) && (redMask
> greenMask
)) b_o
= BRG
;
634 else if ((blueMask
> greenMask
) && (greenMask
> redMask
))b_o
= BGR
;
635 else if ((greenMask
> redMask
) && (redMask
> blueMask
)) b_o
= GRB
;
636 else if ((greenMask
> blueMask
) && (blueMask
> redMask
)) b_o
= GBR
;
639 int r_mask
= image
.GetMaskRed();
640 int g_mask
= image
.GetMaskGreen();
641 int b_mask
= image
.GetMaskBlue();
643 unsigned char* data
= image
.GetData();
644 wxASSERT_MSG( data
, wxT("No image data") );
646 unsigned char *colorCube
=
647 wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_colorCube
;
649 bool hasMask
= image
.HasMask();
652 for (int y
= 0; y
< height
; y
++)
654 for (int x
= 0; x
< width
; x
++)
665 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
666 XPutPixel( mask_image
, x
, y
, 0 );
668 XPutPixel( mask_image
, x
, y
, 1 );
676 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
677 XPutPixel( data_image
, x
, y
, pixel
);
685 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
686 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
687 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
688 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
689 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
690 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
692 XPutPixel( data_image
, x
, y
, pixel
);
700 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
701 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
702 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
703 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
704 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
705 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
707 XPutPixel( data_image
, x
, y
, pixel
);
712 // I actually don't know if for 16-bit displays, it is alway the green
713 // component or the second component which has 6 bits.
717 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
718 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
719 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
720 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
721 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
722 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
724 XPutPixel( data_image
, x
, y
, pixel
);
733 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
734 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
735 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
736 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
737 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
738 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
740 XPutPixel( data_image
, x
, y
, pixel
);
749 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
750 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
751 XDestroyImage( data_image
);
752 XFreeGC( xdisplay
, gc
);
758 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
759 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
761 XDestroyImage( mask_image
);
762 XFreeGC( xdisplay
, gc
);
771 wxImage
wxBitmap::ConvertToImage() const
775 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
777 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
778 wxASSERT_MSG( xdisplay
, wxT("No display") );
781 //int bpp = DefaultDepth(xdisplay, xscreen);
782 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
786 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
787 XImage
*x_image
= NULL
;
790 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
792 GetWidth(), GetHeight(),
793 AllPlanes
, ZPixmap
);
797 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
799 GetWidth(), GetHeight(),
800 AllPlanes
, ZPixmap
);
803 wxFAIL_MSG( wxT("Ill-formed bitmap") );
806 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
808 image
.Create( GetWidth(), GetHeight() );
809 char unsigned *data
= image
.GetData();
813 XDestroyImage( x_image
);
814 wxFAIL_MSG( wxT("couldn't create image") );
818 XImage
*x_image_mask
= NULL
;
821 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
823 GetWidth(), GetHeight(),
824 AllPlanes
, ZPixmap
);
826 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
829 int red_shift_right
= 0;
830 int green_shift_right
= 0;
831 int blue_shift_right
= 0;
832 int red_shift_left
= 0;
833 int green_shift_left
= 0;
834 int blue_shift_left
= 0;
835 bool use_shift
= false;
839 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
841 red_shift_right
= vi
->m_visualRedShift
;
842 red_shift_left
= 8 - vi
->m_visualRedPrec
;
843 green_shift_right
= vi
->m_visualGreenShift
;
844 green_shift_left
= 8 - vi
->m_visualGreenPrec
;
845 blue_shift_right
= vi
->m_visualBlueShift
;
846 blue_shift_left
= 8 - vi
->m_visualBluePrec
;
848 use_shift
= (vi
->m_visualType
== GrayScale
) ||
849 (vi
->m_visualType
!= PseudoColor
);
857 XColor
*colors
= (XColor
*)wxTheApp
->
858 GetVisualInfo(M_BMPDATA
->m_display
)->m_visualColormap
;
860 int width
= GetWidth();
861 int height
= GetHeight();
863 for (int j
= 0; j
< height
; j
++)
865 for (int i
= 0; i
< width
; i
++)
867 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
885 data
[pos
] = (unsigned char)((pixel
>> red_shift_right
) << red_shift_left
);
886 data
[pos
+1] = (unsigned char)((pixel
>> green_shift_right
) << green_shift_left
);
887 data
[pos
+2] = (unsigned char)((pixel
>> blue_shift_right
) << blue_shift_left
);
891 data
[pos
] = (unsigned char)(colors
[pixel
].red
>> 8);
892 data
[pos
+1] = (unsigned char)(colors
[pixel
].green
>> 8);
893 data
[pos
+2] = (unsigned char)(colors
[pixel
].blue
>> 8);
897 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
902 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
915 XDestroyImage( x_image
);
916 if (x_image_mask
) XDestroyImage( x_image_mask
);
922 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
924 LoadFile( filename
, type
);
927 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
929 m_refData
= new wxBitmapRefData
;
931 (void) Create(bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
934 wxBitmap::~wxBitmap()
938 bool wxBitmap::IsOk() const
940 return (m_refData
!= NULL
);
943 int wxBitmap::GetHeight() const
945 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
947 return M_BMPDATA
->m_height
;
950 int wxBitmap::GetWidth() const
952 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
954 return M_BMPDATA
->m_width
;
957 int wxBitmap::GetDepth() const
959 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
961 return M_BMPDATA
->m_bpp
;
964 wxMask
*wxBitmap::GetMask() const
966 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
968 return M_BMPDATA
->m_mask
;
971 void wxBitmap::SetMask( wxMask
*mask
)
973 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
976 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
978 M_BMPDATA
->m_mask
= mask
;
981 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
987 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
990 (rect
.x
>= 0) && (rect
.y
>= 0) &&
991 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
992 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
993 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
995 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
996 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1000 wxMask
* mask
= new wxMask();
1001 mask
->SetDisplay( GetMask()->GetDisplay() );
1002 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
1003 GetMask()->GetBitmap(),
1005 rect
.width
, rect
.height
,
1008 ret
.SetMask( mask
);
1013 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
1016 rect
.width
, rect
.height
,
1017 M_BMPDATA
->m_bpp
) );
1022 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1025 rect
.width
, rect
.height
,
1032 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1033 const wxPalette
*palette
) const
1035 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1037 wxBitmapHandler
*handler
= FindHandler(type
);
1039 // Try to save the bitmap via wxImage handlers:
1040 if (handler
== NULL
)
1042 wxImage
image(this->ConvertToImage());
1043 if (image
.Ok()) return image
.SaveFile( name
, type
);
1048 return handler
->SaveFile(this, name
, type
, palette
);
1051 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1055 if (!wxFileExists(name
)) return false;
1057 wxBitmapHandler
*handler
= FindHandler(type
);
1059 if (handler
== NULL
)
1062 if (!image
.LoadFile( name
, type
))
1067 *this = wxBitmap(image
);
1073 return handler
->LoadFile(this, name
, type
, -1, -1);
1076 void wxBitmap::SetPalette(const wxPalette
& palette
)
1078 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1079 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1080 wxT("cannot set palette for bitmap of this depth"));
1083 delete M_BMPDATA
->m_palette
;
1084 M_BMPDATA
->m_palette
= NULL
;
1086 if (!palette
.Ok()) return;
1088 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1091 wxPalette
*wxBitmap::GetPalette() const
1093 if (!Ok()) return (wxPalette
*) NULL
;
1095 return M_BMPDATA
->m_palette
;
1098 void wxBitmap::SetHeight( int height
)
1102 M_BMPDATA
->m_height
= height
;
1105 void wxBitmap::SetWidth( int width
)
1109 M_BMPDATA
->m_width
= width
;
1112 void wxBitmap::SetDepth( int depth
)
1116 M_BMPDATA
->m_bpp
= depth
;
1119 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1121 if (!m_refData
) m_refData
= new wxBitmapRefData();
1123 M_BMPDATA
->m_pixmap
= (Pixmap
)pixmap
;
1126 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1128 if (!m_refData
) m_refData
= new wxBitmapRefData();
1130 M_BMPDATA
->m_bitmap
= (Pixmap
)bitmap
;
1133 WXPixmap
wxBitmap::GetPixmap() const
1135 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1137 return (WXPixmap
)M_BMPDATA
->m_pixmap
;
1140 WXPixmap
wxBitmap::GetBitmap() const
1142 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1144 return (WXPixmap
)M_BMPDATA
->m_bitmap
;
1147 WXPixmap
wxBitmap::GetDrawable() const
1149 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1151 return (WXPixmap
)(M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
1152 : M_BMPDATA
->m_pixmap
);
1155 WXDisplay
*wxBitmap::GetDisplay() const
1157 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1159 return M_BMPDATA
->m_display
;
1163 // Copy from the drawable to the wxImage
1164 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1166 GR_SCREEN_INFO sinfo
;
1168 GR_PIXELVAL
*pixels
;
1169 GR_PALETTE
* palette
= NULL
;
1170 unsigned char rgb
[3], *pp
;
1172 GrGetScreenInfo(&sinfo
);
1174 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1175 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1178 GrGetSystemPalette(palette
);
1181 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1186 image
.Create(width
, height
);
1188 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1192 for(x
= 0; x
< sinfo
.cols
; x
++) {
1194 pp
= (unsigned char *)pixels
+
1195 ((x
+ (y
* sinfo
.cols
)) *
1196 sizeof(GR_PIXELVAL
));
1198 switch(sinfo
.pixtype
) {
1199 /* FIXME: These may need modifying on big endian. */
1200 case MWPF_TRUECOLOR0888
:
1201 case MWPF_TRUECOLOR888
:
1207 rgb
[0] = palette
->palette
[pp
[0]].r
;
1208 rgb
[1] = palette
->palette
[pp
[0]].g
;
1209 rgb
[2] = palette
->palette
[pp
[0]].b
;
1211 case MWPF_TRUECOLOR565
:
1212 rgb
[0] = pp
[1] & 0xf8;
1213 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1214 ((pp
[0] & 0xe0) >> 3);
1215 rgb
[2] = (pp
[0] & 0x1f) << 3;
1217 case MWPF_TRUECOLOR555
:
1218 rgb
[0] = (pp
[1] & 0x7c) << 1;
1219 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1220 ((pp
[0] & 0xe0) >> 2);
1221 rgb
[2] = (pp
[0] & 0x1f) << 3;
1223 case MWPF_TRUECOLOR332
:
1224 rgb
[0] = pp
[0] & 0xe0;
1225 rgb
[1] = (pp
[0] & 0x1c) << 3;
1226 rgb
[2] = (pp
[0] & 0x03) << 6;
1229 fprintf(stderr
, "Unsupported pixel "
1234 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1239 if(palette
) free(palette
);
1245 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1246 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1248 unsigned char rgb
[3], *pp
;
1250 pp
= (unsigned char*) & pixel
;
1252 switch (sinfo
.pixtype
)
1254 /* FIXME: These may need modifying on big endian. */
1255 case MWPF_TRUECOLOR0888
:
1256 case MWPF_TRUECOLOR888
:
1262 rgb
[0] = palette
->palette
[pp
[0]].r
;
1263 rgb
[1] = palette
->palette
[pp
[0]].g
;
1264 rgb
[2] = palette
->palette
[pp
[0]].b
;
1266 case MWPF_TRUECOLOR565
:
1267 rgb
[0] = pp
[1] & 0xf8;
1268 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1269 ((pp
[0] & 0xe0) >> 3);
1270 rgb
[2] = (pp
[0] & 0x1f) << 3;
1272 case MWPF_TRUECOLOR555
:
1273 rgb
[0] = (pp
[1] & 0x7c) << 1;
1274 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1275 ((pp
[0] & 0xe0) >> 2);
1276 rgb
[2] = (pp
[0] & 0x1f) << 3;
1278 case MWPF_TRUECOLOR332
:
1279 rgb
[0] = pp
[0] & 0xe0;
1280 rgb
[1] = (pp
[0] & 0x1c) << 3;
1281 rgb
[2] = (pp
[0] & 0x03) << 6;
1284 fprintf(stderr
, "Unsupported pixel format\n");
1299 // ============================================================================
1301 // ============================================================================
1303 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1305 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1309 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1311 // ----------------------------------------------------------------------------
1313 // ----------------------------------------------------------------------------
1315 class wxXPMFileHandler
: public wxBitmapHandler
1317 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1321 SetName( wxT("XPM file") );
1322 SetExtension( wxT("xpm") );
1323 SetType( wxBITMAP_TYPE_XPM
);
1326 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1327 int desiredWidth
, int desiredHeight
);
1329 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1330 int type
, const wxPalette
*palette
= NULL
);
1332 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
), const void* WXUNUSED(data
), long WXUNUSED(flags
),
1333 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
) = 1)
1337 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1339 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1340 long WXUNUSED(flags
), int WXUNUSED(desiredWidth
),
1341 int WXUNUSED(desiredHeight
))
1344 if (!bitmap
->GetRefData())
1345 bitmap
->SetRefData( new wxBitmapRefData() );
1347 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1349 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1351 int xscreen
= DefaultScreen( xdisplay
);
1352 Window xroot
= RootWindow( xdisplay
, xscreen
);
1354 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1356 XpmAttributes xpmAttr
;
1357 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1362 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1363 (char*) name
.c_str(),
1364 &pixmap
, &mask
, &xpmAttr
);
1366 if (ErrorStatus
== XpmSuccess
)
1368 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1369 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1371 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1373 XpmFreeAttributes(&xpmAttr
);
1375 M_BMPHANDLERDATA
->m_bitmap
= (Pixmap
) pixmap
;
1379 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1380 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1381 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1393 wxXPMDecoder decoder
;
1394 wxFileInputStream
stream(name
);
1397 wxImage
image(decoder
.ReadFile(stream
));
1398 return image
.Ok() && bitmap
->CreateFromImage(image
);
1402 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1404 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1407 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1409 const wxPalette
*WXUNUSED(palette
))
1411 wxImage
image(bitmap
->ConvertToImage());
1412 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1417 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1419 // ----------------------------------------------------------------------------
1421 // ----------------------------------------------------------------------------
1423 class wxXPMDataHandler
: public wxBitmapHandler
1425 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1429 SetName( wxT("XPM data") );
1430 SetExtension( wxT("xpm") );
1431 SetType( wxBITMAP_TYPE_XPM_DATA
);
1434 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1435 const wxString
& WXUNUSED(name
),
1436 long WXUNUSED(flags
),
1437 int WXUNUSED(desiredWidth
),
1438 int WXUNUSED(desiredHeight
))
1441 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1442 const wxString
& WXUNUSED(name
),
1444 const wxPalette
*WXUNUSED(palette
) = NULL
)
1447 virtual bool Create(wxBitmap
*bitmap
, const void* data
, long flags
,
1448 int width
, int height
, int depth
= 1);
1451 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1453 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, const void* bits
,
1454 long WXUNUSED(flags
),
1455 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
))
1458 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
1460 if (!bitmap
->GetRefData())
1461 bitmap
->SetRefData( new wxBitmapRefData() );
1463 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1465 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1467 int xscreen
= DefaultScreen( xdisplay
);
1468 Window xroot
= RootWindow( xdisplay
, xscreen
);
1470 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1472 XpmAttributes xpmAttr
;
1473 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1478 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1479 &pixmap
, &mask
, &xpmAttr
);
1481 if (ErrorStatus
== XpmSuccess
)
1483 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1484 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1486 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1489 unsigned int depthRet
;
1491 unsigned int widthRet
, heightRet
, borderWidthRet
;
1492 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1493 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1495 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1498 XpmFreeAttributes(&xpmAttr
);
1500 M_BMPHANDLERDATA
->m_pixmap
= (Pixmap
) pixmap
;
1504 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1505 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1506 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1516 #else // !wxHAVE_LIB_XPM
1517 wxXPMDecoder decoder
;
1518 wxImage
image(decoder
.ReadData((const char **)bits
));
1519 return image
.Ok() && bitmap
->CreateFromImage(image
);
1520 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1525 // ----------------------------------------------------------------------------
1527 // ----------------------------------------------------------------------------
1529 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1531 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1533 inline wxXBMDataHandler()
1535 SetName( wxT("XBM data") );
1536 SetExtension( wxT("xbm") );
1537 SetType( wxBITMAP_TYPE_XBM_DATA
);
1540 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1541 const wxString
& WXUNUSED(name
),
1542 long WXUNUSED(flags
),
1543 int WXUNUSED(desiredWidth
),
1544 int WXUNUSED(desiredHeight
))
1547 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1548 const wxString
& WXUNUSED(name
),
1550 const wxPalette
*WXUNUSED(palette
) = NULL
)
1553 virtual bool Create(wxBitmap
*bitmap
, const void* data
, long flags
,
1554 int width
, int height
, int depth
= 1);
1557 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
1559 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, const void* bits
,
1560 long WXUNUSED(flags
),
1561 int width
, int height
, int WXUNUSED(depth
))
1564 if (!bitmap
->GetRefData())
1565 bitmap
->SetRefData( new wxBitmapRefData() );
1567 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1569 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1571 int xscreen
= DefaultScreen( xdisplay
);
1572 Window xroot
= RootWindow( xdisplay
, xscreen
);
1574 M_BMPHANDLERDATA
->m_mask
= (wxMask
*) NULL
;
1575 M_BMPHANDLERDATA
->m_bitmap
=
1576 XCreateBitmapFromData(xdisplay
, xroot
,
1577 (char *) bits
, width
, height
);
1578 M_BMPHANDLERDATA
->m_width
= width
;
1579 M_BMPHANDLERDATA
->m_height
= height
;
1580 M_BMPHANDLERDATA
->m_bpp
= 1;
1584 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, false,
1585 wxT("couldn't create bitmap") );
1589 void wxBitmap::InitStandardHandlers()
1591 AddHandler(new wxXBMDataHandler
);
1593 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1594 AddHandler(new wxXPMFileHandler
);
1596 AddHandler(new wxXPMDataHandler
);