1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart, Robert Roebling
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/bitmap.h"
18 #include "wx/dcmemory.h"
21 #include "wx/x11/private.h"
23 /* No point in using libXPM for NanoX */
26 #define wxHAVE_LIB_XPM 0
28 // Copy from the drawable to the wxImage
29 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
);
36 #include "wx/xpmdecod.h"
37 #include "wx/wfstream.h"
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
54 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
57 Create( bitmap
, colour
);
60 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
63 Create( bitmap
, paletteIndex
);
66 wxMask::wxMask( const wxBitmap
& bitmap
)
75 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
78 bool wxMask::Create( const wxBitmap
& bitmap
,
79 const wxColour
& colour
)
84 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
88 m_display
= bitmap
.GetDisplay();
90 wxImage image
= bitmap
.ConvertToImage();
91 if (!image
.Ok()) return FALSE
;
93 m_display
= bitmap
.GetDisplay();
95 Display
*xdisplay
= (Display
*) m_display
;
96 int xscreen
= DefaultScreen( xdisplay
);
97 Window xroot
= RootWindow( xdisplay
, xscreen
);
99 m_bitmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, image
.GetWidth(), image
.GetHeight(), 1 );
100 GC gc
= XCreateGC( xdisplay
, (Pixmap
) m_bitmap
, 0, NULL
);
102 XSetForeground( xdisplay
, gc
, WhitePixel(xdisplay
,xscreen
) );
103 XSetFillStyle( xdisplay
, gc
, FillSolid
);
104 XFillRectangle( xdisplay
, (Pixmap
) m_bitmap
, gc
, 0, 0, image
.GetWidth(), image
.GetHeight() );
106 unsigned char *data
= image
.GetData();
109 unsigned char red
= colour
.Red();
110 unsigned char green
= colour
.Green();
111 unsigned char blue
= colour
.Blue();
113 int bpp
= wxTheApp
->GetVisualInfo(m_display
)->m_visualDepth
;
118 green
= green
& 0xf8;
124 green
= green
& 0xfc;
130 green
= green
& 0xf0;
134 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
136 int width
= image
.GetWidth();
137 int height
= image
.GetHeight();
138 for (int j
= 0; j
< height
; j
++)
142 for (i
= 0; i
< width
; i
++)
144 if ((data
[index
] == red
) &&
145 (data
[index
+1] == green
) &&
146 (data
[index
+2] == blue
))
155 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
162 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
165 XFreeGC( xdisplay
, gc
);
174 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
177 wxPalette
*pal
= bitmap
.GetPalette();
179 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
181 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
183 return Create(bitmap
, wxColour(r
, g
, b
));
186 bool wxMask::Create( const wxBitmap
& bitmap
)
191 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
195 if (!bitmap
.Ok()) return FALSE
;
197 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
199 m_display
= bitmap
.GetDisplay();
201 int xscreen
= DefaultScreen( (Display
*) m_display
);
202 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
204 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
206 if (!m_bitmap
) return FALSE
;
208 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
210 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
211 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
213 XFreeGC( (Display
*) m_display
, gc
);
222 //-----------------------------------------------------------------------------
224 //-----------------------------------------------------------------------------
226 class wxBitmapRefData
: public wxObjectRefData
234 WXDisplay
*m_display
;
239 wxPalette
*m_palette
;
242 wxBitmapRefData::wxBitmapRefData()
247 m_mask
= (wxMask
*) NULL
;
251 m_palette
= (wxPalette
*) NULL
;
254 wxBitmapRefData::~wxBitmapRefData()
256 if (m_pixmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_pixmap
);
257 if (m_bitmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
258 if (m_mask
) delete m_mask
;
259 if (m_palette
) delete m_palette
;
262 //-----------------------------------------------------------------------------
266 static WXPixmap
wxGetSubPixmap( WXDisplay
* xdisplay
, WXPixmap xpixmap
,
267 int x
, int y
, int width
, int height
,
270 Display
* const dpy
= (Display
*)xdisplay
;
272 int xscreen
= DefaultScreen( dpy
);
273 Window xroot
= RootWindow( dpy
, xscreen
);
274 Visual
* xvisual
= DefaultVisual( dpy
, xscreen
);
276 XImage
* ximage
= XCreateImage( dpy
, xvisual
, depth
,
277 ZPixmap
, 0, 0, width
, height
, 32, 0 );
278 ximage
->data
= (char*)malloc( ximage
->bytes_per_line
* ximage
->height
);
279 ximage
= XGetSubImage( dpy
, (Pixmap
)xpixmap
,
281 AllPlanes
, ZPixmap
, ximage
, 0, 0 );
283 GC gc
= XCreateGC( dpy
, (Pixmap
)xpixmap
, 0, NULL
);
284 Pixmap ret
= XCreatePixmap( dpy
, xroot
,
285 width
, height
, depth
);
287 XPutImage( dpy
, ret
, gc
, ximage
,
288 0, 0, 0, 0, width
, height
);
289 XDestroyImage( ximage
);
292 return (WXPixmap
)ret
;
295 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
297 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
303 wxBitmap::wxBitmap( int width
, int height
, int depth
)
305 Create( width
, height
, depth
);
308 bool wxBitmap::Create( int width
, int height
, int depth
)
312 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
314 m_refData
= new wxBitmapRefData();
316 M_BMPDATA
->m_display
= wxGlobalDisplay();
318 wxASSERT_MSG( M_BMPDATA
->m_display
, wxT("No display") );
320 int xscreen
= DefaultScreen( (Display
*) M_BMPDATA
->m_display
);
321 Window xroot
= RootWindow( (Display
*) M_BMPDATA
->m_display
, xscreen
);
323 int bpp
= DefaultDepth( (Display
*) M_BMPDATA
->m_display
, xscreen
);
324 if (depth
== -1) depth
= bpp
;
326 wxCHECK_MSG( (depth
== bpp
) ||
327 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
329 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
330 M_BMPDATA
->m_width
= width
;
331 M_BMPDATA
->m_height
= height
;
334 M_BMPDATA
->m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
335 M_BMPDATA
->m_bpp
= bpp
;
337 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Bitmap creation failed") );
341 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, 1 );
343 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
345 M_BMPDATA
->m_bpp
= 1;
349 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, depth
);
351 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Pixmap creation failed") );
353 M_BMPDATA
->m_bpp
= depth
;
359 bool wxBitmap::Create(void *data
, wxBitmapType type
,
360 int width
, int height
, int depth
)
364 wxBitmapHandler
*handler
= FindHandler(type
);
366 if ( handler
== NULL
) {
367 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
373 return handler
->Create(this, data
, type
, width
, height
, depth
);
376 bool wxBitmap::Create(WXPixmap pixmap
)
379 Pixmap xpixmap
= (Pixmap
)pixmap
;
380 Display
* xdisplay
= wxGlobalDisplay();
381 int xscreen
= DefaultScreen( xdisplay
);
382 Window xroot
= RootWindow( xdisplay
, xscreen
);
384 // make a copy of the Pixmap
388 unsigned width
, height
, border
, depth
;
390 XGetGeometry( xdisplay
, (Drawable
)xpixmap
, &root
, &x
, &y
,
391 &width
, &height
, &border
, &depth
);
392 copy
= XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
394 GC gc
= XCreateGC( xdisplay
, copy
, 0, NULL
);
395 XCopyArea( xdisplay
, xpixmap
, copy
, gc
, 0, 0, width
, height
, 0, 0 );
396 XFreeGC( xdisplay
, gc
);
399 wxBitmapRefData
* ref
= new wxBitmapRefData();
402 ref
->m_bitmap
= (WXPixmap
)copy
;
404 ref
->m_pixmap
= (WXPixmap
)copy
;
406 ref
->m_display
= (WXDisplay
*)xdisplay
;
407 ref
->m_width
= width
;
408 ref
->m_height
= height
;
416 bool wxBitmap::CreateFromXpm( const char **bits
)
418 wxCHECK_MSG( bits
, FALSE
, wxT("NULL pointer in wxBitmap::CreateFromXpm") );
420 return Create(bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
423 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
428 wxASSERT_MSG(image
.Ok(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
432 int w
= image
.GetWidth();
433 int h
= image
.GetHeight();
435 if (!Create(w
, h
, depth
))
438 // Unfortunately the mask has to be screen-depth since
439 // 1-bpp bitmaps don't seem to be supported
440 // TODO: implement transparent drawing, presumably
441 // by doing several blits as per the Windows
442 // implementation because Nano-X doesn't support
444 // TODO: could perhaps speed this function up
445 // by making a buffer of pixel values,
446 // and then calling GrArea to write that to the
447 // pixmap. See demos/nxroach.c.
449 bool hasMask
= image
.HasMask();
451 GC pixmapGC
= GrNewGC();
452 Pixmap pixmap
= (Pixmap
) GetPixmap();
455 Pixmap maskPixmap
= 0;
457 unsigned char maskR
= 0;
458 unsigned char maskG
= 0;
459 unsigned char maskB
= 0;
463 maskR
= image
.GetMaskRed();
464 maskG
= image
.GetMaskGreen();
465 maskB
= image
.GetMaskBlue();
468 maskPixmap
= GrNewPixmap(w
, h
, 0);
473 wxMask
* mask
= new wxMask
;
474 mask
->SetBitmap((WXPixmap
) maskPixmap
);
479 GR_COLOR lastPixmapColour
= 0;
480 GR_COLOR lastMaskColour
= 0;
483 for (i
= 0; i
< w
; i
++)
485 for (j
= 0; j
< h
; j
++)
487 unsigned char red
= image
.GetRed(i
, j
);
488 unsigned char green
= image
.GetGreen(i
, j
);
489 unsigned char blue
= image
.GetBlue(i
, j
);
491 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
493 // Efficiency measure
494 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
496 GrSetGCForeground(pixmapGC
, colour
);
497 lastPixmapColour
= colour
;
500 GrPoint(pixmap
, pixmapGC
, i
, j
);
504 // scan the bitmap for the transparent colour and set the corresponding
505 // pixels in the mask to BLACK and the rest to WHITE
506 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
508 colour
= GR_RGB(0, 0, 0);
512 colour
= GR_RGB(255, 255, 255);
514 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
516 GrSetGCForeground(maskGC
, colour
);
517 lastMaskColour
= colour
;
519 GrPoint(maskPixmap
, maskGC
, i
, j
);
524 GrDestroyGC(pixmapGC
);
534 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
535 wxCHECK_MSG( depth
== -1, FALSE
, wxT("invalid bitmap depth") )
537 m_refData
= new wxBitmapRefData();
539 M_BMPDATA
->m_display
= wxGlobalDisplay();
541 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
543 int xscreen
= DefaultScreen( xdisplay
);
544 Window xroot
= RootWindow( xdisplay
, xscreen
);
545 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
547 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
549 int width
= image
.GetWidth();
550 int height
= image
.GetHeight();
551 M_BMPDATA
->m_width
= width
;
552 M_BMPDATA
->m_height
= height
;
554 if (depth
!= 1) depth
= bpp
;
555 M_BMPDATA
->m_bpp
= depth
;
559 wxFAIL_MSG( wxT("mono images later") );
565 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
566 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
568 if (data_image
->data
== NULL
)
570 wxLogError( wxT("Out of memory.") ); // TODO clean
574 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
578 XImage
*mask_image
= (XImage
*) NULL
;
581 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
582 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
584 if (mask_image
->data
== NULL
)
586 wxLogError( wxT("Out of memory.") ); // TODO clean
590 wxMask
*mask
= new wxMask();
591 mask
->SetDisplay( xdisplay
);
592 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
597 if (bpp
< 8) bpp
= 8;
601 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
602 byte_order b_o
= RGB
;
604 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
605 unsigned long greenMask
= vi
->m_visualGreenMask
,
606 redMask
= vi
->m_visualRedMask
,
607 blueMask
= vi
->m_visualBlueMask
;
611 if ((redMask
> greenMask
) && (greenMask
> blueMask
)) b_o
= RGB
;
612 else if ((redMask
> blueMask
) && (blueMask
> greenMask
)) b_o
= RBG
;
613 else if ((blueMask
> redMask
) && (redMask
> greenMask
)) b_o
= BRG
;
614 else if ((blueMask
> greenMask
) && (greenMask
> redMask
))b_o
= BGR
;
615 else if ((greenMask
> redMask
) && (redMask
> blueMask
)) b_o
= GRB
;
616 else if ((greenMask
> blueMask
) && (blueMask
> redMask
)) b_o
= GBR
;
619 int r_mask
= image
.GetMaskRed();
620 int g_mask
= image
.GetMaskGreen();
621 int b_mask
= image
.GetMaskBlue();
623 unsigned char* data
= image
.GetData();
624 wxASSERT_MSG( data
, wxT("No image data") );
626 unsigned char *colorCube
=
627 wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_colorCube
;
629 bool hasMask
= image
.HasMask();
632 for (int y
= 0; y
< height
; y
++)
634 for (int x
= 0; x
< width
; x
++)
645 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
646 XPutPixel( mask_image
, x
, y
, 0 );
648 XPutPixel( mask_image
, x
, y
, 1 );
656 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
657 XPutPixel( data_image
, x
, y
, pixel
);
665 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
666 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
667 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
668 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
669 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
670 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
672 XPutPixel( data_image
, x
, y
, pixel
);
680 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
681 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
682 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
683 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
684 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
685 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
687 XPutPixel( data_image
, x
, y
, pixel
);
692 // I actually don't know if for 16-bit displays, it is alway the green
693 // component or the second component which has 6 bits.
697 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
698 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
699 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
700 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
701 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
702 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
704 XPutPixel( data_image
, x
, y
, pixel
);
713 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
714 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
715 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
716 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
717 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
718 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
720 XPutPixel( data_image
, x
, y
, pixel
);
729 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
730 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
731 XDestroyImage( data_image
);
732 XFreeGC( xdisplay
, gc
);
738 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
739 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
741 XDestroyImage( mask_image
);
742 XFreeGC( xdisplay
, gc
);
751 wxImage
wxBitmap::ConvertToImage() const
755 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
757 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
758 wxASSERT_MSG( xdisplay
, wxT("No display") );
761 //int bpp = DefaultDepth(xdisplay, xscreen);
762 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
766 int bpp
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
)->m_visualDepth
;
767 XImage
*x_image
= NULL
;
770 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
772 GetWidth(), GetHeight(),
773 AllPlanes
, ZPixmap
);
777 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
779 GetWidth(), GetHeight(),
780 AllPlanes
, ZPixmap
);
783 wxFAIL_MSG( wxT("Ill-formed bitmap") );
786 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
788 image
.Create( GetWidth(), GetHeight() );
789 char unsigned *data
= image
.GetData();
793 XDestroyImage( x_image
);
794 wxFAIL_MSG( wxT("couldn't create image") );
798 XImage
*x_image_mask
= NULL
;
801 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
803 GetWidth(), GetHeight(),
804 AllPlanes
, ZPixmap
);
806 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
809 int red_shift_right
= 0;
810 int green_shift_right
= 0;
811 int blue_shift_right
= 0;
812 int red_shift_left
= 0;
813 int green_shift_left
= 0;
814 int blue_shift_left
= 0;
815 bool use_shift
= FALSE
;
819 wxXVisualInfo
* vi
= wxTheApp
->GetVisualInfo(M_BMPDATA
->m_display
);
821 red_shift_right
= vi
->m_visualRedShift
;
822 red_shift_left
= 8 - vi
->m_visualRedPrec
;
823 green_shift_right
= vi
->m_visualGreenShift
;
824 green_shift_left
= 8 - vi
->m_visualGreenPrec
;
825 blue_shift_right
= vi
->m_visualBlueShift
;
826 blue_shift_left
= 8 - vi
->m_visualBluePrec
;
828 use_shift
= (vi
->m_visualType
== GrayScale
) ||
829 (vi
->m_visualType
!= PseudoColor
);
837 XColor
*colors
= (XColor
*)wxTheApp
->
838 GetVisualInfo(M_BMPDATA
->m_display
)->m_visualColormap
;
840 int width
= GetWidth();
841 int height
= GetHeight();
843 for (int j
= 0; j
< height
; j
++)
845 for (int i
= 0; i
< width
; i
++)
847 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
865 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
866 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
867 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
871 data
[pos
] = colors
[pixel
].red
>> 8;
872 data
[pos
+1] = colors
[pixel
].green
>> 8;
873 data
[pos
+2] = colors
[pixel
].blue
>> 8;
877 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
882 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
895 XDestroyImage( x_image
);
896 if (x_image_mask
) XDestroyImage( x_image_mask
);
902 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
907 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
909 LoadFile( filename
, type
);
912 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
914 m_refData
= new wxBitmapRefData
;
916 (void) Create((void*) bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
919 wxBitmap::~wxBitmap()
923 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
925 if ( m_refData
!= bmp
.m_refData
)
931 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
933 return m_refData
== bmp
.m_refData
;
936 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
938 return m_refData
!= bmp
.m_refData
;
941 bool wxBitmap::Ok() const
943 return (m_refData
!= NULL
);
946 int wxBitmap::GetHeight() const
948 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
950 return M_BMPDATA
->m_height
;
953 int wxBitmap::GetWidth() const
955 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
957 return M_BMPDATA
->m_width
;
960 int wxBitmap::GetDepth() const
962 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
964 return M_BMPDATA
->m_bpp
;
967 wxMask
*wxBitmap::GetMask() const
969 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
971 return M_BMPDATA
->m_mask
;
974 void wxBitmap::SetMask( wxMask
*mask
)
976 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
978 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
980 M_BMPDATA
->m_mask
= mask
;
983 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
989 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
992 (rect
.x
>= 0) && (rect
.y
>= 0) &&
993 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
994 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
995 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
997 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
998 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1002 wxMask
* mask
= new wxMask();
1003 mask
->SetDisplay( GetMask()->GetDisplay() );
1004 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
1005 GetMask()->GetBitmap(),
1007 rect
.width
, rect
.height
,
1010 ret
.SetMask( mask
);
1015 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
1018 rect
.width
, rect
.height
,
1019 M_BMPDATA
->m_bpp
) );
1024 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
1027 rect
.width
, rect
.height
,
1034 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
1035 const wxPalette
*palette
) const
1037 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1039 wxBitmapHandler
*handler
= FindHandler(type
);
1041 // Try to save the bitmap via wxImage handlers:
1042 if (handler
== NULL
)
1044 wxImage
image(this->ConvertToImage());
1045 if (image
.Ok()) return image
.SaveFile( name
, type
);
1050 return handler
->SaveFile(this, name
, type
, palette
);
1053 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1057 if (!wxFileExists(name
)) return FALSE
;
1059 wxBitmapHandler
*handler
= FindHandler(type
);
1061 if (handler
== NULL
)
1064 if (!image
.LoadFile( name
, type
))
1069 *this = wxBitmap(image
);
1075 return handler
->LoadFile(this, name
, type
, -1, -1);
1078 void wxBitmap::SetPalette(const wxPalette
& palette
)
1080 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1081 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1082 wxT("cannot set palette for bitmap of this depth"));
1084 delete M_BMPDATA
->m_palette
;
1085 M_BMPDATA
->m_palette
= NULL
;
1087 if (!palette
.Ok()) return;
1089 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1092 wxPalette
*wxBitmap::GetPalette() const
1094 if (!Ok()) return (wxPalette
*) NULL
;
1096 return M_BMPDATA
->m_palette
;
1099 void wxBitmap::SetHeight( int height
)
1101 if (!m_refData
) m_refData
= new wxBitmapRefData();
1103 M_BMPDATA
->m_height
= height
;
1106 void wxBitmap::SetWidth( int width
)
1108 if (!m_refData
) m_refData
= new wxBitmapRefData();
1110 M_BMPDATA
->m_width
= width
;
1113 void wxBitmap::SetDepth( int depth
)
1115 if (!m_refData
) m_refData
= new wxBitmapRefData();
1117 M_BMPDATA
->m_bpp
= depth
;
1120 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1122 if (!m_refData
) m_refData
= new wxBitmapRefData();
1124 M_BMPDATA
->m_pixmap
= pixmap
;
1127 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1129 if (!m_refData
) m_refData
= new wxBitmapRefData();
1131 M_BMPDATA
->m_bitmap
= bitmap
;
1134 WXPixmap
wxBitmap::GetPixmap() const
1136 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1138 return M_BMPDATA
->m_pixmap
;
1141 WXPixmap
wxBitmap::GetBitmap() const
1143 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1145 return M_BMPDATA
->m_bitmap
;
1148 WXPixmap
wxBitmap::GetDrawable() const
1150 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1152 return M_BMPDATA
->m_bpp
== 1 ? M_BMPDATA
->m_bitmap
: M_BMPDATA
->m_pixmap
;
1155 WXDisplay
*wxBitmap::GetDisplay() const
1157 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1159 return M_BMPDATA
->m_display
;
1163 // Copy from the drawable to the wxImage
1164 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1166 GR_SCREEN_INFO sinfo
;
1168 GR_PIXELVAL
*pixels
;
1169 GR_PALETTE
* palette
= NULL
;
1170 unsigned char rgb
[3], *pp
;
1172 GrGetScreenInfo(&sinfo
);
1174 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1175 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1178 GrGetSystemPalette(palette
);
1181 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1186 image
.Create(width
, height
);
1188 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1192 for(x
= 0; x
< sinfo
.cols
; x
++) {
1194 pp
= (unsigned char *)pixels
+
1195 ((x
+ (y
* sinfo
.cols
)) *
1196 sizeof(GR_PIXELVAL
));
1198 switch(sinfo
.pixtype
) {
1199 /* FIXME: These may need modifying on big endian. */
1200 case MWPF_TRUECOLOR0888
:
1201 case MWPF_TRUECOLOR888
:
1207 rgb
[0] = palette
->palette
[pp
[0]].r
;
1208 rgb
[1] = palette
->palette
[pp
[0]].g
;
1209 rgb
[2] = palette
->palette
[pp
[0]].b
;
1211 case MWPF_TRUECOLOR565
:
1212 rgb
[0] = pp
[1] & 0xf8;
1213 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1214 ((pp
[0] & 0xe0) >> 3);
1215 rgb
[2] = (pp
[0] & 0x1f) << 3;
1217 case MWPF_TRUECOLOR555
:
1218 rgb
[0] = (pp
[1] & 0x7c) << 1;
1219 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1220 ((pp
[0] & 0xe0) >> 2);
1221 rgb
[2] = (pp
[0] & 0x1f) << 3;
1223 case MWPF_TRUECOLOR332
:
1224 rgb
[0] = pp
[0] & 0xe0;
1225 rgb
[1] = (pp
[0] & 0x1c) << 3;
1226 rgb
[2] = (pp
[0] & 0x03) << 6;
1229 fprintf(stderr
, "Unsupported pixel "
1234 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1239 if(palette
) free(palette
);
1245 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1246 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1248 unsigned char rgb
[3], *pp
;
1250 pp
= (unsigned char*) & pixel
;
1252 switch (sinfo
.pixtype
)
1254 /* FIXME: These may need modifying on big endian. */
1255 case MWPF_TRUECOLOR0888
:
1256 case MWPF_TRUECOLOR888
:
1262 rgb
[0] = palette
->palette
[pp
[0]].r
;
1263 rgb
[1] = palette
->palette
[pp
[0]].g
;
1264 rgb
[2] = palette
->palette
[pp
[0]].b
;
1266 case MWPF_TRUECOLOR565
:
1267 rgb
[0] = pp
[1] & 0xf8;
1268 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1269 ((pp
[0] & 0xe0) >> 3);
1270 rgb
[2] = (pp
[0] & 0x1f) << 3;
1272 case MWPF_TRUECOLOR555
:
1273 rgb
[0] = (pp
[1] & 0x7c) << 1;
1274 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1275 ((pp
[0] & 0xe0) >> 2);
1276 rgb
[2] = (pp
[0] & 0x1f) << 3;
1278 case MWPF_TRUECOLOR332
:
1279 rgb
[0] = pp
[0] & 0xe0;
1280 rgb
[1] = (pp
[0] & 0x1c) << 3;
1281 rgb
[2] = (pp
[0] & 0x03) << 6;
1284 fprintf(stderr
, "Unsupported pixel format\n");
1299 // ============================================================================
1301 // ============================================================================
1303 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
);
1305 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1309 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1311 // ----------------------------------------------------------------------------
1313 // ----------------------------------------------------------------------------
1315 class wxXPMFileHandler
: public wxBitmapHandler
1317 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1321 SetName( wxT("XPM file") );
1322 SetExtension( wxT("xpm") );
1323 SetType( wxBITMAP_TYPE_XPM
);
1326 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1327 int desiredWidth
, int desiredHeight
);
1329 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1330 int type
, const wxPalette
*palette
= NULL
);
1332 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1333 int width
, int height
, int depth
= 1)
1337 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
);
1339 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1340 long flags
, int desiredWidth
,
1344 if (!bitmap
->GetRefData())
1345 bitmap
->SetRefData( new wxBitmapRefData() );
1347 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1349 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1351 int xscreen
= DefaultScreen( xdisplay
);
1352 Window xroot
= RootWindow( xdisplay
, xscreen
);
1354 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1356 XpmAttributes xpmAttr
;
1357 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1362 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1363 (char*) name
.c_str(),
1364 &pixmap
, &mask
, &xpmAttr
);
1366 if (ErrorStatus
== XpmSuccess
)
1368 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1369 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1371 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1373 XpmFreeAttributes(&xpmAttr
);
1375 M_BMPHANDLERDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1379 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1380 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1381 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1393 wxXPMDecoder decoder
;
1394 wxFileInputStream
stream(name
);
1397 wxImage
image(decoder
.ReadFile(stream
));
1398 return image
.Ok() && bitmap
->CreateFromImage(image
);
1402 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1404 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1407 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1409 const wxPalette
*WXUNUSED(palette
))
1411 wxImage
image(bitmap
->ConvertToImage());
1412 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1417 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1419 // ----------------------------------------------------------------------------
1421 // ----------------------------------------------------------------------------
1423 class wxXPMDataHandler
: public wxBitmapHandler
1425 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1429 SetName( wxT("XPM data") );
1430 SetExtension( wxT("xpm") );
1431 SetType( wxBITMAP_TYPE_XPM_DATA
);
1434 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1435 int desiredWidth
, int desiredHeight
)
1438 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1439 int type
, const wxPalette
*palette
= NULL
)
1442 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1443 int width
, int height
, int depth
= 1);
1446 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
);
1448 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *bits
,
1449 long WXUNUSED(flags
),
1450 int width
, int height
, int WXUNUSED(depth
))
1453 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
1455 if (!bitmap
->GetRefData())
1456 bitmap
->SetRefData( new wxBitmapRefData() );
1458 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1460 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1462 int xscreen
= DefaultScreen( xdisplay
);
1463 Window xroot
= RootWindow( xdisplay
, xscreen
);
1465 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1467 XpmAttributes xpmAttr
;
1468 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1473 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1474 &pixmap
, &mask
, &xpmAttr
);
1476 if (ErrorStatus
== XpmSuccess
)
1478 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1479 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1481 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1484 unsigned int depthRet
;
1486 unsigned int widthRet
, heightRet
, borderWidthRet
;
1487 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1488 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1490 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1493 XpmFreeAttributes(&xpmAttr
);
1495 M_BMPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
1499 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1500 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1501 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1511 #else // !wxHAVE_LIB_XPM
1512 wxXPMDecoder decoder
;
1513 wxImage
image(decoder
.ReadData((const char **)bits
));
1514 return image
.Ok() && bitmap
->CreateFromImage(image
);
1515 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1520 // ----------------------------------------------------------------------------
1522 // ----------------------------------------------------------------------------
1524 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1526 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1528 inline wxXBMDataHandler()
1530 SetName( wxT("XBM data") );
1531 SetExtension( wxT("xbm") );
1532 SetType( wxBITMAP_TYPE_XBM_DATA
);
1535 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1536 int desiredWidth
, int desiredHeight
)
1539 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1540 int type
, const wxPalette
*palette
= NULL
)
1543 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1544 int width
, int height
, int depth
= 1);
1547 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
);
1549 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, void *bits
,
1550 long WXUNUSED(flags
),
1551 int width
, int height
, int depth
)
1554 if (!bitmap
->GetRefData())
1555 bitmap
->SetRefData( new wxBitmapRefData() );
1557 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1559 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1561 int xscreen
= DefaultScreen( xdisplay
);
1562 Window xroot
= RootWindow( xdisplay
, xscreen
);
1564 M_BMPHANDLERDATA
->m_mask
= (wxMask
*) NULL
;
1565 M_BMPHANDLERDATA
->m_bitmap
=
1566 (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
,
1567 (char *) bits
, width
, height
);
1568 M_BMPHANDLERDATA
->m_width
= width
;
1569 M_BMPHANDLERDATA
->m_height
= height
;
1570 M_BMPHANDLERDATA
->m_bpp
= 1;
1574 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, FALSE
,
1575 wxT("couldn't create bitmap") );
1579 void wxBitmap::InitStandardHandlers()
1581 AddHandler(new wxXBMDataHandler
);
1583 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1584 AddHandler(new wxXPMFileHandler
);
1586 AddHandler(new wxXPMDataHandler
);