1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart, Robert Roebling
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "bitmap.h"
16 #include "wx/bitmap.h"
22 #include "wx/dcmemory.h"
25 #include "wx/x11/private.h"
27 /* No point in using libXPM for NanoX */
30 #define wxHAVE_LIB_XPM 0
32 // Copy from the drawable to the wxImage
33 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
);
40 #include "wx/xpmdecod.h"
41 #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
;
122 green
= green
& 0xf8;
128 green
= green
& 0xfc;
134 green
= green
& 0xf0;
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
] = (pixel
>> red_shift_right
) << red_shift_left
;
870 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
871 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
875 data
[pos
] = colors
[pixel
].red
>> 8;
876 data
[pos
+1] = colors
[pixel
].green
>> 8;
877 data
[pos
+2] = 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 wxBitmap
& bmp
)
911 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
913 LoadFile( filename
, type
);
916 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
918 m_refData
= new wxBitmapRefData
;
920 (void) Create((void*) bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
923 wxBitmap::~wxBitmap()
927 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
929 if ( m_refData
!= bmp
.m_refData
)
935 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
937 return m_refData
== bmp
.m_refData
;
940 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
942 return m_refData
!= bmp
.m_refData
;
945 bool wxBitmap::Ok() const
947 return (m_refData
!= NULL
);
950 int wxBitmap::GetHeight() const
952 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
954 return M_BMPDATA
->m_height
;
957 int wxBitmap::GetWidth() const
959 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
961 return M_BMPDATA
->m_width
;
964 int wxBitmap::GetDepth() const
966 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
968 return M_BMPDATA
->m_bpp
;
971 wxMask
*wxBitmap::GetMask() const
973 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
975 return M_BMPDATA
->m_mask
;
978 void wxBitmap::SetMask( wxMask
*mask
)
980 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
982 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
984 M_BMPDATA
->m_mask
= mask
;
987 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
993 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
996 (rect
.x
>= 0) && (rect
.y
>= 0) &&
997 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
998 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
999 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1001 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1002 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1006 wxMask
* mask
= new wxMask();
1007 mask
->SetDisplay( GetMask()->GetDisplay() );
1008 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
1009 GetMask()->GetBitmap(),
1011 rect
.width
, rect
.height
,
1014 ret
.SetMask( mask
);
1019 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
1022 rect
.width
, rect
.height
,
1023 M_BMPDATA
->m_bpp
) );
1028 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1031 rect
.width
, rect
.height
,
1038 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1039 const wxPalette
*palette
) const
1041 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1043 wxBitmapHandler
*handler
= FindHandler(type
);
1045 // Try to save the bitmap via wxImage handlers:
1046 if (handler
== NULL
)
1048 wxImage
image(this->ConvertToImage());
1049 if (image
.Ok()) return image
.SaveFile( name
, type
);
1054 return handler
->SaveFile(this, name
, type
, palette
);
1057 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1061 if (!wxFileExists(name
)) return FALSE
;
1063 wxBitmapHandler
*handler
= FindHandler(type
);
1065 if (handler
== NULL
)
1068 if (!image
.LoadFile( name
, type
))
1073 *this = wxBitmap(image
);
1079 return handler
->LoadFile(this, name
, type
, -1, -1);
1082 void wxBitmap::SetPalette(const wxPalette
& palette
)
1084 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1085 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1086 wxT("cannot set palette for bitmap of this depth"));
1088 delete M_BMPDATA
->m_palette
;
1089 M_BMPDATA
->m_palette
= NULL
;
1091 if (!palette
.Ok()) return;
1093 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1096 wxPalette
*wxBitmap::GetPalette() const
1098 if (!Ok()) return (wxPalette
*) NULL
;
1100 return M_BMPDATA
->m_palette
;
1103 void wxBitmap::SetHeight( int height
)
1105 if (!m_refData
) m_refData
= new wxBitmapRefData();
1107 M_BMPDATA
->m_height
= height
;
1110 void wxBitmap::SetWidth( int width
)
1112 if (!m_refData
) m_refData
= new wxBitmapRefData();
1114 M_BMPDATA
->m_width
= width
;
1117 void wxBitmap::SetDepth( int depth
)
1119 if (!m_refData
) m_refData
= new wxBitmapRefData();
1121 M_BMPDATA
->m_bpp
= depth
;
1124 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1126 if (!m_refData
) m_refData
= new wxBitmapRefData();
1128 M_BMPDATA
->m_pixmap
= pixmap
;
1131 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1133 if (!m_refData
) m_refData
= new wxBitmapRefData();
1135 M_BMPDATA
->m_bitmap
= bitmap
;
1138 WXPixmap
wxBitmap::GetPixmap() const
1140 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1142 return M_BMPDATA
->m_pixmap
;
1145 WXPixmap
wxBitmap::GetBitmap() const
1147 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1149 return M_BMPDATA
->m_bitmap
;
1152 WXPixmap
wxBitmap::GetDrawable() const
1154 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1156 return M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
: M_BMPDATA
->m_pixmap
;
1159 WXDisplay
*wxBitmap::GetDisplay() const
1161 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1163 return M_BMPDATA
->m_display
;
1167 // Copy from the drawable to the wxImage
1168 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1170 GR_SCREEN_INFO sinfo
;
1172 GR_PIXELVAL
*pixels
;
1173 GR_PALETTE
* palette
= NULL
;
1174 unsigned char rgb
[3], *pp
;
1176 GrGetScreenInfo(&sinfo
);
1178 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1179 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1182 GrGetSystemPalette(palette
);
1185 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1190 image
.Create(width
, height
);
1192 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1196 for(x
= 0; x
< sinfo
.cols
; x
++) {
1198 pp
= (unsigned char *)pixels
+
1199 ((x
+ (y
* sinfo
.cols
)) *
1200 sizeof(GR_PIXELVAL
));
1202 switch(sinfo
.pixtype
) {
1203 /* FIXME: These may need modifying on big endian. */
1204 case MWPF_TRUECOLOR0888
:
1205 case MWPF_TRUECOLOR888
:
1211 rgb
[0] = palette
->palette
[pp
[0]].r
;
1212 rgb
[1] = palette
->palette
[pp
[0]].g
;
1213 rgb
[2] = palette
->palette
[pp
[0]].b
;
1215 case MWPF_TRUECOLOR565
:
1216 rgb
[0] = pp
[1] & 0xf8;
1217 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1218 ((pp
[0] & 0xe0) >> 3);
1219 rgb
[2] = (pp
[0] & 0x1f) << 3;
1221 case MWPF_TRUECOLOR555
:
1222 rgb
[0] = (pp
[1] & 0x7c) << 1;
1223 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1224 ((pp
[0] & 0xe0) >> 2);
1225 rgb
[2] = (pp
[0] & 0x1f) << 3;
1227 case MWPF_TRUECOLOR332
:
1228 rgb
[0] = pp
[0] & 0xe0;
1229 rgb
[1] = (pp
[0] & 0x1c) << 3;
1230 rgb
[2] = (pp
[0] & 0x03) << 6;
1233 fprintf(stderr
, "Unsupported pixel "
1238 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1243 if(palette
) free(palette
);
1249 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1250 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1252 unsigned char rgb
[3], *pp
;
1254 pp
= (unsigned char*) & pixel
;
1256 switch (sinfo
.pixtype
)
1258 /* FIXME: These may need modifying on big endian. */
1259 case MWPF_TRUECOLOR0888
:
1260 case MWPF_TRUECOLOR888
:
1266 rgb
[0] = palette
->palette
[pp
[0]].r
;
1267 rgb
[1] = palette
->palette
[pp
[0]].g
;
1268 rgb
[2] = palette
->palette
[pp
[0]].b
;
1270 case MWPF_TRUECOLOR565
:
1271 rgb
[0] = pp
[1] & 0xf8;
1272 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1273 ((pp
[0] & 0xe0) >> 3);
1274 rgb
[2] = (pp
[0] & 0x1f) << 3;
1276 case MWPF_TRUECOLOR555
:
1277 rgb
[0] = (pp
[1] & 0x7c) << 1;
1278 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1279 ((pp
[0] & 0xe0) >> 2);
1280 rgb
[2] = (pp
[0] & 0x1f) << 3;
1282 case MWPF_TRUECOLOR332
:
1283 rgb
[0] = pp
[0] & 0xe0;
1284 rgb
[1] = (pp
[0] & 0x1c) << 3;
1285 rgb
[2] = (pp
[0] & 0x03) << 6;
1288 fprintf(stderr
, "Unsupported pixel format\n");
1303 // ============================================================================
1305 // ============================================================================
1307 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
);
1309 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1313 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1315 // ----------------------------------------------------------------------------
1317 // ----------------------------------------------------------------------------
1319 class wxXPMFileHandler
: public wxBitmapHandler
1321 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1325 SetName( wxT("XPM file") );
1326 SetExtension( wxT("xpm") );
1327 SetType( wxBITMAP_TYPE_XPM
);
1330 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1331 int desiredWidth
, int desiredHeight
);
1333 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1334 int type
, const wxPalette
*palette
= NULL
);
1336 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1337 int width
, int height
, int depth
= 1)
1341 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
);
1343 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1344 long flags
, int desiredWidth
,
1348 if (!bitmap
->GetRefData())
1349 bitmap
->SetRefData( new wxBitmapRefData() );
1351 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1353 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1355 int xscreen
= DefaultScreen( xdisplay
);
1356 Window xroot
= RootWindow( xdisplay
, xscreen
);
1358 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1360 XpmAttributes xpmAttr
;
1361 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1366 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1367 (char*) name
.c_str(),
1368 &pixmap
, &mask
, &xpmAttr
);
1370 if (ErrorStatus
== XpmSuccess
)
1372 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1373 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1375 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1377 XpmFreeAttributes(&xpmAttr
);
1379 M_BMPHANDLERDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1383 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1384 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1385 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1397 wxXPMDecoder decoder
;
1398 wxFileInputStream
stream(name
);
1401 wxImage
image(decoder
.ReadFile(stream
));
1402 return image
.Ok() && bitmap
->CreateFromImage(image
);
1406 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1408 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1411 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1413 const wxPalette
*WXUNUSED(palette
))
1415 wxImage
image(bitmap
->ConvertToImage());
1416 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1421 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1423 // ----------------------------------------------------------------------------
1425 // ----------------------------------------------------------------------------
1427 class wxXPMDataHandler
: public wxBitmapHandler
1429 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1433 SetName( wxT("XPM data") );
1434 SetExtension( wxT("xpm") );
1435 SetType( wxBITMAP_TYPE_XPM_DATA
);
1438 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1439 int desiredWidth
, int desiredHeight
)
1442 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1443 int type
, const wxPalette
*palette
= NULL
)
1446 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1447 int width
, int height
, int depth
= 1);
1450 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
);
1452 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *bits
,
1453 long WXUNUSED(flags
),
1454 int width
, int height
, int WXUNUSED(depth
))
1457 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
1459 if (!bitmap
->GetRefData())
1460 bitmap
->SetRefData( new wxBitmapRefData() );
1462 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1464 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1466 int xscreen
= DefaultScreen( xdisplay
);
1467 Window xroot
= RootWindow( xdisplay
, xscreen
);
1469 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1471 XpmAttributes xpmAttr
;
1472 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1477 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1478 &pixmap
, &mask
, &xpmAttr
);
1480 if (ErrorStatus
== XpmSuccess
)
1482 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1483 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1485 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1488 unsigned int depthRet
;
1490 unsigned int widthRet
, heightRet
, borderWidthRet
;
1491 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1492 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1494 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1497 XpmFreeAttributes(&xpmAttr
);
1499 M_BMPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
1503 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1504 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1505 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1515 #else // !wxHAVE_LIB_XPM
1516 wxXPMDecoder decoder
;
1517 wxImage
image(decoder
.ReadData((const char **)bits
));
1518 return image
.Ok() && bitmap
->CreateFromImage(image
);
1519 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1524 // ----------------------------------------------------------------------------
1526 // ----------------------------------------------------------------------------
1528 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1530 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1532 inline wxXBMDataHandler()
1534 SetName( wxT("XBM data") );
1535 SetExtension( wxT("xbm") );
1536 SetType( wxBITMAP_TYPE_XBM_DATA
);
1539 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1540 int desiredWidth
, int desiredHeight
)
1543 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1544 int type
, const wxPalette
*palette
= NULL
)
1547 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1548 int width
, int height
, int depth
= 1);
1551 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
);
1553 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, void *bits
,
1554 long WXUNUSED(flags
),
1555 int width
, int height
, int depth
)
1558 if (!bitmap
->GetRefData())
1559 bitmap
->SetRefData( new wxBitmapRefData() );
1561 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1563 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1565 int xscreen
= DefaultScreen( xdisplay
);
1566 Window xroot
= RootWindow( xdisplay
, xscreen
);
1568 M_BMPHANDLERDATA
->m_mask
= (wxMask
*) NULL
;
1569 M_BMPHANDLERDATA
->m_bitmap
=
1570 (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
,
1571 (char *) bits
, width
, height
);
1572 M_BMPHANDLERDATA
->m_width
= width
;
1573 M_BMPHANDLERDATA
->m_height
= height
;
1574 M_BMPHANDLERDATA
->m_bpp
= 1;
1578 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, FALSE
,
1579 wxT("couldn't create bitmap") );
1583 void wxBitmap::InitStandardHandlers()
1585 AddHandler(new wxXBMDataHandler
);
1587 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1588 AddHandler(new wxXPMFileHandler
);
1590 AddHandler(new wxXPMDataHandler
);