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 virtual ~wxBitmapRefData();
238 WXDisplay
*m_display
;
243 wxPalette
*m_palette
;
246 wxBitmapRefData::wxBitmapRefData()
251 m_mask
= (wxMask
*) NULL
;
255 m_palette
= (wxPalette
*) NULL
;
258 wxBitmapRefData::~wxBitmapRefData()
260 if (m_pixmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_pixmap
);
261 if (m_bitmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
262 if (m_mask
) delete m_mask
;
263 if (m_palette
) delete m_palette
;
266 //-----------------------------------------------------------------------------
270 static WXPixmap
wxGetSubPixmap( WXDisplay
* xdisplay
, WXPixmap xpixmap
,
271 int x
, int y
, int width
, int height
,
274 Display
* const dpy
= (Display
*)xdisplay
;
276 int xscreen
= DefaultScreen( dpy
);
277 Window xroot
= RootWindow( dpy
, xscreen
);
278 Visual
* xvisual
= DefaultVisual( dpy
, xscreen
);
280 XImage
* ximage
= XCreateImage( dpy
, xvisual
, depth
,
281 ZPixmap
, 0, 0, width
, height
, 32, 0 );
282 ximage
->data
= (char*)malloc( ximage
->bytes_per_line
* ximage
->height
);
283 ximage
= XGetSubImage( dpy
, (Pixmap
)xpixmap
,
285 AllPlanes
, ZPixmap
, ximage
, 0, 0 );
287 GC gc
= XCreateGC( dpy
, (Pixmap
)xpixmap
, 0, NULL
);
288 Pixmap ret
= XCreatePixmap( dpy
, xroot
,
289 width
, height
, depth
);
291 XPutImage( dpy
, ret
, gc
, ximage
,
292 0, 0, 0, 0, width
, height
);
293 XDestroyImage( ximage
);
296 return (WXPixmap
)ret
;
299 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
301 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
307 wxBitmap::wxBitmap( int width
, int height
, int depth
)
309 Create( width
, height
, depth
);
312 bool wxBitmap::Create( int width
, int height
, int depth
)
316 wxCHECK_MSG( (width
> 0) && (height
> 0), false, wxT("invalid bitmap size") );
318 m_refData
= new wxBitmapRefData();
320 M_BMPDATA
->m_display
= wxGlobalDisplay();
322 wxASSERT_MSG( M_BMPDATA
->m_display
, wxT("No display") );
324 int xscreen
= DefaultScreen( (Display
*) M_BMPDATA
->m_display
);
325 Window xroot
= RootWindow( (Display
*) M_BMPDATA
->m_display
, xscreen
);
327 int bpp
= DefaultDepth( (Display
*) M_BMPDATA
->m_display
, xscreen
);
328 if (depth
== -1) depth
= bpp
;
330 wxCHECK_MSG( (depth
== bpp
) ||
331 (depth
== 1), false, wxT("invalid bitmap depth") );
333 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
334 M_BMPDATA
->m_width
= width
;
335 M_BMPDATA
->m_height
= height
;
338 M_BMPDATA
->m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
339 M_BMPDATA
->m_bpp
= bpp
;
341 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Bitmap creation failed") );
345 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, 1 );
347 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
349 M_BMPDATA
->m_bpp
= 1;
353 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, depth
);
355 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Pixmap creation failed") );
357 M_BMPDATA
->m_bpp
= depth
;
363 bool wxBitmap::Create(const void* data
, wxBitmapType type
,
364 int width
, int height
, int depth
)
368 wxBitmapHandler
*handler
= FindHandler(type
);
370 if ( handler
== NULL
) {
371 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
377 return handler
->Create(this, data
, type
, width
, height
, depth
);
380 bool wxBitmap::Create(WXPixmap pixmap
)
383 Pixmap xpixmap
= (Pixmap
)pixmap
;
384 Display
* xdisplay
= wxGlobalDisplay();
385 int xscreen
= DefaultScreen( xdisplay
);
386 Window xroot
= RootWindow( xdisplay
, xscreen
);
388 // make a copy of the Pixmap
392 unsigned width
, height
, border
, depth
;
394 XGetGeometry( xdisplay
, (Drawable
)xpixmap
, &root
, &x
, &y
,
395 &width
, &height
, &border
, &depth
);
396 copy
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
398 GC gc
= XCreateGC( xdisplay
, copy
, 0, NULL
);
399 XCopyArea( xdisplay
, xpixmap
, copy
, gc
, 0, 0, width
, height
, 0, 0 );
400 XFreeGC( xdisplay
, gc
);
403 wxBitmapRefData
* ref
= new wxBitmapRefData();
406 ref
->m_bitmap
= (WXPixmap
)copy
;
408 ref
->m_pixmap
= (WXPixmap
)copy
;
410 ref
->m_display
= (WXDisplay
*)xdisplay
;
411 ref
->m_width
= width
;
412 ref
->m_height
= height
;
420 wxBitmap::wxBitmap(const char* const* bits
)
422 Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
425 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
430 wxASSERT_MSG(image
.Ok(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
434 int w
= image
.GetWidth();
435 int h
= image
.GetHeight();
437 if (!Create(w
, h
, depth
))
440 // Unfortunately the mask has to be screen-depth since
441 // 1-bpp bitmaps don't seem to be supported
442 // TODO: implement transparent drawing, presumably
443 // by doing several blits as per the Windows
444 // implementation because Nano-X doesn't support
446 // TODO: could perhaps speed this function up
447 // by making a buffer of pixel values,
448 // and then calling GrArea to write that to the
449 // pixmap. See demos/nxroach.c.
451 bool hasMask
= image
.HasMask();
453 GC pixmapGC
= GrNewGC();
454 Pixmap pixmap
= (Pixmap
) GetPixmap();
457 Pixmap maskPixmap
= 0;
459 unsigned char maskR
= 0;
460 unsigned char maskG
= 0;
461 unsigned char maskB
= 0;
465 maskR
= image
.GetMaskRed();
466 maskG
= image
.GetMaskGreen();
467 maskB
= image
.GetMaskBlue();
470 maskPixmap
= GrNewPixmap(w
, h
, 0);
475 wxMask
* mask
= new wxMask
;
476 mask
->SetBitmap((WXPixmap
) maskPixmap
);
481 GR_COLOR lastPixmapColour
= 0;
482 GR_COLOR lastMaskColour
= 0;
485 for (i
= 0; i
< w
; i
++)
487 for (j
= 0; j
< h
; j
++)
489 unsigned char red
= image
.GetRed(i
, j
);
490 unsigned char green
= image
.GetGreen(i
, j
);
491 unsigned char blue
= image
.GetBlue(i
, j
);
493 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
495 // Efficiency measure
496 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
498 GrSetGCForeground(pixmapGC
, colour
);
499 lastPixmapColour
= colour
;
502 GrPoint(pixmap
, pixmapGC
, i
, j
);
506 // scan the bitmap for the transparent colour and set the corresponding
507 // pixels in the mask to BLACK and the rest to WHITE
508 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
510 colour
= GR_RGB(0, 0, 0);
514 colour
= GR_RGB(255, 255, 255);
516 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
518 GrSetGCForeground(maskGC
, colour
);
519 lastMaskColour
= colour
;
521 GrPoint(maskPixmap
, maskGC
, i
, j
);
526 GrDestroyGC(pixmapGC
);
536 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
537 wxCHECK_MSG( depth
== -1, false, wxT("invalid bitmap depth") );
539 m_refData
= new wxBitmapRefData();
541 M_BMPDATA
->m_display
= wxGlobalDisplay();
543 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
545 int xscreen
= DefaultScreen( xdisplay
);
546 Window xroot
= RootWindow( xdisplay
, xscreen
);
547 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
549 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
551 int width
= image
.GetWidth();
552 int height
= image
.GetHeight();
553 M_BMPDATA
->m_width
= width
;
554 M_BMPDATA
->m_height
= height
;
556 if (depth
!= 1) depth
= bpp
;
557 M_BMPDATA
->m_bpp
= depth
;
561 wxFAIL_MSG( wxT("mono images later") );
567 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
568 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
570 if (data_image
->data
== NULL
)
572 wxLogError( wxT("Out of memory.") ); // TODO clean
576 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
580 XImage
*mask_image
= (XImage
*) NULL
;
583 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
584 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
586 if (mask_image
->data
== NULL
)
588 wxLogError( wxT("Out of memory.") ); // TODO clean
592 wxMask
*mask
= new wxMask();
593 mask
->SetDisplay( xdisplay
);
594 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
599 if (bpp
< 8) bpp
= 8;
603 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
604 byte_order b_o
= RGB
;
606 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
607 unsigned long greenMask
= vi
->m_visualGreenMask
,
608 redMask
= vi
->m_visualRedMask
,
609 blueMask
= vi
->m_visualBlueMask
;
613 if ((redMask
> greenMask
) && (greenMask
> blueMask
)) b_o
= RGB
;
614 else if ((redMask
> blueMask
) && (blueMask
> greenMask
)) b_o
= RBG
;
615 else if ((blueMask
> redMask
) && (redMask
> greenMask
)) b_o
= BRG
;
616 else if ((blueMask
> greenMask
) && (greenMask
> redMask
))b_o
= BGR
;
617 else if ((greenMask
> redMask
) && (redMask
> blueMask
)) b_o
= GRB
;
618 else if ((greenMask
> blueMask
) && (blueMask
> redMask
)) b_o
= GBR
;
621 int r_mask
= image
.GetMaskRed();
622 int g_mask
= image
.GetMaskGreen();
623 int b_mask
= image
.GetMaskBlue();
625 unsigned char* data
= image
.GetData();
626 wxASSERT_MSG( data
, wxT("No image data") );
628 unsigned char *colorCube
=
629 wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_colorCube
;
631 bool hasMask
= image
.HasMask();
634 for (int y
= 0; y
< height
; y
++)
636 for (int x
= 0; x
< width
; x
++)
647 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
648 XPutPixel( mask_image
, x
, y
, 0 );
650 XPutPixel( mask_image
, x
, y
, 1 );
658 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
659 XPutPixel( data_image
, x
, y
, pixel
);
667 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
668 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
669 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
670 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
671 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
672 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
674 XPutPixel( data_image
, x
, y
, pixel
);
682 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
683 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
684 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
685 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
686 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
687 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
689 XPutPixel( data_image
, x
, y
, pixel
);
694 // I actually don't know if for 16-bit displays, it is alway the green
695 // component or the second component which has 6 bits.
699 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
700 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
701 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
702 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
703 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
704 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
706 XPutPixel( data_image
, x
, y
, pixel
);
715 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
716 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
717 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
718 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
719 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
720 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
722 XPutPixel( data_image
, x
, y
, pixel
);
731 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
732 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
733 XDestroyImage( data_image
);
734 XFreeGC( xdisplay
, gc
);
740 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
741 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
743 XDestroyImage( mask_image
);
744 XFreeGC( xdisplay
, gc
);
753 wxImage
wxBitmap::ConvertToImage() const
757 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
759 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
760 wxASSERT_MSG( xdisplay
, wxT("No display") );
763 //int bpp = DefaultDepth(xdisplay, xscreen);
764 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
768 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
769 XImage
*x_image
= NULL
;
772 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
774 GetWidth(), GetHeight(),
775 AllPlanes
, ZPixmap
);
779 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
781 GetWidth(), GetHeight(),
782 AllPlanes
, ZPixmap
);
785 wxFAIL_MSG( wxT("Ill-formed bitmap") );
788 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
790 image
.Create( GetWidth(), GetHeight() );
791 char unsigned *data
= image
.GetData();
795 XDestroyImage( x_image
);
796 wxFAIL_MSG( wxT("couldn't create image") );
800 XImage
*x_image_mask
= NULL
;
803 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
805 GetWidth(), GetHeight(),
806 AllPlanes
, ZPixmap
);
808 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
811 int red_shift_right
= 0;
812 int green_shift_right
= 0;
813 int blue_shift_right
= 0;
814 int red_shift_left
= 0;
815 int green_shift_left
= 0;
816 int blue_shift_left
= 0;
817 bool use_shift
= false;
821 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
823 red_shift_right
= vi
->m_visualRedShift
;
824 red_shift_left
= 8 - vi
->m_visualRedPrec
;
825 green_shift_right
= vi
->m_visualGreenShift
;
826 green_shift_left
= 8 - vi
->m_visualGreenPrec
;
827 blue_shift_right
= vi
->m_visualBlueShift
;
828 blue_shift_left
= 8 - vi
->m_visualBluePrec
;
830 use_shift
= (vi
->m_visualType
== GrayScale
) ||
831 (vi
->m_visualType
!= PseudoColor
);
839 XColor
*colors
= (XColor
*)wxTheApp
->
840 GetVisualInfo(M_BMPDATA
->m_display
)->m_visualColormap
;
842 int width
= GetWidth();
843 int height
= GetHeight();
845 for (int j
= 0; j
< height
; j
++)
847 for (int i
= 0; i
< width
; i
++)
849 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
867 data
[pos
] = (unsigned char)((pixel
>> red_shift_right
) << red_shift_left
);
868 data
[pos
+1] = (unsigned char)((pixel
>> green_shift_right
) << green_shift_left
);
869 data
[pos
+2] = (unsigned char)((pixel
>> blue_shift_right
) << blue_shift_left
);
873 data
[pos
] = (unsigned char)(colors
[pixel
].red
>> 8);
874 data
[pos
+1] = (unsigned char)(colors
[pixel
].green
>> 8);
875 data
[pos
+2] = (unsigned char)(colors
[pixel
].blue
>> 8);
879 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
884 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
897 XDestroyImage( x_image
);
898 if (x_image_mask
) XDestroyImage( x_image_mask
);
904 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
906 LoadFile( filename
, type
);
909 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
911 m_refData
= new wxBitmapRefData
;
913 (void) Create(bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
916 wxBitmap::~wxBitmap()
920 bool wxBitmap::IsOk() const
922 return (m_refData
!= NULL
);
925 int wxBitmap::GetHeight() const
927 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
929 return M_BMPDATA
->m_height
;
932 int wxBitmap::GetWidth() const
934 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
936 return M_BMPDATA
->m_width
;
939 int wxBitmap::GetDepth() const
941 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
943 return M_BMPDATA
->m_bpp
;
946 wxMask
*wxBitmap::GetMask() const
948 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
950 return M_BMPDATA
->m_mask
;
953 void wxBitmap::SetMask( wxMask
*mask
)
955 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
958 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
960 M_BMPDATA
->m_mask
= mask
;
963 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
969 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
972 (rect
.x
>= 0) && (rect
.y
>= 0) &&
973 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
974 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
975 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
977 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
978 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
982 wxMask
* mask
= new wxMask();
983 mask
->SetDisplay( GetMask()->GetDisplay() );
984 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
985 GetMask()->GetBitmap(),
987 rect
.width
, rect
.height
,
995 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
998 rect
.width
, rect
.height
,
999 M_BMPDATA
->m_bpp
) );
1004 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1007 rect
.width
, rect
.height
,
1014 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1015 const wxPalette
*palette
) const
1017 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1019 wxBitmapHandler
*handler
= FindHandler(type
);
1021 // Try to save the bitmap via wxImage handlers:
1022 if (handler
== NULL
)
1024 wxImage
image(this->ConvertToImage());
1025 if (image
.Ok()) return image
.SaveFile( name
, type
);
1030 return handler
->SaveFile(this, name
, type
, palette
);
1033 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1037 if (!wxFileExists(name
)) return false;
1039 wxBitmapHandler
*handler
= FindHandler(type
);
1041 if (handler
== NULL
)
1044 if (!image
.LoadFile( name
, type
))
1049 *this = wxBitmap(image
);
1055 return handler
->LoadFile(this, name
, type
, -1, -1);
1058 void wxBitmap::SetPalette(const wxPalette
& palette
)
1060 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1061 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1062 wxT("cannot set palette for bitmap of this depth"));
1065 delete M_BMPDATA
->m_palette
;
1066 M_BMPDATA
->m_palette
= NULL
;
1068 if (!palette
.Ok()) return;
1070 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1073 wxPalette
*wxBitmap::GetPalette() const
1075 if (!Ok()) return (wxPalette
*) NULL
;
1077 return M_BMPDATA
->m_palette
;
1080 void wxBitmap::SetHeight( int height
)
1084 M_BMPDATA
->m_height
= height
;
1087 void wxBitmap::SetWidth( int width
)
1091 M_BMPDATA
->m_width
= width
;
1094 void wxBitmap::SetDepth( int depth
)
1098 M_BMPDATA
->m_bpp
= depth
;
1101 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1103 if (!m_refData
) m_refData
= new wxBitmapRefData();
1105 M_BMPDATA
->m_pixmap
= pixmap
;
1108 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1110 if (!m_refData
) m_refData
= new wxBitmapRefData();
1112 M_BMPDATA
->m_bitmap
= bitmap
;
1115 WXPixmap
wxBitmap::GetPixmap() const
1117 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1119 return M_BMPDATA
->m_pixmap
;
1122 WXPixmap
wxBitmap::GetBitmap() const
1124 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1126 return M_BMPDATA
->m_bitmap
;
1129 WXPixmap
wxBitmap::GetDrawable() const
1131 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1133 return M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
: M_BMPDATA
->m_pixmap
;
1136 WXDisplay
*wxBitmap::GetDisplay() const
1138 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1140 return M_BMPDATA
->m_display
;
1144 // Copy from the drawable to the wxImage
1145 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1147 GR_SCREEN_INFO sinfo
;
1149 GR_PIXELVAL
*pixels
;
1150 GR_PALETTE
* palette
= NULL
;
1151 unsigned char rgb
[3], *pp
;
1153 GrGetScreenInfo(&sinfo
);
1155 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1156 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1159 GrGetSystemPalette(palette
);
1162 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1167 image
.Create(width
, height
);
1169 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1173 for(x
= 0; x
< sinfo
.cols
; x
++) {
1175 pp
= (unsigned char *)pixels
+
1176 ((x
+ (y
* sinfo
.cols
)) *
1177 sizeof(GR_PIXELVAL
));
1179 switch(sinfo
.pixtype
) {
1180 /* FIXME: These may need modifying on big endian. */
1181 case MWPF_TRUECOLOR0888
:
1182 case MWPF_TRUECOLOR888
:
1188 rgb
[0] = palette
->palette
[pp
[0]].r
;
1189 rgb
[1] = palette
->palette
[pp
[0]].g
;
1190 rgb
[2] = palette
->palette
[pp
[0]].b
;
1192 case MWPF_TRUECOLOR565
:
1193 rgb
[0] = pp
[1] & 0xf8;
1194 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1195 ((pp
[0] & 0xe0) >> 3);
1196 rgb
[2] = (pp
[0] & 0x1f) << 3;
1198 case MWPF_TRUECOLOR555
:
1199 rgb
[0] = (pp
[1] & 0x7c) << 1;
1200 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1201 ((pp
[0] & 0xe0) >> 2);
1202 rgb
[2] = (pp
[0] & 0x1f) << 3;
1204 case MWPF_TRUECOLOR332
:
1205 rgb
[0] = pp
[0] & 0xe0;
1206 rgb
[1] = (pp
[0] & 0x1c) << 3;
1207 rgb
[2] = (pp
[0] & 0x03) << 6;
1210 fprintf(stderr
, "Unsupported pixel "
1215 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1220 if(palette
) free(palette
);
1226 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1227 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1229 unsigned char rgb
[3], *pp
;
1231 pp
= (unsigned char*) & pixel
;
1233 switch (sinfo
.pixtype
)
1235 /* FIXME: These may need modifying on big endian. */
1236 case MWPF_TRUECOLOR0888
:
1237 case MWPF_TRUECOLOR888
:
1243 rgb
[0] = palette
->palette
[pp
[0]].r
;
1244 rgb
[1] = palette
->palette
[pp
[0]].g
;
1245 rgb
[2] = palette
->palette
[pp
[0]].b
;
1247 case MWPF_TRUECOLOR565
:
1248 rgb
[0] = pp
[1] & 0xf8;
1249 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1250 ((pp
[0] & 0xe0) >> 3);
1251 rgb
[2] = (pp
[0] & 0x1f) << 3;
1253 case MWPF_TRUECOLOR555
:
1254 rgb
[0] = (pp
[1] & 0x7c) << 1;
1255 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1256 ((pp
[0] & 0xe0) >> 2);
1257 rgb
[2] = (pp
[0] & 0x1f) << 3;
1259 case MWPF_TRUECOLOR332
:
1260 rgb
[0] = pp
[0] & 0xe0;
1261 rgb
[1] = (pp
[0] & 0x1c) << 3;
1262 rgb
[2] = (pp
[0] & 0x03) << 6;
1265 fprintf(stderr
, "Unsupported pixel format\n");
1280 // ============================================================================
1282 // ============================================================================
1284 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1286 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1290 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1292 // ----------------------------------------------------------------------------
1294 // ----------------------------------------------------------------------------
1296 class wxXPMFileHandler
: public wxBitmapHandler
1298 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1302 SetName( wxT("XPM file") );
1303 SetExtension( wxT("xpm") );
1304 SetType( wxBITMAP_TYPE_XPM
);
1307 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1308 int desiredWidth
, int desiredHeight
);
1310 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1311 int type
, const wxPalette
*palette
= NULL
);
1313 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
), const void* WXUNUSED(data
), long WXUNUSED(flags
),
1314 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
) = 1)
1318 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1320 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1321 long WXUNUSED(flags
), int WXUNUSED(desiredWidth
),
1322 int WXUNUSED(desiredHeight
))
1325 if (!bitmap
->GetRefData())
1326 bitmap
->SetRefData( new wxBitmapRefData() );
1328 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1330 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1332 int xscreen
= DefaultScreen( xdisplay
);
1333 Window xroot
= RootWindow( xdisplay
, xscreen
);
1335 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1337 XpmAttributes xpmAttr
;
1338 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1343 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1344 (char*) name
.c_str(),
1345 &pixmap
, &mask
, &xpmAttr
);
1347 if (ErrorStatus
== XpmSuccess
)
1349 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1350 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1352 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1354 XpmFreeAttributes(&xpmAttr
);
1356 M_BMPHANDLERDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1360 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1361 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1362 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1374 wxXPMDecoder decoder
;
1375 wxFileInputStream
stream(name
);
1378 wxImage
image(decoder
.ReadFile(stream
));
1379 return image
.Ok() && bitmap
->CreateFromImage(image
);
1383 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1385 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1388 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1390 const wxPalette
*WXUNUSED(palette
))
1392 wxImage
image(bitmap
->ConvertToImage());
1393 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1398 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1400 // ----------------------------------------------------------------------------
1402 // ----------------------------------------------------------------------------
1404 class wxXPMDataHandler
: public wxBitmapHandler
1406 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1410 SetName( wxT("XPM data") );
1411 SetExtension( wxT("xpm") );
1412 SetType( wxBITMAP_TYPE_XPM_DATA
);
1415 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1416 const wxString
& WXUNUSED(name
),
1417 long WXUNUSED(flags
),
1418 int WXUNUSED(desiredWidth
),
1419 int WXUNUSED(desiredHeight
))
1422 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1423 const wxString
& WXUNUSED(name
),
1425 const wxPalette
*WXUNUSED(palette
) = NULL
)
1428 virtual bool Create(wxBitmap
*bitmap
, const void* data
, long flags
,
1429 int width
, int height
, int depth
= 1);
1432 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1434 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, const void* bits
,
1435 long WXUNUSED(flags
),
1436 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
))
1439 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
1441 if (!bitmap
->GetRefData())
1442 bitmap
->SetRefData( new wxBitmapRefData() );
1444 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1446 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1448 int xscreen
= DefaultScreen( xdisplay
);
1449 Window xroot
= RootWindow( xdisplay
, xscreen
);
1451 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1453 XpmAttributes xpmAttr
;
1454 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1459 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1460 &pixmap
, &mask
, &xpmAttr
);
1462 if (ErrorStatus
== XpmSuccess
)
1464 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1465 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1467 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1470 unsigned int depthRet
;
1472 unsigned int widthRet
, heightRet
, borderWidthRet
;
1473 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1474 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1476 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1479 XpmFreeAttributes(&xpmAttr
);
1481 M_BMPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
1485 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1486 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1487 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1497 #else // !wxHAVE_LIB_XPM
1498 wxXPMDecoder decoder
;
1499 wxImage
image(decoder
.ReadData((const char **)bits
));
1500 return image
.Ok() && bitmap
->CreateFromImage(image
);
1501 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1506 // ----------------------------------------------------------------------------
1508 // ----------------------------------------------------------------------------
1510 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1512 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1514 inline wxXBMDataHandler()
1516 SetName( wxT("XBM data") );
1517 SetExtension( wxT("xbm") );
1518 SetType( wxBITMAP_TYPE_XBM_DATA
);
1521 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1522 const wxString
& WXUNUSED(name
),
1523 long WXUNUSED(flags
),
1524 int WXUNUSED(desiredWidth
),
1525 int WXUNUSED(desiredHeight
))
1528 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1529 const wxString
& WXUNUSED(name
),
1531 const wxPalette
*WXUNUSED(palette
) = NULL
)
1534 virtual bool Create(wxBitmap
*bitmap
, const void* data
, long flags
,
1535 int width
, int height
, int depth
= 1);
1538 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
1540 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, const void* bits
,
1541 long WXUNUSED(flags
),
1542 int width
, int height
, int WXUNUSED(depth
))
1545 if (!bitmap
->GetRefData())
1546 bitmap
->SetRefData( new wxBitmapRefData() );
1548 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1550 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1552 int xscreen
= DefaultScreen( xdisplay
);
1553 Window xroot
= RootWindow( xdisplay
, xscreen
);
1555 M_BMPHANDLERDATA
->m_mask
= (wxMask
*) NULL
;
1556 M_BMPHANDLERDATA
->m_bitmap
=
1557 (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
,
1558 (char *) bits
, width
, height
);
1559 M_BMPHANDLERDATA
->m_width
= width
;
1560 M_BMPHANDLERDATA
->m_height
= height
;
1561 M_BMPHANDLERDATA
->m_bpp
= 1;
1565 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, false,
1566 wxT("couldn't create bitmap") );
1570 void wxBitmap::InitStandardHandlers()
1572 AddHandler(new wxXBMDataHandler
);
1574 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1575 AddHandler(new wxXPMFileHandler
);
1577 AddHandler(new wxXPMDataHandler
);