1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart, Robert Roebling
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
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("no data bitmap handler for type %ld defined.",
375 return handler
->Create(this, data
, type
, width
, height
, depth
);
378 bool wxBitmap::CreateFromXpm( const char **bits
)
380 wxCHECK_MSG( bits
, FALSE
, _T("NULL pointer in wxBitmap::CreateFromXpm") );
382 return Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
385 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
390 wxASSERT_MSG(image
.Ok(), "Invalid wxImage passed to wxBitmap::CreateFromImage.");
394 int w
= image
.GetWidth();
395 int h
= image
.GetHeight();
397 if (!Create(w
, h
, depth
))
400 // Unfortunately the mask has to be screen-depth since
401 // 1-bpp bitmaps don't seem to be supported
402 // TODO: implement transparent drawing, presumably
403 // by doing several blits as per the Windows
404 // implementation because Nano-X doesn't support
406 // TODO: could perhaps speed this function up
407 // by making a buffer of pixel values,
408 // and then calling GrArea to write that to the
409 // pixmap. See demos/nxroach.c.
411 bool hasMask
= image
.HasMask();
413 GC pixmapGC
= GrNewGC();
414 Pixmap pixmap
= (Pixmap
) GetPixmap();
417 Pixmap maskPixmap
= 0;
419 unsigned char maskR
= 0;
420 unsigned char maskG
= 0;
421 unsigned char maskB
= 0;
425 maskR
= image
.GetMaskRed();
426 maskG
= image
.GetMaskGreen();
427 maskB
= image
.GetMaskBlue();
430 maskPixmap
= GrNewPixmap(w
, h
, 0);
435 wxMask
* mask
= new wxMask
;
436 mask
->SetBitmap((WXPixmap
) maskPixmap
);
441 GR_COLOR lastPixmapColour
= 0;
442 GR_COLOR lastMaskColour
= 0;
445 for (i
= 0; i
< w
; i
++)
447 for (j
= 0; j
< h
; j
++)
449 unsigned char red
= image
.GetRed(i
, j
);
450 unsigned char green
= image
.GetGreen(i
, j
);
451 unsigned char blue
= image
.GetBlue(i
, j
);
453 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
455 // Efficiency measure
456 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
458 GrSetGCForeground(pixmapGC
, colour
);
459 lastPixmapColour
= colour
;
462 GrPoint(pixmap
, pixmapGC
, i
, j
);
466 // scan the bitmap for the transparent colour and set the corresponding
467 // pixels in the mask to BLACK and the rest to WHITE
468 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
470 colour
= GR_RGB(0, 0, 0);
474 colour
= GR_RGB(255, 255, 255);
476 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
478 GrSetGCForeground(maskGC
, colour
);
479 lastMaskColour
= colour
;
481 GrPoint(maskPixmap
, maskGC
, i
, j
);
486 GrDestroyGC(pixmapGC
);
496 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
497 wxCHECK_MSG( depth
== -1, FALSE
, wxT("invalid bitmap depth") )
499 m_refData
= new wxBitmapRefData();
501 M_BMPDATA
->m_display
= wxGlobalDisplay();
503 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
505 int xscreen
= DefaultScreen( xdisplay
);
506 Window xroot
= RootWindow( xdisplay
, xscreen
);
507 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
509 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
511 int width
= image
.GetWidth();
512 int height
= image
.GetHeight();
513 M_BMPDATA
->m_width
= width
;
514 M_BMPDATA
->m_height
= height
;
516 if (depth
!= 1) depth
= bpp
;
517 M_BMPDATA
->m_bpp
= depth
;
521 wxFAIL_MSG( "mono images later" );
527 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
528 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
530 if (data_image
->data
== NULL
)
532 wxLogError( wxT("Out of memory.") ); // TODO clean
536 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
540 XImage
*mask_image
= (XImage
*) NULL
;
543 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
544 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
546 if (mask_image
->data
== NULL
)
548 wxLogError( wxT("Out of memory.") ); // TODO clean
552 wxMask
*mask
= new wxMask();
553 mask
->SetDisplay( xdisplay
);
554 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
559 if (bpp
< 8) bpp
= 8;
563 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
564 byte_order b_o
= RGB
;
566 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
567 unsigned long greenMask
= vi
->m_visualGreenMask
,
568 redMask
= vi
->m_visualRedMask
,
569 blueMask
= vi
->m_visualBlueMask
;
573 if ((redMask
> greenMask
) && (greenMask
> blueMask
)) b_o
= RGB
;
574 else if ((redMask
> blueMask
) && (blueMask
> greenMask
)) b_o
= RBG
;
575 else if ((blueMask
> redMask
) && (redMask
> greenMask
)) b_o
= BRG
;
576 else if ((blueMask
> greenMask
) && (greenMask
> redMask
))b_o
= BGR
;
577 else if ((greenMask
> redMask
) && (redMask
> blueMask
)) b_o
= GRB
;
578 else if ((greenMask
> blueMask
) && (blueMask
> redMask
)) b_o
= GBR
;
581 int r_mask
= image
.GetMaskRed();
582 int g_mask
= image
.GetMaskGreen();
583 int b_mask
= image
.GetMaskBlue();
585 unsigned char* data
= image
.GetData();
586 wxASSERT_MSG( data
, "No image data" );
588 unsigned char *colorCube
=
589 wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_colorCube
;
591 bool hasMask
= image
.HasMask();
594 for (int y
= 0; y
< height
; y
++)
596 for (int x
= 0; x
< width
; x
++)
607 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
608 XPutPixel( mask_image
, x
, y
, 0 );
610 XPutPixel( mask_image
, x
, y
, 1 );
618 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
619 XPutPixel( data_image
, x
, y
, pixel
);
627 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
628 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
629 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
630 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
631 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
632 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
634 XPutPixel( data_image
, x
, y
, pixel
);
642 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
643 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
644 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
645 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
646 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
647 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
649 XPutPixel( data_image
, x
, y
, pixel
);
654 // I actually don't know if for 16-bit displays, it is alway the green
655 // component or the second component which has 6 bits.
659 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
660 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
661 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
662 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
663 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
664 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
666 XPutPixel( data_image
, x
, y
, pixel
);
675 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
676 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
677 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
678 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
679 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
680 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
682 XPutPixel( data_image
, x
, y
, pixel
);
691 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
692 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
693 XDestroyImage( data_image
);
694 XFreeGC( xdisplay
, gc
);
700 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
701 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
703 XDestroyImage( mask_image
);
704 XFreeGC( xdisplay
, gc
);
713 wxImage
wxBitmap::ConvertToImage() const
717 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
719 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
720 wxASSERT_MSG( xdisplay
, wxT("No display") );
723 //int bpp = DefaultDepth(xdisplay, xscreen);
724 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
728 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
729 XImage
*x_image
= NULL
;
732 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
734 GetWidth(), GetHeight(),
735 AllPlanes
, ZPixmap
);
739 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
741 GetWidth(), GetHeight(),
742 AllPlanes
, ZPixmap
);
745 wxFAIL_MSG( wxT("Ill-formed bitmap") );
748 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
750 image
.Create( GetWidth(), GetHeight() );
751 char unsigned *data
= image
.GetData();
755 XDestroyImage( x_image
);
756 wxFAIL_MSG( wxT("couldn't create image") );
760 XImage
*x_image_mask
= NULL
;
763 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
765 GetWidth(), GetHeight(),
766 AllPlanes
, ZPixmap
);
768 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
771 int red_shift_right
= 0;
772 int green_shift_right
= 0;
773 int blue_shift_right
= 0;
774 int red_shift_left
= 0;
775 int green_shift_left
= 0;
776 int blue_shift_left
= 0;
777 bool use_shift
= FALSE
;
781 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
783 red_shift_right
= vi
->m_visualRedShift
;
784 red_shift_left
= 8 - vi
->m_visualRedPrec
;
785 green_shift_right
= vi
->m_visualGreenShift
;
786 green_shift_left
= 8 - vi
->m_visualGreenPrec
;
787 blue_shift_right
= vi
->m_visualBlueShift
;
788 blue_shift_left
= 8 - vi
->m_visualBluePrec
;
790 use_shift
= (vi
->m_visualType
== GrayScale
) ||
791 (vi
->m_visualType
!= PseudoColor
);
799 XColor
*colors
= (XColor
*)wxTheApp
->
800 GetVisualInfo(M_BMPDATA
->m_display
)->m_visualColormap
;
802 int width
= GetWidth();
803 int height
= GetHeight();
805 for (int j
= 0; j
< height
; j
++)
807 for (int i
= 0; i
< width
; i
++)
809 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
827 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
828 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
829 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
833 data
[pos
] = colors
[pixel
].red
>> 8;
834 data
[pos
+1] = colors
[pixel
].green
>> 8;
835 data
[pos
+2] = colors
[pixel
].blue
>> 8;
839 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
844 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
857 XDestroyImage( x_image
);
858 if (x_image_mask
) XDestroyImage( x_image_mask
);
864 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
869 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
871 LoadFile( filename
, type
);
874 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
876 m_refData
= new wxBitmapRefData
;
878 (void) Create((void*) bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
881 wxBitmap::~wxBitmap()
885 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
887 if ( m_refData
!= bmp
.m_refData
)
893 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
895 return m_refData
== bmp
.m_refData
;
898 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
900 return m_refData
!= bmp
.m_refData
;
903 bool wxBitmap::Ok() const
905 return (m_refData
!= NULL
);
908 int wxBitmap::GetHeight() const
910 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
912 return M_BMPDATA
->m_height
;
915 int wxBitmap::GetWidth() const
917 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
919 return M_BMPDATA
->m_width
;
922 int wxBitmap::GetDepth() const
924 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
926 return M_BMPDATA
->m_bpp
;
929 wxMask
*wxBitmap::GetMask() const
931 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
933 return M_BMPDATA
->m_mask
;
936 void wxBitmap::SetMask( wxMask
*mask
)
938 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
940 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
942 M_BMPDATA
->m_mask
= mask
;
945 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
951 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
954 (rect
.x
>= 0) && (rect
.y
>= 0) &&
955 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
956 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
957 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
959 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
960 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
964 wxMask
* mask
= new wxMask();
965 mask
->SetDisplay( GetMask()->GetDisplay() );
966 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
967 GetMask()->GetBitmap(),
969 rect
.width
, rect
.height
,
977 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
980 rect
.width
, rect
.height
,
981 M_BMPDATA
->m_bpp
) );
986 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
989 rect
.width
, rect
.height
,
996 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
997 const wxPalette
*palette
) const
999 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1001 wxBitmapHandler
*handler
= FindHandler(type
);
1003 // Try to save the bitmap via wxImage handlers:
1004 if (handler
== NULL
)
1006 wxImage
image(this->ConvertToImage());
1007 if (image
.Ok()) return image
.SaveFile( name
, type
);
1012 return handler
->SaveFile(this, name
, type
, palette
);
1015 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1019 if (!wxFileExists(name
)) return FALSE
;
1021 wxBitmapHandler
*handler
= FindHandler(type
);
1023 if (handler
== NULL
)
1026 if (!image
.LoadFile( name
, type
))
1031 *this = wxBitmap(image
);
1037 return handler
->LoadFile(this, name
, type
, -1, -1);
1040 void wxBitmap::SetPalette(const wxPalette
& palette
)
1042 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1043 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1044 wxT("cannot set palette for bitmap of this depth"));
1046 delete M_BMPDATA
->m_palette
;
1047 M_BMPDATA
->m_palette
= NULL
;
1049 if (!palette
.Ok()) return;
1051 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1054 wxPalette
*wxBitmap::GetPalette() const
1056 if (!Ok()) return (wxPalette
*) NULL
;
1058 return M_BMPDATA
->m_palette
;
1061 void wxBitmap::SetHeight( int height
)
1063 if (!m_refData
) m_refData
= new wxBitmapRefData();
1065 M_BMPDATA
->m_height
= height
;
1068 void wxBitmap::SetWidth( int width
)
1070 if (!m_refData
) m_refData
= new wxBitmapRefData();
1072 M_BMPDATA
->m_width
= width
;
1075 void wxBitmap::SetDepth( int depth
)
1077 if (!m_refData
) m_refData
= new wxBitmapRefData();
1079 M_BMPDATA
->m_bpp
= depth
;
1082 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1084 if (!m_refData
) m_refData
= new wxBitmapRefData();
1086 M_BMPDATA
->m_pixmap
= pixmap
;
1089 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1091 if (!m_refData
) m_refData
= new wxBitmapRefData();
1093 M_BMPDATA
->m_bitmap
= bitmap
;
1096 WXPixmap
wxBitmap::GetPixmap() const
1098 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1100 return M_BMPDATA
->m_pixmap
;
1103 WXPixmap
wxBitmap::GetBitmap() const
1105 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1107 return M_BMPDATA
->m_bitmap
;
1110 WXPixmap
wxBitmap::GetDrawable() const
1112 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1114 return M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
: M_BMPDATA
->m_pixmap
;
1117 WXDisplay
*wxBitmap::GetDisplay() const
1119 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1121 return M_BMPDATA
->m_display
;
1125 // Copy from the drawable to the wxImage
1126 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1128 GR_SCREEN_INFO sinfo
;
1130 GR_PIXELVAL
*pixels
;
1131 GR_PALETTE
* palette
= NULL
;
1132 unsigned char rgb
[3], *pp
;
1134 GrGetScreenInfo(&sinfo
);
1136 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1137 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1140 GrGetSystemPalette(palette
);
1143 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1148 image
.Create(width
, height
);
1150 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1154 for(x
= 0; x
< sinfo
.cols
; x
++) {
1156 pp
= (unsigned char *)pixels
+
1157 ((x
+ (y
* sinfo
.cols
)) *
1158 sizeof(GR_PIXELVAL
));
1160 switch(sinfo
.pixtype
) {
1161 /* FIXME: These may need modifying on big endian. */
1162 case MWPF_TRUECOLOR0888
:
1163 case MWPF_TRUECOLOR888
:
1169 rgb
[0] = palette
->palette
[pp
[0]].r
;
1170 rgb
[1] = palette
->palette
[pp
[0]].g
;
1171 rgb
[2] = palette
->palette
[pp
[0]].b
;
1173 case MWPF_TRUECOLOR565
:
1174 rgb
[0] = pp
[1] & 0xf8;
1175 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1176 ((pp
[0] & 0xe0) >> 3);
1177 rgb
[2] = (pp
[0] & 0x1f) << 3;
1179 case MWPF_TRUECOLOR555
:
1180 rgb
[0] = (pp
[1] & 0x7c) << 1;
1181 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1182 ((pp
[0] & 0xe0) >> 2);
1183 rgb
[2] = (pp
[0] & 0x1f) << 3;
1185 case MWPF_TRUECOLOR332
:
1186 rgb
[0] = pp
[0] & 0xe0;
1187 rgb
[1] = (pp
[0] & 0x1c) << 3;
1188 rgb
[2] = (pp
[0] & 0x03) << 6;
1191 fprintf(stderr
, "Unsupported pixel "
1196 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1201 if(palette
) free(palette
);
1207 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1208 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1210 unsigned char rgb
[3], *pp
;
1212 pp
= (unsigned char*) & pixel
;
1214 switch (sinfo
.pixtype
)
1216 /* FIXME: These may need modifying on big endian. */
1217 case MWPF_TRUECOLOR0888
:
1218 case MWPF_TRUECOLOR888
:
1224 rgb
[0] = palette
->palette
[pp
[0]].r
;
1225 rgb
[1] = palette
->palette
[pp
[0]].g
;
1226 rgb
[2] = palette
->palette
[pp
[0]].b
;
1228 case MWPF_TRUECOLOR565
:
1229 rgb
[0] = pp
[1] & 0xf8;
1230 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1231 ((pp
[0] & 0xe0) >> 3);
1232 rgb
[2] = (pp
[0] & 0x1f) << 3;
1234 case MWPF_TRUECOLOR555
:
1235 rgb
[0] = (pp
[1] & 0x7c) << 1;
1236 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1237 ((pp
[0] & 0xe0) >> 2);
1238 rgb
[2] = (pp
[0] & 0x1f) << 3;
1240 case MWPF_TRUECOLOR332
:
1241 rgb
[0] = pp
[0] & 0xe0;
1242 rgb
[1] = (pp
[0] & 0x1c) << 3;
1243 rgb
[2] = (pp
[0] & 0x03) << 6;
1246 fprintf(stderr
, "Unsupported pixel format\n");
1261 // ============================================================================
1263 // ============================================================================
1265 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
);
1267 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1271 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1273 // ----------------------------------------------------------------------------
1275 // ----------------------------------------------------------------------------
1277 class wxXPMFileHandler
: public wxBitmapHandler
1279 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
);
1283 m_name
= "XPM file";
1284 m_extension
= "xpm";
1285 m_type
= wxBITMAP_TYPE_XPM
;
1288 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1289 int desiredWidth
, int desiredHeight
);
1291 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1292 int type
, const wxPalette
*palette
= NULL
);
1294 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1295 int width
, int height
, int depth
= 1)
1299 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
);
1301 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1302 long flags
, int desiredWidth
,
1306 if (!bitmap
->GetRefData())
1307 bitmap
->SetRefData( new wxBitmapRefData() );
1309 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1311 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1313 int xscreen
= DefaultScreen( xdisplay
);
1314 Window xroot
= RootWindow( xdisplay
, xscreen
);
1316 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1318 XpmAttributes xpmAttr
;
1319 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1324 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1325 (char*) name
.c_str(),
1326 &pixmap
, &mask
, &xpmAttr
);
1328 if (ErrorStatus
== XpmSuccess
)
1330 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1331 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1333 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1335 XpmFreeAttributes(&xpmAttr
);
1337 M_BMPHANDLERDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1341 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1342 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1343 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1355 wxXPMDecoder decoder
;
1356 wxFileInputStream
stream(name
);
1359 wxImage
image(decoder
.ReadFile(stream
));
1361 return CreateFromImage(image
);
1367 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1370 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1372 const wxPalette
*WXUNUSED(palette
))
1374 wxImage
image(bitmap
->ConvertToImage());
1375 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1380 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1382 // ----------------------------------------------------------------------------
1384 // ----------------------------------------------------------------------------
1386 class wxXPMDataHandler
: public wxBitmapHandler
1388 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
);
1392 m_name
= "XPM data";
1393 m_extension
= "xpm";
1394 m_type
= wxBITMAP_TYPE_XPM_DATA
;
1397 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1398 int desiredWidth
, int desiredHeight
)
1401 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1402 int type
, const wxPalette
*palette
= NULL
)
1405 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1406 int width
, int height
, int depth
= 1);
1409 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
);
1411 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *bits
,
1412 long WXUNUSED(flags
),
1413 int width
, int height
, int WXUNUSED(depth
))
1416 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
1418 if (!bitmap
->GetRefData())
1419 bitmap
->SetRefData( new wxBitmapRefData() );
1421 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1423 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1425 int xscreen
= DefaultScreen( xdisplay
);
1426 Window xroot
= RootWindow( xdisplay
, xscreen
);
1428 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1430 XpmAttributes xpmAttr
;
1431 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1436 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1437 &pixmap
, &mask
, &xpmAttr
);
1439 if (ErrorStatus
== XpmSuccess
)
1441 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1442 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1444 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1447 unsigned int depthRet
;
1449 unsigned int widthRet
, heightRet
, borderWidthRet
;
1450 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1451 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1453 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1456 XpmFreeAttributes(&xpmAttr
);
1458 M_BMPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
1462 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1463 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1464 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1475 wxXPMDecoder decoder
;
1476 wxImage
image(decoder
.ReadData(bits
));
1478 return bitmap
->CreateFromImage(image
);
1486 // ----------------------------------------------------------------------------
1488 // ----------------------------------------------------------------------------
1490 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1492 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1494 inline wxXBMDataHandler()
1496 m_name
= "XBM data";
1497 m_extension
= "xbm";
1498 m_type
= wxBITMAP_TYPE_XBM_DATA
;
1501 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1502 int desiredWidth
, int desiredHeight
)
1505 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1506 int type
, const wxPalette
*palette
= NULL
)
1509 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1510 int width
, int height
, int depth
= 1);
1513 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
);
1515 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, void *bits
,
1516 long WXUNUSED(flags
),
1517 int width
, int height
, int depth
)
1520 if (!bitmap
->GetRefData())
1521 bitmap
->SetRefData( new wxBitmapRefData() );
1523 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1525 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1527 int xscreen
= DefaultScreen( xdisplay
);
1528 Window xroot
= RootWindow( xdisplay
, xscreen
);
1530 M_BMPHANDLERDATA
->m_mask
= (wxMask
*) NULL
;
1531 M_BMPHANDLERDATA
->m_bitmap
=
1532 (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
,
1533 (char *) bits
, width
, height
);
1534 M_BMPHANDLERDATA
->m_width
= width
;
1535 M_BMPHANDLERDATA
->m_height
= height
;
1536 M_BMPHANDLERDATA
->m_bpp
= 1;
1540 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, FALSE
,
1541 wxT("couldn't create bitmap") );
1544 void wxBitmap::InitStandardHandlers()
1546 AddHandler(new wxXBMDataHandler
);
1548 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1549 AddHandler(new wxXPMFileHandler
);
1551 AddHandler(new wxXPMDataHandler
);