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
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(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 bool wxBitmap::CreateFromXpm( const char **bits
)
422 wxCHECK_MSG( bits
, false, wxT("NULL pointer in wxBitmap::CreateFromXpm") );
424 return Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
427 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
432 wxASSERT_MSG(image
.Ok(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
436 int w
= image
.GetWidth();
437 int h
= image
.GetHeight();
439 if (!Create(w
, h
, depth
))
442 // Unfortunately the mask has to be screen-depth since
443 // 1-bpp bitmaps don't seem to be supported
444 // TODO: implement transparent drawing, presumably
445 // by doing several blits as per the Windows
446 // implementation because Nano-X doesn't support
448 // TODO: could perhaps speed this function up
449 // by making a buffer of pixel values,
450 // and then calling GrArea to write that to the
451 // pixmap. See demos/nxroach.c.
453 bool hasMask
= image
.HasMask();
455 GC pixmapGC
= GrNewGC();
456 Pixmap pixmap
= (Pixmap
) GetPixmap();
459 Pixmap maskPixmap
= 0;
461 unsigned char maskR
= 0;
462 unsigned char maskG
= 0;
463 unsigned char maskB
= 0;
467 maskR
= image
.GetMaskRed();
468 maskG
= image
.GetMaskGreen();
469 maskB
= image
.GetMaskBlue();
472 maskPixmap
= GrNewPixmap(w
, h
, 0);
477 wxMask
* mask
= new wxMask
;
478 mask
->SetBitmap((WXPixmap
) maskPixmap
);
483 GR_COLOR lastPixmapColour
= 0;
484 GR_COLOR lastMaskColour
= 0;
487 for (i
= 0; i
< w
; i
++)
489 for (j
= 0; j
< h
; j
++)
491 unsigned char red
= image
.GetRed(i
, j
);
492 unsigned char green
= image
.GetGreen(i
, j
);
493 unsigned char blue
= image
.GetBlue(i
, j
);
495 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
497 // Efficiency measure
498 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
500 GrSetGCForeground(pixmapGC
, colour
);
501 lastPixmapColour
= colour
;
504 GrPoint(pixmap
, pixmapGC
, i
, j
);
508 // scan the bitmap for the transparent colour and set the corresponding
509 // pixels in the mask to BLACK and the rest to WHITE
510 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
512 colour
= GR_RGB(0, 0, 0);
516 colour
= GR_RGB(255, 255, 255);
518 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
520 GrSetGCForeground(maskGC
, colour
);
521 lastMaskColour
= colour
;
523 GrPoint(maskPixmap
, maskGC
, i
, j
);
528 GrDestroyGC(pixmapGC
);
538 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
539 wxCHECK_MSG( depth
== -1, false, wxT("invalid bitmap depth") );
541 m_refData
= new wxBitmapRefData();
543 M_BMPDATA
->m_display
= wxGlobalDisplay();
545 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
547 int xscreen
= DefaultScreen( xdisplay
);
548 Window xroot
= RootWindow( xdisplay
, xscreen
);
549 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
551 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
553 int width
= image
.GetWidth();
554 int height
= image
.GetHeight();
555 M_BMPDATA
->m_width
= width
;
556 M_BMPDATA
->m_height
= height
;
558 if (depth
!= 1) depth
= bpp
;
559 M_BMPDATA
->m_bpp
= depth
;
563 wxFAIL_MSG( wxT("mono images later") );
569 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
570 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
572 if (data_image
->data
== NULL
)
574 wxLogError( wxT("Out of memory.") ); // TODO clean
578 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
582 XImage
*mask_image
= (XImage
*) NULL
;
585 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
586 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
588 if (mask_image
->data
== NULL
)
590 wxLogError( wxT("Out of memory.") ); // TODO clean
594 wxMask
*mask
= new wxMask();
595 mask
->SetDisplay( xdisplay
);
596 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
601 if (bpp
< 8) bpp
= 8;
605 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
606 byte_order b_o
= RGB
;
608 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
609 unsigned long greenMask
= vi
->m_visualGreenMask
,
610 redMask
= vi
->m_visualRedMask
,
611 blueMask
= vi
->m_visualBlueMask
;
615 if ((redMask
> greenMask
) && (greenMask
> blueMask
)) b_o
= RGB
;
616 else if ((redMask
> blueMask
) && (blueMask
> greenMask
)) b_o
= RBG
;
617 else if ((blueMask
> redMask
) && (redMask
> greenMask
)) b_o
= BRG
;
618 else if ((blueMask
> greenMask
) && (greenMask
> redMask
))b_o
= BGR
;
619 else if ((greenMask
> redMask
) && (redMask
> blueMask
)) b_o
= GRB
;
620 else if ((greenMask
> blueMask
) && (blueMask
> redMask
)) b_o
= GBR
;
623 int r_mask
= image
.GetMaskRed();
624 int g_mask
= image
.GetMaskGreen();
625 int b_mask
= image
.GetMaskBlue();
627 unsigned char* data
= image
.GetData();
628 wxASSERT_MSG( data
, wxT("No image data") );
630 unsigned char *colorCube
=
631 wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_colorCube
;
633 bool hasMask
= image
.HasMask();
636 for (int y
= 0; y
< height
; y
++)
638 for (int x
= 0; x
< width
; x
++)
649 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
650 XPutPixel( mask_image
, x
, y
, 0 );
652 XPutPixel( mask_image
, x
, y
, 1 );
660 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
661 XPutPixel( data_image
, x
, y
, pixel
);
669 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
670 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
671 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
672 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
673 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
674 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
676 XPutPixel( data_image
, x
, y
, pixel
);
684 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
685 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
686 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
687 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
688 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
689 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
691 XPutPixel( data_image
, x
, y
, pixel
);
696 // I actually don't know if for 16-bit displays, it is alway the green
697 // component or the second component which has 6 bits.
701 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
702 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
703 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
704 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
705 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
706 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
708 XPutPixel( data_image
, x
, y
, pixel
);
717 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
718 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
719 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
720 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
721 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
722 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
724 XPutPixel( data_image
, x
, y
, pixel
);
733 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
734 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
735 XDestroyImage( data_image
);
736 XFreeGC( xdisplay
, gc
);
742 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
743 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
745 XDestroyImage( mask_image
);
746 XFreeGC( xdisplay
, gc
);
755 wxImage
wxBitmap::ConvertToImage() const
759 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
761 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
762 wxASSERT_MSG( xdisplay
, wxT("No display") );
765 //int bpp = DefaultDepth(xdisplay, xscreen);
766 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
770 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
771 XImage
*x_image
= NULL
;
774 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
776 GetWidth(), GetHeight(),
777 AllPlanes
, ZPixmap
);
781 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
783 GetWidth(), GetHeight(),
784 AllPlanes
, ZPixmap
);
787 wxFAIL_MSG( wxT("Ill-formed bitmap") );
790 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
792 image
.Create( GetWidth(), GetHeight() );
793 char unsigned *data
= image
.GetData();
797 XDestroyImage( x_image
);
798 wxFAIL_MSG( wxT("couldn't create image") );
802 XImage
*x_image_mask
= NULL
;
805 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
807 GetWidth(), GetHeight(),
808 AllPlanes
, ZPixmap
);
810 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
813 int red_shift_right
= 0;
814 int green_shift_right
= 0;
815 int blue_shift_right
= 0;
816 int red_shift_left
= 0;
817 int green_shift_left
= 0;
818 int blue_shift_left
= 0;
819 bool use_shift
= false;
823 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
825 red_shift_right
= vi
->m_visualRedShift
;
826 red_shift_left
= 8 - vi
->m_visualRedPrec
;
827 green_shift_right
= vi
->m_visualGreenShift
;
828 green_shift_left
= 8 - vi
->m_visualGreenPrec
;
829 blue_shift_right
= vi
->m_visualBlueShift
;
830 blue_shift_left
= 8 - vi
->m_visualBluePrec
;
832 use_shift
= (vi
->m_visualType
== GrayScale
) ||
833 (vi
->m_visualType
!= PseudoColor
);
841 XColor
*colors
= (XColor
*)wxTheApp
->
842 GetVisualInfo(M_BMPDATA
->m_display
)->m_visualColormap
;
844 int width
= GetWidth();
845 int height
= GetHeight();
847 for (int j
= 0; j
< height
; j
++)
849 for (int i
= 0; i
< width
; i
++)
851 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
869 data
[pos
] = (unsigned char)((pixel
>> red_shift_right
) << red_shift_left
);
870 data
[pos
+1] = (unsigned char)((pixel
>> green_shift_right
) << green_shift_left
);
871 data
[pos
+2] = (unsigned char)((pixel
>> blue_shift_right
) << blue_shift_left
);
875 data
[pos
] = (unsigned char)(colors
[pixel
].red
>> 8);
876 data
[pos
+1] = (unsigned char)(colors
[pixel
].green
>> 8);
877 data
[pos
+2] = (unsigned char)(colors
[pixel
].blue
>> 8);
881 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
886 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
899 XDestroyImage( x_image
);
900 if (x_image_mask
) XDestroyImage( x_image_mask
);
906 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
908 LoadFile( filename
, type
);
911 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
913 m_refData
= new wxBitmapRefData
;
915 (void) Create((void*) bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
918 wxBitmap::~wxBitmap()
922 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
924 return m_refData
== bmp
.m_refData
;
927 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
929 return m_refData
!= bmp
.m_refData
;
932 bool wxBitmap::Ok() const
934 return (m_refData
!= NULL
);
937 int wxBitmap::GetHeight() const
939 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
941 return M_BMPDATA
->m_height
;
944 int wxBitmap::GetWidth() const
946 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
948 return M_BMPDATA
->m_width
;
951 int wxBitmap::GetDepth() const
953 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
955 return M_BMPDATA
->m_bpp
;
958 wxMask
*wxBitmap::GetMask() const
960 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
962 return M_BMPDATA
->m_mask
;
965 void wxBitmap::SetMask( wxMask
*mask
)
967 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
969 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
971 M_BMPDATA
->m_mask
= mask
;
974 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
980 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
983 (rect
.x
>= 0) && (rect
.y
>= 0) &&
984 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
985 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
986 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
988 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
989 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
993 wxMask
* mask
= new wxMask();
994 mask
->SetDisplay( GetMask()->GetDisplay() );
995 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
996 GetMask()->GetBitmap(),
998 rect
.width
, rect
.height
,
1001 ret
.SetMask( mask
);
1006 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
1009 rect
.width
, rect
.height
,
1010 M_BMPDATA
->m_bpp
) );
1015 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1018 rect
.width
, rect
.height
,
1025 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1026 const wxPalette
*palette
) const
1028 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1030 wxBitmapHandler
*handler
= FindHandler(type
);
1032 // Try to save the bitmap via wxImage handlers:
1033 if (handler
== NULL
)
1035 wxImage
image(this->ConvertToImage());
1036 if (image
.Ok()) return image
.SaveFile( name
, type
);
1041 return handler
->SaveFile(this, name
, type
, palette
);
1044 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1048 if (!wxFileExists(name
)) return false;
1050 wxBitmapHandler
*handler
= FindHandler(type
);
1052 if (handler
== NULL
)
1055 if (!image
.LoadFile( name
, type
))
1060 *this = wxBitmap(image
);
1066 return handler
->LoadFile(this, name
, type
, -1, -1);
1069 void wxBitmap::SetPalette(const wxPalette
& palette
)
1071 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1072 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1073 wxT("cannot set palette for bitmap of this depth"));
1075 delete M_BMPDATA
->m_palette
;
1076 M_BMPDATA
->m_palette
= NULL
;
1078 if (!palette
.Ok()) return;
1080 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1083 wxPalette
*wxBitmap::GetPalette() const
1085 if (!Ok()) return (wxPalette
*) NULL
;
1087 return M_BMPDATA
->m_palette
;
1090 void wxBitmap::SetHeight( int height
)
1092 if (!m_refData
) m_refData
= new wxBitmapRefData();
1094 M_BMPDATA
->m_height
= height
;
1097 void wxBitmap::SetWidth( int width
)
1099 if (!m_refData
) m_refData
= new wxBitmapRefData();
1101 M_BMPDATA
->m_width
= width
;
1104 void wxBitmap::SetDepth( int depth
)
1106 if (!m_refData
) m_refData
= new wxBitmapRefData();
1108 M_BMPDATA
->m_bpp
= depth
;
1111 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1113 if (!m_refData
) m_refData
= new wxBitmapRefData();
1115 M_BMPDATA
->m_pixmap
= pixmap
;
1118 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1120 if (!m_refData
) m_refData
= new wxBitmapRefData();
1122 M_BMPDATA
->m_bitmap
= bitmap
;
1125 WXPixmap
wxBitmap::GetPixmap() const
1127 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1129 return M_BMPDATA
->m_pixmap
;
1132 WXPixmap
wxBitmap::GetBitmap() const
1134 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1136 return M_BMPDATA
->m_bitmap
;
1139 WXPixmap
wxBitmap::GetDrawable() const
1141 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1143 return M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
: M_BMPDATA
->m_pixmap
;
1146 WXDisplay
*wxBitmap::GetDisplay() const
1148 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1150 return M_BMPDATA
->m_display
;
1154 // Copy from the drawable to the wxImage
1155 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1157 GR_SCREEN_INFO sinfo
;
1159 GR_PIXELVAL
*pixels
;
1160 GR_PALETTE
* palette
= NULL
;
1161 unsigned char rgb
[3], *pp
;
1163 GrGetScreenInfo(&sinfo
);
1165 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1166 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1169 GrGetSystemPalette(palette
);
1172 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1177 image
.Create(width
, height
);
1179 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1183 for(x
= 0; x
< sinfo
.cols
; x
++) {
1185 pp
= (unsigned char *)pixels
+
1186 ((x
+ (y
* sinfo
.cols
)) *
1187 sizeof(GR_PIXELVAL
));
1189 switch(sinfo
.pixtype
) {
1190 /* FIXME: These may need modifying on big endian. */
1191 case MWPF_TRUECOLOR0888
:
1192 case MWPF_TRUECOLOR888
:
1198 rgb
[0] = palette
->palette
[pp
[0]].r
;
1199 rgb
[1] = palette
->palette
[pp
[0]].g
;
1200 rgb
[2] = palette
->palette
[pp
[0]].b
;
1202 case MWPF_TRUECOLOR565
:
1203 rgb
[0] = pp
[1] & 0xf8;
1204 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1205 ((pp
[0] & 0xe0) >> 3);
1206 rgb
[2] = (pp
[0] & 0x1f) << 3;
1208 case MWPF_TRUECOLOR555
:
1209 rgb
[0] = (pp
[1] & 0x7c) << 1;
1210 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1211 ((pp
[0] & 0xe0) >> 2);
1212 rgb
[2] = (pp
[0] & 0x1f) << 3;
1214 case MWPF_TRUECOLOR332
:
1215 rgb
[0] = pp
[0] & 0xe0;
1216 rgb
[1] = (pp
[0] & 0x1c) << 3;
1217 rgb
[2] = (pp
[0] & 0x03) << 6;
1220 fprintf(stderr
, "Unsupported pixel "
1225 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1230 if(palette
) free(palette
);
1236 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1237 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1239 unsigned char rgb
[3], *pp
;
1241 pp
= (unsigned char*) & pixel
;
1243 switch (sinfo
.pixtype
)
1245 /* FIXME: These may need modifying on big endian. */
1246 case MWPF_TRUECOLOR0888
:
1247 case MWPF_TRUECOLOR888
:
1253 rgb
[0] = palette
->palette
[pp
[0]].r
;
1254 rgb
[1] = palette
->palette
[pp
[0]].g
;
1255 rgb
[2] = palette
->palette
[pp
[0]].b
;
1257 case MWPF_TRUECOLOR565
:
1258 rgb
[0] = pp
[1] & 0xf8;
1259 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1260 ((pp
[0] & 0xe0) >> 3);
1261 rgb
[2] = (pp
[0] & 0x1f) << 3;
1263 case MWPF_TRUECOLOR555
:
1264 rgb
[0] = (pp
[1] & 0x7c) << 1;
1265 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1266 ((pp
[0] & 0xe0) >> 2);
1267 rgb
[2] = (pp
[0] & 0x1f) << 3;
1269 case MWPF_TRUECOLOR332
:
1270 rgb
[0] = pp
[0] & 0xe0;
1271 rgb
[1] = (pp
[0] & 0x1c) << 3;
1272 rgb
[2] = (pp
[0] & 0x03) << 6;
1275 fprintf(stderr
, "Unsupported pixel format\n");
1290 // ============================================================================
1292 // ============================================================================
1294 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1296 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1300 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1302 // ----------------------------------------------------------------------------
1304 // ----------------------------------------------------------------------------
1306 class wxXPMFileHandler
: public wxBitmapHandler
1308 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1312 SetName( wxT("XPM file") );
1313 SetExtension( wxT("xpm") );
1314 SetType( wxBITMAP_TYPE_XPM
);
1317 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1318 int desiredWidth
, int desiredHeight
);
1320 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1321 int type
, const wxPalette
*palette
= NULL
);
1323 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
), void *WXUNUSED(data
), long WXUNUSED(flags
),
1324 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
) = 1)
1328 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1330 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1331 long WXUNUSED(flags
), int WXUNUSED(desiredWidth
),
1332 int WXUNUSED(desiredHeight
))
1335 if (!bitmap
->GetRefData())
1336 bitmap
->SetRefData( new wxBitmapRefData() );
1338 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1340 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1342 int xscreen
= DefaultScreen( xdisplay
);
1343 Window xroot
= RootWindow( xdisplay
, xscreen
);
1345 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1347 XpmAttributes xpmAttr
;
1348 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1353 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1354 (char*) name
.c_str(),
1355 &pixmap
, &mask
, &xpmAttr
);
1357 if (ErrorStatus
== XpmSuccess
)
1359 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1360 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1362 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1364 XpmFreeAttributes(&xpmAttr
);
1366 M_BMPHANDLERDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1370 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1371 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1372 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1384 wxXPMDecoder decoder
;
1385 wxFileInputStream
stream(name
);
1388 wxImage
image(decoder
.ReadFile(stream
));
1389 return image
.Ok() && bitmap
->CreateFromImage(image
);
1393 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1395 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1398 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1400 const wxPalette
*WXUNUSED(palette
))
1402 wxImage
image(bitmap
->ConvertToImage());
1403 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1408 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1410 // ----------------------------------------------------------------------------
1412 // ----------------------------------------------------------------------------
1414 class wxXPMDataHandler
: public wxBitmapHandler
1416 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1420 SetName( wxT("XPM data") );
1421 SetExtension( wxT("xpm") );
1422 SetType( wxBITMAP_TYPE_XPM_DATA
);
1425 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1426 const wxString
& WXUNUSED(name
),
1427 long WXUNUSED(flags
),
1428 int WXUNUSED(desiredWidth
),
1429 int WXUNUSED(desiredHeight
))
1432 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1433 const wxString
& WXUNUSED(name
),
1435 const wxPalette
*WXUNUSED(palette
) = NULL
)
1438 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1439 int width
, int height
, int depth
= 1);
1442 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1444 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *bits
,
1445 long WXUNUSED(flags
),
1446 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
))
1449 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
1451 if (!bitmap
->GetRefData())
1452 bitmap
->SetRefData( new wxBitmapRefData() );
1454 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1456 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1458 int xscreen
= DefaultScreen( xdisplay
);
1459 Window xroot
= RootWindow( xdisplay
, xscreen
);
1461 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1463 XpmAttributes xpmAttr
;
1464 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1469 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1470 &pixmap
, &mask
, &xpmAttr
);
1472 if (ErrorStatus
== XpmSuccess
)
1474 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1475 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1477 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1480 unsigned int depthRet
;
1482 unsigned int widthRet
, heightRet
, borderWidthRet
;
1483 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1484 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1486 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1489 XpmFreeAttributes(&xpmAttr
);
1491 M_BMPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
1495 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1496 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1497 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1507 #else // !wxHAVE_LIB_XPM
1508 wxXPMDecoder decoder
;
1509 wxImage
image(decoder
.ReadData((const char **)bits
));
1510 return image
.Ok() && bitmap
->CreateFromImage(image
);
1511 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1516 // ----------------------------------------------------------------------------
1518 // ----------------------------------------------------------------------------
1520 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1522 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1524 inline wxXBMDataHandler()
1526 SetName( wxT("XBM data") );
1527 SetExtension( wxT("xbm") );
1528 SetType( wxBITMAP_TYPE_XBM_DATA
);
1531 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1532 const wxString
& WXUNUSED(name
),
1533 long WXUNUSED(flags
),
1534 int WXUNUSED(desiredWidth
),
1535 int WXUNUSED(desiredHeight
))
1538 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1539 const wxString
& WXUNUSED(name
),
1541 const wxPalette
*WXUNUSED(palette
) = NULL
)
1544 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1545 int width
, int height
, int depth
= 1);
1548 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
1550 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, void *bits
,
1551 long WXUNUSED(flags
),
1552 int width
, int height
, int WXUNUSED(depth
))
1555 if (!bitmap
->GetRefData())
1556 bitmap
->SetRefData( new wxBitmapRefData() );
1558 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1560 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1562 int xscreen
= DefaultScreen( xdisplay
);
1563 Window xroot
= RootWindow( xdisplay
, xscreen
);
1565 M_BMPHANDLERDATA
->m_mask
= (wxMask
*) NULL
;
1566 M_BMPHANDLERDATA
->m_bitmap
=
1567 (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
,
1568 (char *) bits
, width
, height
);
1569 M_BMPHANDLERDATA
->m_width
= width
;
1570 M_BMPHANDLERDATA
->m_height
= height
;
1571 M_BMPHANDLERDATA
->m_bpp
= 1;
1575 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, false,
1576 wxT("couldn't create bitmap") );
1580 void wxBitmap::InitStandardHandlers()
1582 AddHandler(new wxXBMDataHandler
);
1584 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1585 AddHandler(new wxXPMFileHandler
);
1587 AddHandler(new wxXPMDataHandler
);