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 int xscreen
= DefaultScreen( (Display
*)xdisplay
);
275 Window xroot
= RootWindow( (Display
*)xdisplay
, xscreen
);
276 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
278 XImage
* ximage
= XCreateImage( (Display
*)xdisplay
, xvisual
, depth
,
279 ZPixmap
, 0, 0, width
, height
, 32, 0 );
280 ximage
->data
= (char*)malloc( ximage
->bytes_per_line
* ximage
->height
);
281 ximage
= XGetSubImage( (Display
*)xdisplay
, (Pixmap
)xpixmap
,
283 AllPlanes
, ZPixmap
, ximage
, 0, 0 );
285 GC gc
= XCreateGC( (Display
*)xdisplay
, (Pixmap
)xpixmap
, 0, NULL
);
286 Pixmap ret
= XCreatePixmap( (Display
*)xdisplay
, xroot
,
287 width
, height
, depth
);
289 XPutImage( (Display
*)xdisplay
, ret
, gc
, ximage
,
290 0, 0, 0, 0, width
, height
);
291 XDestroyImage( ximage
);
292 XFreeGC( (Display
*)xdisplay
, gc
);
294 return (WXPixmap
)ret
;
297 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
299 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
305 wxBitmap::wxBitmap( int width
, int height
, int depth
)
307 Create( width
, height
, depth
);
310 bool wxBitmap::Create( int width
, int height
, int depth
)
314 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
316 m_refData
= new wxBitmapRefData();
318 M_BMPDATA
->m_display
= wxGlobalDisplay();
320 wxASSERT_MSG( M_BMPDATA
->m_display
, wxT("No display") );
322 int xscreen
= DefaultScreen( (Display
*) M_BMPDATA
->m_display
);
323 Window xroot
= RootWindow( (Display
*) M_BMPDATA
->m_display
, xscreen
);
325 int bpp
= DefaultDepth( (Display
*) M_BMPDATA
->m_display
, xscreen
);
326 if (depth
== -1) depth
= bpp
;
328 wxCHECK_MSG( (depth
== bpp
) ||
329 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
331 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
332 M_BMPDATA
->m_width
= width
;
333 M_BMPDATA
->m_height
= height
;
336 M_BMPDATA
->m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
337 M_BMPDATA
->m_bpp
= bpp
;
339 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Bitmap creation failed") );
343 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, 1 );
345 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
347 M_BMPDATA
->m_bpp
= 1;
351 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, depth
);
353 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Pixmap creation failed") );
355 M_BMPDATA
->m_bpp
= depth
;
361 bool wxBitmap::Create(void *data
, wxBitmapType type
,
362 int width
, int height
, int depth
)
366 wxBitmapHandler
*handler
= FindHandler(type
);
368 if ( handler
== NULL
) {
369 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
375 return handler
->Create(this, data
, type
, width
, height
, depth
);
378 bool wxBitmap::Create(WXPixmap pixmap
)
381 Pixmap xpixmap
= (Pixmap
)pixmap
;
382 Display
* xdisplay
= wxGlobalDisplay();
383 int xscreen
= DefaultScreen( xdisplay
);
384 Window xroot
= RootWindow( xdisplay
, xscreen
);
386 // make a copy of the Pixmap
390 unsigned width
, height
, border
, depth
;
392 XGetGeometry( xdisplay
, (Drawable
)xpixmap
, &root
, &x
, &y
,
393 &width
, &height
, &border
, &depth
);
394 copy
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
396 GC gc
= XCreateGC( xdisplay
, copy
, 0, NULL
);
397 XCopyArea( xdisplay
, xpixmap
, copy
, gc
, 0, 0, width
, height
, 0, 0 );
398 XFreeGC( xdisplay
, gc
);
401 wxBitmapRefData
* ref
= new wxBitmapRefData();
404 ref
->m_bitmap
= (WXPixmap
)copy
;
406 ref
->m_pixmap
= (WXPixmap
)copy
;
408 ref
->m_display
= (WXDisplay
*)xdisplay
;
409 ref
->m_width
= width
;
410 ref
->m_height
= height
;
418 bool wxBitmap::CreateFromXpm( const char **bits
)
420 wxCHECK_MSG( bits
, FALSE
, wxT("NULL pointer in wxBitmap::CreateFromXpm") );
422 return 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
] = (pixel
>> red_shift_right
) << red_shift_left
;
868 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
869 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
873 data
[pos
] = colors
[pixel
].red
>> 8;
874 data
[pos
+1] = colors
[pixel
].green
>> 8;
875 data
[pos
+2] = 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 wxBitmap
& bmp
)
909 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
911 LoadFile( filename
, type
);
914 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
916 m_refData
= new wxBitmapRefData
;
918 (void) Create((void*) bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
921 wxBitmap::~wxBitmap()
925 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
927 if ( m_refData
!= bmp
.m_refData
)
933 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
935 return m_refData
== bmp
.m_refData
;
938 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
940 return m_refData
!= bmp
.m_refData
;
943 bool wxBitmap::Ok() const
945 return (m_refData
!= NULL
);
948 int wxBitmap::GetHeight() const
950 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
952 return M_BMPDATA
->m_height
;
955 int wxBitmap::GetWidth() const
957 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
959 return M_BMPDATA
->m_width
;
962 int wxBitmap::GetDepth() const
964 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
966 return M_BMPDATA
->m_bpp
;
969 wxMask
*wxBitmap::GetMask() const
971 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
973 return M_BMPDATA
->m_mask
;
976 void wxBitmap::SetMask( wxMask
*mask
)
978 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
980 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
982 M_BMPDATA
->m_mask
= mask
;
985 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
991 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
994 (rect
.x
>= 0) && (rect
.y
>= 0) &&
995 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
996 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
997 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
999 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1000 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1004 wxMask
* mask
= new wxMask();
1005 mask
->SetDisplay( GetMask()->GetDisplay() );
1006 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
1007 GetMask()->GetBitmap(),
1009 rect
.width
, rect
.height
,
1012 ret
.SetMask( mask
);
1017 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
1020 rect
.width
, rect
.height
,
1021 M_BMPDATA
->m_bpp
) );
1026 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1029 rect
.width
, rect
.height
,
1036 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1037 const wxPalette
*palette
) const
1039 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1041 wxBitmapHandler
*handler
= FindHandler(type
);
1043 // Try to save the bitmap via wxImage handlers:
1044 if (handler
== NULL
)
1046 wxImage
image(this->ConvertToImage());
1047 if (image
.Ok()) return image
.SaveFile( name
, type
);
1052 return handler
->SaveFile(this, name
, type
, palette
);
1055 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1059 if (!wxFileExists(name
)) return FALSE
;
1061 wxBitmapHandler
*handler
= FindHandler(type
);
1063 if (handler
== NULL
)
1066 if (!image
.LoadFile( name
, type
))
1071 *this = wxBitmap(image
);
1077 return handler
->LoadFile(this, name
, type
, -1, -1);
1080 void wxBitmap::SetPalette(const wxPalette
& palette
)
1082 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1083 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1084 wxT("cannot set palette for bitmap of this depth"));
1086 delete M_BMPDATA
->m_palette
;
1087 M_BMPDATA
->m_palette
= NULL
;
1089 if (!palette
.Ok()) return;
1091 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1094 wxPalette
*wxBitmap::GetPalette() const
1096 if (!Ok()) return (wxPalette
*) NULL
;
1098 return M_BMPDATA
->m_palette
;
1101 void wxBitmap::SetHeight( int height
)
1103 if (!m_refData
) m_refData
= new wxBitmapRefData();
1105 M_BMPDATA
->m_height
= height
;
1108 void wxBitmap::SetWidth( int width
)
1110 if (!m_refData
) m_refData
= new wxBitmapRefData();
1112 M_BMPDATA
->m_width
= width
;
1115 void wxBitmap::SetDepth( int depth
)
1117 if (!m_refData
) m_refData
= new wxBitmapRefData();
1119 M_BMPDATA
->m_bpp
= depth
;
1122 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1124 if (!m_refData
) m_refData
= new wxBitmapRefData();
1126 M_BMPDATA
->m_pixmap
= pixmap
;
1129 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1131 if (!m_refData
) m_refData
= new wxBitmapRefData();
1133 M_BMPDATA
->m_bitmap
= bitmap
;
1136 WXPixmap
wxBitmap::GetPixmap() const
1138 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1140 return M_BMPDATA
->m_pixmap
;
1143 WXPixmap
wxBitmap::GetBitmap() const
1145 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1147 return M_BMPDATA
->m_bitmap
;
1150 WXPixmap
wxBitmap::GetDrawable() const
1152 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1154 return M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
: M_BMPDATA
->m_pixmap
;
1157 WXDisplay
*wxBitmap::GetDisplay() const
1159 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1161 return M_BMPDATA
->m_display
;
1165 // Copy from the drawable to the wxImage
1166 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1168 GR_SCREEN_INFO sinfo
;
1170 GR_PIXELVAL
*pixels
;
1171 GR_PALETTE
* palette
= NULL
;
1172 unsigned char rgb
[3], *pp
;
1174 GrGetScreenInfo(&sinfo
);
1176 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1177 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1180 GrGetSystemPalette(palette
);
1183 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1188 image
.Create(width
, height
);
1190 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1194 for(x
= 0; x
< sinfo
.cols
; x
++) {
1196 pp
= (unsigned char *)pixels
+
1197 ((x
+ (y
* sinfo
.cols
)) *
1198 sizeof(GR_PIXELVAL
));
1200 switch(sinfo
.pixtype
) {
1201 /* FIXME: These may need modifying on big endian. */
1202 case MWPF_TRUECOLOR0888
:
1203 case MWPF_TRUECOLOR888
:
1209 rgb
[0] = palette
->palette
[pp
[0]].r
;
1210 rgb
[1] = palette
->palette
[pp
[0]].g
;
1211 rgb
[2] = palette
->palette
[pp
[0]].b
;
1213 case MWPF_TRUECOLOR565
:
1214 rgb
[0] = pp
[1] & 0xf8;
1215 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1216 ((pp
[0] & 0xe0) >> 3);
1217 rgb
[2] = (pp
[0] & 0x1f) << 3;
1219 case MWPF_TRUECOLOR555
:
1220 rgb
[0] = (pp
[1] & 0x7c) << 1;
1221 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1222 ((pp
[0] & 0xe0) >> 2);
1223 rgb
[2] = (pp
[0] & 0x1f) << 3;
1225 case MWPF_TRUECOLOR332
:
1226 rgb
[0] = pp
[0] & 0xe0;
1227 rgb
[1] = (pp
[0] & 0x1c) << 3;
1228 rgb
[2] = (pp
[0] & 0x03) << 6;
1231 fprintf(stderr
, "Unsupported pixel "
1236 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1241 if(palette
) free(palette
);
1247 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1248 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1250 unsigned char rgb
[3], *pp
;
1252 pp
= (unsigned char*) & pixel
;
1254 switch (sinfo
.pixtype
)
1256 /* FIXME: These may need modifying on big endian. */
1257 case MWPF_TRUECOLOR0888
:
1258 case MWPF_TRUECOLOR888
:
1264 rgb
[0] = palette
->palette
[pp
[0]].r
;
1265 rgb
[1] = palette
->palette
[pp
[0]].g
;
1266 rgb
[2] = palette
->palette
[pp
[0]].b
;
1268 case MWPF_TRUECOLOR565
:
1269 rgb
[0] = pp
[1] & 0xf8;
1270 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1271 ((pp
[0] & 0xe0) >> 3);
1272 rgb
[2] = (pp
[0] & 0x1f) << 3;
1274 case MWPF_TRUECOLOR555
:
1275 rgb
[0] = (pp
[1] & 0x7c) << 1;
1276 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1277 ((pp
[0] & 0xe0) >> 2);
1278 rgb
[2] = (pp
[0] & 0x1f) << 3;
1280 case MWPF_TRUECOLOR332
:
1281 rgb
[0] = pp
[0] & 0xe0;
1282 rgb
[1] = (pp
[0] & 0x1c) << 3;
1283 rgb
[2] = (pp
[0] & 0x03) << 6;
1286 fprintf(stderr
, "Unsupported pixel format\n");
1301 // ============================================================================
1303 // ============================================================================
1305 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
);
1307 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1311 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1313 // ----------------------------------------------------------------------------
1315 // ----------------------------------------------------------------------------
1317 class wxXPMFileHandler
: public wxBitmapHandler
1319 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
);
1323 m_name
= wxT("XPM file");
1324 m_extension
= wxT("xpm");
1325 m_type
= wxBITMAP_TYPE_XPM
;
1328 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1329 int desiredWidth
, int desiredHeight
);
1331 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1332 int type
, const wxPalette
*palette
= NULL
);
1334 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1335 int width
, int height
, int depth
= 1)
1339 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
);
1341 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1342 long flags
, int desiredWidth
,
1346 if (!bitmap
->GetRefData())
1347 bitmap
->SetRefData( new wxBitmapRefData() );
1349 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1351 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1353 int xscreen
= DefaultScreen( xdisplay
);
1354 Window xroot
= RootWindow( xdisplay
, xscreen
);
1356 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1358 XpmAttributes xpmAttr
;
1359 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1364 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1365 (char*) name
.c_str(),
1366 &pixmap
, &mask
, &xpmAttr
);
1368 if (ErrorStatus
== XpmSuccess
)
1370 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1371 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1373 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1375 XpmFreeAttributes(&xpmAttr
);
1377 M_BMPHANDLERDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1381 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1382 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1383 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1395 wxXPMDecoder decoder
;
1396 wxFileInputStream
stream(name
);
1399 wxImage
image(decoder
.ReadFile(stream
));
1400 return image
.Ok() && bitmap
->CreateFromImage(image
);
1404 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1406 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1409 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1411 const wxPalette
*WXUNUSED(palette
))
1413 wxImage
image(bitmap
->ConvertToImage());
1414 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1419 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1421 // ----------------------------------------------------------------------------
1423 // ----------------------------------------------------------------------------
1425 class wxXPMDataHandler
: public wxBitmapHandler
1427 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
);
1431 m_name
= wxT("XPM data");
1432 m_extension
= wxT("xpm");
1433 m_type
= wxBITMAP_TYPE_XPM_DATA
;
1436 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1437 int desiredWidth
, int desiredHeight
)
1440 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1441 int type
, const wxPalette
*palette
= NULL
)
1444 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1445 int width
, int height
, int depth
= 1);
1448 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
);
1450 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *bits
,
1451 long WXUNUSED(flags
),
1452 int width
, int height
, int WXUNUSED(depth
))
1455 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
1457 if (!bitmap
->GetRefData())
1458 bitmap
->SetRefData( new wxBitmapRefData() );
1460 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1462 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1464 int xscreen
= DefaultScreen( xdisplay
);
1465 Window xroot
= RootWindow( xdisplay
, xscreen
);
1467 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1469 XpmAttributes xpmAttr
;
1470 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1475 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1476 &pixmap
, &mask
, &xpmAttr
);
1478 if (ErrorStatus
== XpmSuccess
)
1480 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1481 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1483 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1486 unsigned int depthRet
;
1488 unsigned int widthRet
, heightRet
, borderWidthRet
;
1489 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1490 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1492 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1495 XpmFreeAttributes(&xpmAttr
);
1497 M_BMPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
1501 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1502 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1503 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1513 #else // !wxHAVE_LIB_XPM
1514 wxXPMDecoder decoder
;
1515 wxImage
image(decoder
.ReadData((const char **)bits
));
1516 return image
.Ok() && bitmap
->CreateFromImage(image
);
1517 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1522 // ----------------------------------------------------------------------------
1524 // ----------------------------------------------------------------------------
1526 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1528 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1530 inline wxXBMDataHandler()
1532 m_name
= wxT("XBM data");
1533 m_extension
= wxT("xbm");
1534 m_type
= wxBITMAP_TYPE_XBM_DATA
;
1537 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1538 int desiredWidth
, int desiredHeight
)
1541 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1542 int type
, const wxPalette
*palette
= NULL
)
1545 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1546 int width
, int height
, int depth
= 1);
1549 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
);
1551 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, void *bits
,
1552 long WXUNUSED(flags
),
1553 int width
, int height
, int depth
)
1556 if (!bitmap
->GetRefData())
1557 bitmap
->SetRefData( new wxBitmapRefData() );
1559 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1561 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1563 int xscreen
= DefaultScreen( xdisplay
);
1564 Window xroot
= RootWindow( xdisplay
, xscreen
);
1566 M_BMPHANDLERDATA
->m_mask
= (wxMask
*) NULL
;
1567 M_BMPHANDLERDATA
->m_bitmap
=
1568 (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
,
1569 (char *) bits
, width
, height
);
1570 M_BMPHANDLERDATA
->m_width
= width
;
1571 M_BMPHANDLERDATA
->m_height
= height
;
1572 M_BMPHANDLERDATA
->m_bpp
= 1;
1576 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, FALSE
,
1577 wxT("couldn't create bitmap") );
1581 void wxBitmap::InitStandardHandlers()
1583 AddHandler(new wxXBMDataHandler
);
1585 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1586 AddHandler(new wxXPMFileHandler
);
1588 AddHandler(new wxXPMDataHandler
);