1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/bitmap.cpp
4 // Author: Julian Smart, Robert Roebling
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #include "wx/bitmap.h"
20 #include "wx/dcmemory.h"
26 #include "wx/x11/private.h"
28 /* No point in using libXPM for NanoX */
31 #define wxHAVE_LIB_XPM 0
33 // Copy from the drawable to the wxImage
34 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
);
41 #include "wx/xpmdecod.h"
42 #include "wx/wfstream.h"
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
58 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
61 Create( bitmap
, colour
);
64 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
67 Create( bitmap
, paletteIndex
);
70 wxMask::wxMask( const wxBitmap
& bitmap
)
79 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
82 bool wxMask::Create( const wxBitmap
& bitmap
,
83 const wxColour
& colour
)
88 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
92 m_display
= bitmap
.GetDisplay();
94 wxImage image
= bitmap
.ConvertToImage();
95 if (!image
.Ok()) return false;
97 m_display
= bitmap
.GetDisplay();
99 Display
*xdisplay
= (Display
*) m_display
;
100 int xscreen
= DefaultScreen( xdisplay
);
101 Window xroot
= RootWindow( xdisplay
, xscreen
);
103 m_bitmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, image
.GetWidth(), image
.GetHeight(), 1 );
104 GC gc
= XCreateGC( xdisplay
, (Pixmap
) m_bitmap
, 0, NULL
);
106 XSetForeground( xdisplay
, gc
, WhitePixel(xdisplay
,xscreen
) );
107 XSetFillStyle( xdisplay
, gc
, FillSolid
);
108 XFillRectangle( xdisplay
, (Pixmap
) m_bitmap
, gc
, 0, 0, image
.GetWidth(), image
.GetHeight() );
110 unsigned char *data
= image
.GetData();
113 unsigned char red
= colour
.Red();
114 unsigned char green
= colour
.Green();
115 unsigned char blue
= colour
.Blue();
117 int bpp
= wxTheApp
->GetVisualInfo(m_display
)->m_visualDepth
;
138 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
140 int width
= image
.GetWidth();
141 int height
= image
.GetHeight();
142 for (int j
= 0; j
< height
; j
++)
146 for (i
= 0; i
< width
; i
++)
148 if ((data
[index
] == red
) &&
149 (data
[index
+1] == green
) &&
150 (data
[index
+2] == blue
))
159 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
166 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
169 XFreeGC( xdisplay
, gc
);
178 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
181 wxPalette
*pal
= bitmap
.GetPalette();
183 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
185 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
187 return Create(bitmap
, wxColour(r
, g
, b
));
190 bool wxMask::Create( const wxBitmap
& bitmap
)
195 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
199 if (!bitmap
.Ok()) return false;
201 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
203 m_display
= bitmap
.GetDisplay();
205 int xscreen
= DefaultScreen( (Display
*) m_display
);
206 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
208 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
210 if (!m_bitmap
) return false;
212 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
214 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
215 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
217 XFreeGC( (Display
*) m_display
, gc
);
226 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
230 class wxBitmapRefData
: public wxGDIRefData
234 wxBitmapRefData(const wxBitmapRefData
& data
);
235 virtual ~wxBitmapRefData();
237 // shouldn't be called more than once as it doesn't free the existing data
238 bool Create(int width
, int height
, int depth
);
240 virtual bool IsOk() const { return m_pixmap
|| m_bitmap
; }
249 wxPalette
*m_palette
;
252 wxBitmapRefData::wxBitmapRefData()
261 m_palette
= (wxPalette
*) NULL
;
264 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
268 m_display
= data
.m_display
;
269 m_mask
= NULL
; // FIXME: should copy
270 m_palette
= NULL
; // FIXME: should copy
272 Create(data
.m_width
, data
.m_height
, data
.m_bpp
);
275 bool wxBitmapRefData::Create(int width
, int height
, int depth
)
281 m_display
= wxGlobalDisplay();
283 wxCHECK_MSG( m_display
, false, wxT("No display") );
285 int xscreen
= DefaultScreen(m_display
);
286 int bpp
= DefaultDepth(m_display
, xscreen
);
290 wxCHECK_MSG( (depth
== bpp
) || (depth
== 1), false,
291 wxT("invalid bitmap depth") );
294 m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
295 #else // !wxUSE_NANOX
296 Window xroot
= RootWindow(m_display
, xscreen
);
298 *(depth
== 1 ? &m_bitmap
: &m_pixmap
) =
299 XCreatePixmap(m_display
, xroot
, width
, height
, depth
);
300 #endif // wxUSE_NANOX/!wxUSE_NANOX
302 wxCHECK_MSG( m_pixmap
|| m_bitmap
, false, wxT("Bitmap creation failed") );
307 wxBitmapRefData::~wxBitmapRefData()
310 XFreePixmap(m_display
, m_pixmap
);
312 XFreePixmap(m_display
, m_bitmap
);
317 //-----------------------------------------------------------------------------
321 static WXPixmap
wxGetSubPixmap( WXDisplay
* xdisplay
, WXPixmap xpixmap
,
322 int x
, int y
, int width
, int height
,
325 Display
* const dpy
= (Display
*)xdisplay
;
327 int xscreen
= DefaultScreen( dpy
);
328 Window xroot
= RootWindow( dpy
, xscreen
);
329 Visual
* xvisual
= DefaultVisual( dpy
, xscreen
);
331 XImage
* ximage
= XCreateImage( dpy
, xvisual
, depth
,
332 ZPixmap
, 0, 0, width
, height
, 32, 0 );
333 ximage
->data
= (char*)malloc( ximage
->bytes_per_line
* ximage
->height
);
334 ximage
= XGetSubImage( dpy
, (Pixmap
)xpixmap
,
336 AllPlanes
, ZPixmap
, ximage
, 0, 0 );
338 GC gc
= XCreateGC( dpy
, (Pixmap
)xpixmap
, 0, NULL
);
339 Pixmap ret
= XCreatePixmap( dpy
, xroot
,
340 width
, height
, depth
);
342 XPutImage( dpy
, ret
, gc
, ximage
,
343 0, 0, 0, 0, width
, height
);
344 XDestroyImage( ximage
);
347 return (WXPixmap
)ret
;
350 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
352 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
358 wxBitmap::wxBitmap( int width
, int height
, int depth
)
360 Create( width
, height
, depth
);
363 bool wxBitmap::Create( int width
, int height
, int depth
)
367 wxCHECK_MSG( (width
> 0) && (height
> 0), false, wxT("invalid bitmap size") );
369 m_refData
= new wxBitmapRefData();
371 return M_BMPDATA
->Create(width
, height
, depth
);
374 bool wxBitmap::Create(const void* data
, wxBitmapType type
,
375 int width
, int height
, int depth
)
379 wxBitmapHandler
*handler
= FindHandler(type
);
381 if ( handler
== NULL
) {
382 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
388 return handler
->Create(this, data
, type
, width
, height
, depth
);
391 bool wxBitmap::Create(WXPixmap pixmap
)
394 Pixmap xpixmap
= (Pixmap
)pixmap
;
395 Display
* xdisplay
= wxGlobalDisplay();
396 int xscreen
= DefaultScreen( xdisplay
);
397 Window xroot
= RootWindow( xdisplay
, xscreen
);
399 // make a copy of the Pixmap
402 unsigned width
, height
, border
, depth
;
404 XGetGeometry( xdisplay
, (Drawable
)xpixmap
, &root
, &x
, &y
,
405 &width
, &height
, &border
, &depth
);
406 Pixmap copy
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
408 GC gc
= XCreateGC( xdisplay
, copy
, 0, NULL
);
409 XCopyArea( xdisplay
, xpixmap
, copy
, gc
, 0, 0, width
, height
, 0, 0 );
410 XFreeGC( xdisplay
, gc
);
413 wxBitmapRefData
* ref
= new wxBitmapRefData();
416 ref
->m_bitmap
= copy
;
418 ref
->m_pixmap
= copy
;
420 ref
->m_display
= xdisplay
;
421 ref
->m_width
= width
;
422 ref
->m_height
= height
;
430 wxBitmap::wxBitmap(const char* const* bits
)
432 Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
435 wxGDIRefData
*wxBitmap::CreateGDIRefData() const
437 return new wxBitmapRefData
;
440 wxGDIRefData
*wxBitmap::CloneGDIRefData(const wxGDIRefData
*data
) const
442 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
445 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
450 wxASSERT_MSG(image
.Ok(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
454 int w
= image
.GetWidth();
455 int h
= image
.GetHeight();
457 if (!Create(w
, h
, depth
))
460 // Unfortunately the mask has to be screen-depth since
461 // 1-bpp bitmaps don't seem to be supported
462 // TODO: implement transparent drawing, presumably
463 // by doing several blits as per the Windows
464 // implementation because Nano-X doesn't support
466 // TODO: could perhaps speed this function up
467 // by making a buffer of pixel values,
468 // and then calling GrArea to write that to the
469 // pixmap. See demos/nxroach.c.
471 bool hasMask
= image
.HasMask();
473 GC pixmapGC
= GrNewGC();
474 Pixmap pixmap
= (Pixmap
) GetPixmap();
477 Pixmap maskPixmap
= 0;
479 unsigned char maskR
= 0;
480 unsigned char maskG
= 0;
481 unsigned char maskB
= 0;
485 maskR
= image
.GetMaskRed();
486 maskG
= image
.GetMaskGreen();
487 maskB
= image
.GetMaskBlue();
490 maskPixmap
= GrNewPixmap(w
, h
, 0);
495 wxMask
* mask
= new wxMask
;
496 mask
->SetBitmap((WXPixmap
) maskPixmap
);
501 GR_COLOR lastPixmapColour
= 0;
502 GR_COLOR lastMaskColour
= 0;
505 for (i
= 0; i
< w
; i
++)
507 for (j
= 0; j
< h
; j
++)
509 unsigned char red
= image
.GetRed(i
, j
);
510 unsigned char green
= image
.GetGreen(i
, j
);
511 unsigned char blue
= image
.GetBlue(i
, j
);
513 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
515 // Efficiency measure
516 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
518 GrSetGCForeground(pixmapGC
, colour
);
519 lastPixmapColour
= colour
;
522 GrPoint(pixmap
, pixmapGC
, i
, j
);
526 // scan the bitmap for the transparent colour and set the corresponding
527 // pixels in the mask to BLACK and the rest to WHITE
528 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
530 colour
= GR_RGB(0, 0, 0);
534 colour
= GR_RGB(255, 255, 255);
536 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
538 GrSetGCForeground(maskGC
, colour
);
539 lastMaskColour
= colour
;
541 GrPoint(maskPixmap
, maskGC
, i
, j
);
546 GrDestroyGC(pixmapGC
);
556 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
557 wxCHECK_MSG( depth
== -1, false, wxT("invalid bitmap depth") );
559 m_refData
= new wxBitmapRefData();
561 M_BMPDATA
->m_display
= wxGlobalDisplay();
563 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
565 int xscreen
= DefaultScreen( xdisplay
);
566 Window xroot
= RootWindow( xdisplay
, xscreen
);
567 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
569 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
571 int width
= image
.GetWidth();
572 int height
= image
.GetHeight();
573 M_BMPDATA
->m_width
= width
;
574 M_BMPDATA
->m_height
= height
;
576 if (depth
!= 1) depth
= bpp
;
577 M_BMPDATA
->m_bpp
= depth
;
581 wxFAIL_MSG( wxT("mono images later") );
587 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
588 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
590 if (data_image
->data
== NULL
)
592 wxLogError( wxT("Out of memory.") ); // TODO clean
596 M_BMPDATA
->m_pixmap
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
598 // Create mask if necessary
599 const bool hasMask
= image
.HasMask();
601 XImage
*mask_image
= (XImage
*) NULL
;
604 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
605 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
607 if (mask_image
->data
== NULL
)
609 wxLogError( wxT("Out of memory.") ); // TODO clean
613 wxMask
*mask
= new wxMask();
614 mask
->SetDisplay( xdisplay
);
615 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
620 if (bpp
< 8) bpp
= 8;
624 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
625 byte_order b_o
= RGB
;
627 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
628 unsigned long greenMask
= vi
->m_visualGreenMask
,
629 redMask
= vi
->m_visualRedMask
,
630 blueMask
= vi
->m_visualBlueMask
;
634 if ((redMask
> greenMask
) && (greenMask
> blueMask
)) b_o
= RGB
;
635 else if ((redMask
> blueMask
) && (blueMask
> greenMask
)) b_o
= RBG
;
636 else if ((blueMask
> redMask
) && (redMask
> greenMask
)) b_o
= BRG
;
637 else if ((blueMask
> greenMask
) && (greenMask
> redMask
))b_o
= BGR
;
638 else if ((greenMask
> redMask
) && (redMask
> blueMask
)) b_o
= GRB
;
639 else if ((greenMask
> blueMask
) && (blueMask
> redMask
)) b_o
= GBR
;
642 int r_mask
= image
.GetMaskRed();
643 int g_mask
= image
.GetMaskGreen();
644 int b_mask
= image
.GetMaskBlue();
646 unsigned char* data
= image
.GetData();
647 wxASSERT_MSG( data
, wxT("No image data") );
649 unsigned char *colorCube
=
650 wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_colorCube
;
653 for (int y
= 0; y
< height
; y
++)
655 for (int x
= 0; x
< width
; x
++)
666 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
667 XPutPixel( mask_image
, x
, y
, 0 );
669 XPutPixel( mask_image
, x
, y
, 1 );
677 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
678 XPutPixel( data_image
, x
, y
, pixel
);
686 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
687 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
688 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
689 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
690 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
691 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
693 XPutPixel( data_image
, x
, y
, pixel
);
701 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
702 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
703 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
704 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
705 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
706 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
708 XPutPixel( data_image
, x
, y
, pixel
);
713 // I actually don't know if for 16-bit displays, it is alway the green
714 // component or the second component which has 6 bits.
718 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
719 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
720 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
721 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
722 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
723 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
725 XPutPixel( data_image
, x
, y
, pixel
);
734 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
735 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
736 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
737 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
738 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
739 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
741 XPutPixel( data_image
, x
, y
, pixel
);
750 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
751 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
752 XDestroyImage( data_image
);
753 XFreeGC( xdisplay
, gc
);
759 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
760 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
762 XDestroyImage( mask_image
);
763 XFreeGC( xdisplay
, gc
);
772 wxImage
wxBitmap::ConvertToImage() const
776 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
778 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
779 wxASSERT_MSG( xdisplay
, wxT("No display") );
782 //int bpp = DefaultDepth(xdisplay, xscreen);
783 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
787 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
788 XImage
*x_image
= NULL
;
791 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
793 GetWidth(), GetHeight(),
794 AllPlanes
, ZPixmap
);
798 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
800 GetWidth(), GetHeight(),
801 AllPlanes
, ZPixmap
);
804 wxFAIL_MSG( wxT("Ill-formed bitmap") );
807 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
809 image
.Create( GetWidth(), GetHeight() );
810 char unsigned *data
= image
.GetData();
814 XDestroyImage( x_image
);
815 wxFAIL_MSG( wxT("couldn't create image") );
819 XImage
*x_image_mask
= NULL
;
822 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
824 GetWidth(), GetHeight(),
825 AllPlanes
, ZPixmap
);
827 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
830 int red_shift_right
= 0;
831 int green_shift_right
= 0;
832 int blue_shift_right
= 0;
833 int red_shift_left
= 0;
834 int green_shift_left
= 0;
835 int blue_shift_left
= 0;
836 bool use_shift
= false;
840 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
842 red_shift_right
= vi
->m_visualRedShift
;
843 red_shift_left
= 8 - vi
->m_visualRedPrec
;
844 green_shift_right
= vi
->m_visualGreenShift
;
845 green_shift_left
= 8 - vi
->m_visualGreenPrec
;
846 blue_shift_right
= vi
->m_visualBlueShift
;
847 blue_shift_left
= 8 - vi
->m_visualBluePrec
;
849 use_shift
= (vi
->m_visualType
== GrayScale
) ||
850 (vi
->m_visualType
!= PseudoColor
);
858 XColor
*colors
= (XColor
*)wxTheApp
->
859 GetVisualInfo(M_BMPDATA
->m_display
)->m_visualColormap
;
861 int width
= GetWidth();
862 int height
= GetHeight();
864 for (int j
= 0; j
< height
; j
++)
866 for (int i
= 0; i
< width
; i
++)
868 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
886 data
[pos
] = (unsigned char)((pixel
>> red_shift_right
) << red_shift_left
);
887 data
[pos
+1] = (unsigned char)((pixel
>> green_shift_right
) << green_shift_left
);
888 data
[pos
+2] = (unsigned char)((pixel
>> blue_shift_right
) << blue_shift_left
);
892 data
[pos
] = (unsigned char)(colors
[pixel
].red
>> 8);
893 data
[pos
+1] = (unsigned char)(colors
[pixel
].green
>> 8);
894 data
[pos
+2] = (unsigned char)(colors
[pixel
].blue
>> 8);
898 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
903 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
916 XDestroyImage( x_image
);
917 if (x_image_mask
) XDestroyImage( x_image_mask
);
923 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
925 LoadFile( filename
, type
);
928 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
930 m_refData
= new wxBitmapRefData
;
932 (void) Create(bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
935 wxBitmap::~wxBitmap()
939 int wxBitmap::GetHeight() const
941 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
943 return M_BMPDATA
->m_height
;
946 int wxBitmap::GetWidth() const
948 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
950 return M_BMPDATA
->m_width
;
953 int wxBitmap::GetDepth() const
955 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
957 return M_BMPDATA
->m_bpp
;
960 wxMask
*wxBitmap::GetMask() const
962 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
964 return M_BMPDATA
->m_mask
;
967 void wxBitmap::SetMask( wxMask
*mask
)
969 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
972 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
974 M_BMPDATA
->m_mask
= mask
;
977 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
983 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
986 (rect
.x
>= 0) && (rect
.y
>= 0) &&
987 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
988 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
989 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
991 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
992 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
996 wxMask
* mask
= new wxMask();
997 mask
->SetDisplay( GetMask()->GetDisplay() );
998 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
999 GetMask()->GetBitmap(),
1001 rect
.width
, rect
.height
,
1004 ret
.SetMask( mask
);
1009 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
1012 rect
.width
, rect
.height
,
1013 M_BMPDATA
->m_bpp
) );
1018 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1021 rect
.width
, rect
.height
,
1028 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1029 const wxPalette
*palette
) const
1031 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1033 wxBitmapHandler
*handler
= FindHandler(type
);
1035 // Try to save the bitmap via wxImage handlers:
1036 if (handler
== NULL
)
1038 wxImage
image(this->ConvertToImage());
1039 if (image
.Ok()) return image
.SaveFile( name
, type
);
1044 return handler
->SaveFile(this, name
, type
, palette
);
1047 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1051 if (!wxFileExists(name
)) return false;
1053 wxBitmapHandler
*handler
= FindHandler(type
);
1055 if (handler
== NULL
)
1058 if (!image
.LoadFile( name
, type
))
1063 *this = wxBitmap(image
);
1069 return handler
->LoadFile(this, name
, type
, -1, -1);
1072 void wxBitmap::SetPalette(const wxPalette
& palette
)
1074 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1075 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1076 wxT("cannot set palette for bitmap of this depth"));
1079 delete M_BMPDATA
->m_palette
;
1080 M_BMPDATA
->m_palette
= NULL
;
1082 if (!palette
.Ok()) return;
1084 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1087 wxPalette
*wxBitmap::GetPalette() const
1089 if (!Ok()) return (wxPalette
*) NULL
;
1091 return M_BMPDATA
->m_palette
;
1094 void wxBitmap::SetHeight( int height
)
1098 M_BMPDATA
->m_height
= height
;
1101 void wxBitmap::SetWidth( int width
)
1105 M_BMPDATA
->m_width
= width
;
1108 void wxBitmap::SetDepth( int depth
)
1112 M_BMPDATA
->m_bpp
= depth
;
1115 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1117 if (!m_refData
) m_refData
= new wxBitmapRefData();
1119 M_BMPDATA
->m_pixmap
= (Pixmap
)pixmap
;
1122 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1124 if (!m_refData
) m_refData
= new wxBitmapRefData();
1126 M_BMPDATA
->m_bitmap
= (Pixmap
)bitmap
;
1129 WXPixmap
wxBitmap::GetPixmap() const
1131 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1133 return (WXPixmap
)M_BMPDATA
->m_pixmap
;
1136 WXPixmap
wxBitmap::GetBitmap() const
1138 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1140 return (WXPixmap
)M_BMPDATA
->m_bitmap
;
1143 WXPixmap
wxBitmap::GetDrawable() const
1145 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1147 return (WXPixmap
)(M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
1148 : M_BMPDATA
->m_pixmap
);
1151 WXDisplay
*wxBitmap::GetDisplay() const
1153 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1155 return M_BMPDATA
->m_display
;
1159 // Copy from the drawable to the wxImage
1160 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1162 GR_SCREEN_INFO sinfo
;
1164 GR_PIXELVAL
*pixels
;
1165 GR_PALETTE
* palette
= NULL
;
1166 unsigned char rgb
[3], *pp
;
1168 GrGetScreenInfo(&sinfo
);
1170 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1171 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1174 GrGetSystemPalette(palette
);
1177 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1182 image
.Create(width
, height
);
1184 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1188 for(x
= 0; x
< sinfo
.cols
; x
++) {
1190 pp
= (unsigned char *)pixels
+
1191 ((x
+ (y
* sinfo
.cols
)) *
1192 sizeof(GR_PIXELVAL
));
1194 switch(sinfo
.pixtype
) {
1195 /* FIXME: These may need modifying on big endian. */
1196 case MWPF_TRUECOLOR0888
:
1197 case MWPF_TRUECOLOR888
:
1203 rgb
[0] = palette
->palette
[pp
[0]].r
;
1204 rgb
[1] = palette
->palette
[pp
[0]].g
;
1205 rgb
[2] = palette
->palette
[pp
[0]].b
;
1207 case MWPF_TRUECOLOR565
:
1208 rgb
[0] = pp
[1] & 0xf8;
1209 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1210 ((pp
[0] & 0xe0) >> 3);
1211 rgb
[2] = (pp
[0] & 0x1f) << 3;
1213 case MWPF_TRUECOLOR555
:
1214 rgb
[0] = (pp
[1] & 0x7c) << 1;
1215 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1216 ((pp
[0] & 0xe0) >> 2);
1217 rgb
[2] = (pp
[0] & 0x1f) << 3;
1219 case MWPF_TRUECOLOR332
:
1220 rgb
[0] = pp
[0] & 0xe0;
1221 rgb
[1] = (pp
[0] & 0x1c) << 3;
1222 rgb
[2] = (pp
[0] & 0x03) << 6;
1225 fprintf(stderr
, "Unsupported pixel "
1230 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1235 if(palette
) free(palette
);
1241 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1242 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1244 unsigned char rgb
[3], *pp
;
1246 pp
= (unsigned char*) & pixel
;
1248 switch (sinfo
.pixtype
)
1250 /* FIXME: These may need modifying on big endian. */
1251 case MWPF_TRUECOLOR0888
:
1252 case MWPF_TRUECOLOR888
:
1258 rgb
[0] = palette
->palette
[pp
[0]].r
;
1259 rgb
[1] = palette
->palette
[pp
[0]].g
;
1260 rgb
[2] = palette
->palette
[pp
[0]].b
;
1262 case MWPF_TRUECOLOR565
:
1263 rgb
[0] = pp
[1] & 0xf8;
1264 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1265 ((pp
[0] & 0xe0) >> 3);
1266 rgb
[2] = (pp
[0] & 0x1f) << 3;
1268 case MWPF_TRUECOLOR555
:
1269 rgb
[0] = (pp
[1] & 0x7c) << 1;
1270 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1271 ((pp
[0] & 0xe0) >> 2);
1272 rgb
[2] = (pp
[0] & 0x1f) << 3;
1274 case MWPF_TRUECOLOR332
:
1275 rgb
[0] = pp
[0] & 0xe0;
1276 rgb
[1] = (pp
[0] & 0x1c) << 3;
1277 rgb
[2] = (pp
[0] & 0x03) << 6;
1280 fprintf(stderr
, "Unsupported pixel format\n");
1295 // ============================================================================
1297 // ============================================================================
1299 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1303 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1305 // ----------------------------------------------------------------------------
1307 // ----------------------------------------------------------------------------
1309 class wxXPMFileHandler
: public wxBitmapHandler
1311 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1315 SetName( wxT("XPM file") );
1316 SetExtension( wxT("xpm") );
1317 SetType( wxBITMAP_TYPE_XPM
);
1320 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1321 int desiredWidth
, int desiredHeight
);
1323 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1324 int type
, const wxPalette
*palette
= NULL
);
1326 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
), const void* WXUNUSED(data
), long WXUNUSED(flags
),
1327 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
) = 1)
1331 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1333 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1334 long WXUNUSED(flags
), int WXUNUSED(desiredWidth
),
1335 int WXUNUSED(desiredHeight
))
1338 if (!bitmap
->GetRefData())
1339 bitmap
->SetRefData( new wxBitmapRefData() );
1341 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1343 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1345 int xscreen
= DefaultScreen( xdisplay
);
1346 Window xroot
= RootWindow( xdisplay
, xscreen
);
1348 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1350 XpmAttributes xpmAttr
;
1351 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1356 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1357 (char*) ((const char*) name
.c_str()),
1358 &pixmap
, &mask
, &xpmAttr
);
1360 if (ErrorStatus
== XpmSuccess
)
1362 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1363 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1365 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1367 XpmFreeAttributes(&xpmAttr
);
1369 M_BMPHANDLERDATA
->m_bitmap
= (Pixmap
) pixmap
;
1373 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1374 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1375 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1387 wxXPMDecoder decoder
;
1388 wxFileInputStream
stream(name
);
1391 wxImage
image(decoder
.ReadFile(stream
));
1392 return image
.Ok() && bitmap
->CreateFromImage(image
);
1396 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1398 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1401 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1403 const wxPalette
*WXUNUSED(palette
))
1405 wxImage
image(bitmap
->ConvertToImage());
1406 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1411 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1413 // ----------------------------------------------------------------------------
1415 // ----------------------------------------------------------------------------
1417 class wxXPMDataHandler
: public wxBitmapHandler
1419 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1423 SetName( wxT("XPM data") );
1424 SetExtension( wxT("xpm") );
1425 SetType( wxBITMAP_TYPE_XPM_DATA
);
1428 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1429 const wxString
& WXUNUSED(name
),
1430 long WXUNUSED(flags
),
1431 int WXUNUSED(desiredWidth
),
1432 int WXUNUSED(desiredHeight
))
1435 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1436 const wxString
& WXUNUSED(name
),
1438 const wxPalette
*WXUNUSED(palette
) = NULL
)
1441 virtual bool Create(wxBitmap
*bitmap
, const void* data
, long flags
,
1442 int width
, int height
, int depth
= 1);
1445 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1447 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, const void* bits
,
1448 long WXUNUSED(flags
),
1449 int WXUNUSED(width
), int WXUNUSED(height
), int WXUNUSED(depth
))
1452 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
1454 if (!bitmap
->GetRefData())
1455 bitmap
->SetRefData( new wxBitmapRefData() );
1457 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1459 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1461 int xscreen
= DefaultScreen( xdisplay
);
1462 Window xroot
= RootWindow( xdisplay
, xscreen
);
1464 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1466 XpmAttributes xpmAttr
;
1467 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1472 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1473 &pixmap
, &mask
, &xpmAttr
);
1475 if (ErrorStatus
== XpmSuccess
)
1477 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1478 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1480 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1483 unsigned int depthRet
;
1485 unsigned int widthRet
, heightRet
, borderWidthRet
;
1486 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1487 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1489 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1492 XpmFreeAttributes(&xpmAttr
);
1494 M_BMPHANDLERDATA
->m_pixmap
= (Pixmap
) pixmap
;
1498 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1499 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1500 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1510 #else // !wxHAVE_LIB_XPM
1511 wxXPMDecoder decoder
;
1512 wxImage
image(decoder
.ReadData((const char **)bits
));
1513 return image
.Ok() && bitmap
->CreateFromImage(image
);
1514 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1519 // ----------------------------------------------------------------------------
1521 // ----------------------------------------------------------------------------
1523 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1525 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1527 inline wxXBMDataHandler()
1529 SetName( wxT("XBM data") );
1530 SetExtension( wxT("xbm") );
1531 SetType( wxBITMAP_TYPE_XBM_DATA
);
1534 virtual bool LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1535 const wxString
& WXUNUSED(name
),
1536 long WXUNUSED(flags
),
1537 int WXUNUSED(desiredWidth
),
1538 int WXUNUSED(desiredHeight
))
1541 virtual bool SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1542 const wxString
& WXUNUSED(name
),
1544 const wxPalette
*WXUNUSED(palette
) = NULL
)
1547 virtual bool Create(wxBitmap
*bitmap
, const void* data
, long flags
,
1548 int width
, int height
, int depth
= 1);
1551 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
)
1553 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, const void* bits
,
1554 long WXUNUSED(flags
),
1555 int width
, int height
, int WXUNUSED(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 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
);