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
->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
->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
;
568 if ((wxTheApp
->m_visualRedMask
> wxTheApp
->m_visualGreenMask
) && (wxTheApp
->m_visualGreenMask
> wxTheApp
->m_visualBlueMask
)) b_o
= RGB
;
569 else if ((wxTheApp
->m_visualRedMask
> wxTheApp
->m_visualBlueMask
) && (wxTheApp
->m_visualBlueMask
> wxTheApp
->m_visualGreenMask
)) b_o
= RBG
;
570 else if ((wxTheApp
->m_visualBlueMask
> wxTheApp
->m_visualRedMask
) && (wxTheApp
->m_visualRedMask
> wxTheApp
->m_visualGreenMask
)) b_o
= BRG
;
571 else if ((wxTheApp
->m_visualBlueMask
> wxTheApp
->m_visualGreenMask
) && (wxTheApp
->m_visualGreenMask
> wxTheApp
->m_visualRedMask
)) b_o
= BGR
;
572 else if ((wxTheApp
->m_visualGreenMask
> wxTheApp
->m_visualRedMask
) && (wxTheApp
->m_visualRedMask
> wxTheApp
->m_visualBlueMask
)) b_o
= GRB
;
573 else if ((wxTheApp
->m_visualGreenMask
> wxTheApp
->m_visualBlueMask
) && (wxTheApp
->m_visualBlueMask
> wxTheApp
->m_visualRedMask
)) b_o
= GBR
;
576 int r_mask
= image
.GetMaskRed();
577 int g_mask
= image
.GetMaskGreen();
578 int b_mask
= image
.GetMaskBlue();
580 unsigned char* data
= image
.GetData();
581 wxASSERT_MSG( data
, "No image data" );
583 unsigned char *colorCube
= wxTheApp
->m_colorCube
;
585 bool hasMask
= image
.HasMask();
588 for (int y
= 0; y
< height
; y
++)
590 for (int x
= 0; x
< width
; x
++)
601 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
602 XPutPixel( mask_image
, x
, y
, 0 );
604 XPutPixel( mask_image
, x
, y
, 1 );
612 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
613 XPutPixel( data_image
, x
, y
, pixel
);
621 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
622 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
623 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
624 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
625 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
626 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
628 XPutPixel( data_image
, x
, y
, pixel
);
636 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
637 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
638 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
639 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
640 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
641 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
643 XPutPixel( data_image
, x
, y
, pixel
);
648 // I actually don't know if for 16-bit displays, it is alway the green
649 // component or the second component which has 6 bits.
653 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
654 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
655 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
656 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
657 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
658 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
660 XPutPixel( data_image
, x
, y
, pixel
);
669 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
670 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
671 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
672 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
673 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
674 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
676 XPutPixel( data_image
, x
, y
, pixel
);
685 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
686 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
687 XDestroyImage( data_image
);
688 XFreeGC( xdisplay
, gc
);
694 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
695 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
697 XDestroyImage( mask_image
);
698 XFreeGC( xdisplay
, gc
);
707 wxImage
wxBitmap::ConvertToImage() const
711 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
713 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
714 wxASSERT_MSG( xdisplay
, wxT("No display") );
717 //int bpp = DefaultDepth(xdisplay, xscreen);
718 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
722 int bpp
= wxTheApp
->m_visualDepth
;
723 XImage
*x_image
= NULL
;
726 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
728 GetWidth(), GetHeight(),
729 AllPlanes
, ZPixmap
);
733 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
735 GetWidth(), GetHeight(),
736 AllPlanes
, ZPixmap
);
739 wxFAIL_MSG( wxT("Ill-formed bitmap") );
742 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
744 image
.Create( GetWidth(), GetHeight() );
745 char unsigned *data
= image
.GetData();
749 XDestroyImage( x_image
);
750 wxFAIL_MSG( wxT("couldn't create image") );
754 XImage
*x_image_mask
= NULL
;
757 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
759 GetWidth(), GetHeight(),
760 AllPlanes
, ZPixmap
);
762 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
765 int red_shift_right
= 0;
766 int green_shift_right
= 0;
767 int blue_shift_right
= 0;
768 int red_shift_left
= 0;
769 int green_shift_left
= 0;
770 int blue_shift_left
= 0;
771 bool use_shift
= FALSE
;
775 red_shift_right
= wxTheApp
->m_visualRedShift
;
776 red_shift_left
= 8-wxTheApp
->m_visualRedPrec
;
777 green_shift_right
= wxTheApp
->m_visualGreenShift
;
778 green_shift_left
= 8-wxTheApp
->m_visualGreenPrec
;
779 blue_shift_right
= wxTheApp
->m_visualBlueShift
;
780 blue_shift_left
= 8-wxTheApp
->m_visualBluePrec
;
782 use_shift
= (wxTheApp
->m_visualType
== GrayScale
) || (wxTheApp
->m_visualType
!= PseudoColor
);
790 XColor
*colors
= (XColor
*) wxTheApp
->m_visualColormap
;
792 int width
= GetWidth();
793 int height
= GetHeight();
795 for (int j
= 0; j
< height
; j
++)
797 for (int i
= 0; i
< width
; i
++)
799 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
817 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
818 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
819 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
823 data
[pos
] = colors
[pixel
].red
>> 8;
824 data
[pos
+1] = colors
[pixel
].green
>> 8;
825 data
[pos
+2] = colors
[pixel
].blue
>> 8;
829 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
834 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
847 XDestroyImage( x_image
);
848 if (x_image_mask
) XDestroyImage( x_image_mask
);
854 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
859 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
861 LoadFile( filename
, type
);
864 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int depth
)
866 m_refData
= new wxBitmapRefData
;
868 (void) Create((void*) bits
, wxBITMAP_TYPE_XBM_DATA
, width
, height
, depth
);
871 wxBitmap::~wxBitmap()
875 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
877 if ( m_refData
!= bmp
.m_refData
)
883 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
885 return m_refData
== bmp
.m_refData
;
888 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
890 return m_refData
!= bmp
.m_refData
;
893 bool wxBitmap::Ok() const
895 return (m_refData
!= NULL
);
898 int wxBitmap::GetHeight() const
900 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
902 return M_BMPDATA
->m_height
;
905 int wxBitmap::GetWidth() const
907 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
909 return M_BMPDATA
->m_width
;
912 int wxBitmap::GetDepth() const
914 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
916 return M_BMPDATA
->m_bpp
;
919 wxMask
*wxBitmap::GetMask() const
921 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
923 return M_BMPDATA
->m_mask
;
926 void wxBitmap::SetMask( wxMask
*mask
)
928 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
930 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
932 M_BMPDATA
->m_mask
= mask
;
935 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
941 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
944 (rect
.x
>= 0) && (rect
.y
>= 0) &&
945 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) &&
946 (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
947 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
949 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
950 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
954 wxMask
* mask
= new wxMask();
955 mask
->SetDisplay( GetMask()->GetDisplay() );
956 mask
->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
957 GetMask()->GetBitmap(),
959 rect
.width
, rect
.height
,
967 ret
.SetPixmap( wxGetSubPixmap( GetDisplay(),
970 rect
.width
, rect
.height
,
971 M_BMPDATA
->m_bpp
) );
976 ret
.SetBitmap( wxGetSubPixmap( GetDisplay(),
979 rect
.width
, rect
.height
,
986 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
,
987 const wxPalette
*palette
) const
989 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
991 wxBitmapHandler
*handler
= FindHandler(type
);
993 // Try to save the bitmap via wxImage handlers:
996 wxImage
image(this->ConvertToImage());
997 if (image
.Ok()) return image
.SaveFile( name
, type
);
1002 return handler
->SaveFile(this, name
, type
, palette
);
1005 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1009 if (!wxFileExists(name
)) return FALSE
;
1011 wxBitmapHandler
*handler
= FindHandler(type
);
1013 if (handler
== NULL
)
1016 if (!image
.LoadFile( name
, type
))
1021 *this = wxBitmap(image
);
1027 return handler
->LoadFile(this, name
, type
, -1, -1);
1030 void wxBitmap::SetPalette(const wxPalette
& palette
)
1032 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1033 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1034 wxT("cannot set palette for bitmap of this depth"));
1036 delete M_BMPDATA
->m_palette
;
1037 M_BMPDATA
->m_palette
= NULL
;
1039 if (!palette
.Ok()) return;
1041 M_BMPDATA
->m_palette
= new wxPalette(palette
);
1044 wxPalette
*wxBitmap::GetPalette() const
1046 if (!Ok()) return (wxPalette
*) NULL
;
1048 return M_BMPDATA
->m_palette
;
1051 void wxBitmap::SetHeight( int height
)
1053 if (!m_refData
) m_refData
= new wxBitmapRefData();
1055 M_BMPDATA
->m_height
= height
;
1058 void wxBitmap::SetWidth( int width
)
1060 if (!m_refData
) m_refData
= new wxBitmapRefData();
1062 M_BMPDATA
->m_width
= width
;
1065 void wxBitmap::SetDepth( int depth
)
1067 if (!m_refData
) m_refData
= new wxBitmapRefData();
1069 M_BMPDATA
->m_bpp
= depth
;
1072 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1074 if (!m_refData
) m_refData
= new wxBitmapRefData();
1076 M_BMPDATA
->m_pixmap
= pixmap
;
1079 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1081 if (!m_refData
) m_refData
= new wxBitmapRefData();
1083 M_BMPDATA
->m_bitmap
= bitmap
;
1086 WXPixmap
wxBitmap::GetPixmap() const
1088 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1090 return M_BMPDATA
->m_pixmap
;
1093 WXPixmap
wxBitmap::GetBitmap() const
1095 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1097 return M_BMPDATA
->m_bitmap
;
1100 WXDisplay
*wxBitmap::GetDisplay() const
1102 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1104 return M_BMPDATA
->m_display
;
1108 // Copy from the drawable to the wxImage
1109 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1111 GR_SCREEN_INFO sinfo
;
1113 GR_PIXELVAL
*pixels
;
1114 GR_PALETTE
* palette
= NULL
;
1115 unsigned char rgb
[3], *pp
;
1117 GrGetScreenInfo(&sinfo
);
1119 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1120 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1123 GrGetSystemPalette(palette
);
1126 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1131 image
.Create(width
, height
);
1133 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1137 for(x
= 0; x
< sinfo
.cols
; x
++) {
1139 pp
= (unsigned char *)pixels
+
1140 ((x
+ (y
* sinfo
.cols
)) *
1141 sizeof(GR_PIXELVAL
));
1143 switch(sinfo
.pixtype
) {
1144 /* FIXME: These may need modifying on big endian. */
1145 case MWPF_TRUECOLOR0888
:
1146 case MWPF_TRUECOLOR888
:
1152 rgb
[0] = palette
->palette
[pp
[0]].r
;
1153 rgb
[1] = palette
->palette
[pp
[0]].g
;
1154 rgb
[2] = palette
->palette
[pp
[0]].b
;
1156 case MWPF_TRUECOLOR565
:
1157 rgb
[0] = pp
[1] & 0xf8;
1158 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1159 ((pp
[0] & 0xe0) >> 3);
1160 rgb
[2] = (pp
[0] & 0x1f) << 3;
1162 case MWPF_TRUECOLOR555
:
1163 rgb
[0] = (pp
[1] & 0x7c) << 1;
1164 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1165 ((pp
[0] & 0xe0) >> 2);
1166 rgb
[2] = (pp
[0] & 0x1f) << 3;
1168 case MWPF_TRUECOLOR332
:
1169 rgb
[0] = pp
[0] & 0xe0;
1170 rgb
[1] = (pp
[0] & 0x1c) << 3;
1171 rgb
[2] = (pp
[0] & 0x03) << 6;
1174 fprintf(stderr
, "Unsupported pixel "
1179 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1184 if(palette
) free(palette
);
1190 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1191 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1193 unsigned char rgb
[3], *pp
;
1195 pp
= (unsigned char*) & pixel
;
1197 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 format\n");
1244 // ============================================================================
1246 // ============================================================================
1248 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
);
1250 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1254 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1256 // ----------------------------------------------------------------------------
1258 // ----------------------------------------------------------------------------
1260 class wxXPMFileHandler
: public wxBitmapHandler
1262 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
);
1266 m_name
= "XPM file";
1267 m_extension
= "xpm";
1268 m_type
= wxBITMAP_TYPE_XPM
;
1271 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1272 int desiredWidth
, int desiredHeight
);
1274 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1275 int type
, const wxPalette
*palette
= NULL
);
1277 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1278 int width
, int height
, int depth
= 1)
1282 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
);
1284 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
1285 long flags
, int desiredWidth
,
1289 if (!bitmap
->GetRefData())
1290 bitmap
->SetRefData( new wxBitmapRefData() );
1292 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1294 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1296 int xscreen
= DefaultScreen( xdisplay
);
1297 Window xroot
= RootWindow( xdisplay
, xscreen
);
1299 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1301 XpmAttributes xpmAttr
;
1302 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1307 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
,
1308 (char*) name
.c_str(),
1309 &pixmap
, &mask
, &xpmAttr
);
1311 if (ErrorStatus
== XpmSuccess
)
1313 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1314 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1316 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1318 XpmFreeAttributes(&xpmAttr
);
1320 M_BMPHANDLERDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1324 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1325 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1326 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1338 wxXPMDecoder decoder
;
1339 wxFileInputStream
stream(name
);
1342 wxImage
image(decoder
.ReadFile(stream
));
1344 return CreateFromImage(image
);
1350 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1353 bool wxXPMFileHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1355 const wxPalette
*WXUNUSED(palette
))
1357 wxImage
image(bitmap
->ConvertToImage());
1358 if (image
.Ok()) return image
.SaveFile( name
, (wxBitmapType
)type
);
1363 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1365 // ----------------------------------------------------------------------------
1367 // ----------------------------------------------------------------------------
1369 class wxXPMDataHandler
: public wxBitmapHandler
1371 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
);
1375 m_name
= "XPM data";
1376 m_extension
= "xpm";
1377 m_type
= wxBITMAP_TYPE_XPM_DATA
;
1380 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1381 int desiredWidth
, int desiredHeight
)
1384 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1385 int type
, const wxPalette
*palette
= NULL
)
1388 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1389 int width
, int height
, int depth
= 1);
1392 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
);
1394 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *bits
,
1395 long WXUNUSED(flags
),
1396 int width
, int height
, int WXUNUSED(depth
))
1399 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
1401 if (!bitmap
->GetRefData())
1402 bitmap
->SetRefData( new wxBitmapRefData() );
1404 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1406 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1408 int xscreen
= DefaultScreen( xdisplay
);
1409 Window xroot
= RootWindow( xdisplay
, xscreen
);
1411 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1413 XpmAttributes xpmAttr
;
1414 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1419 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
,
1420 &pixmap
, &mask
, &xpmAttr
);
1422 if (ErrorStatus
== XpmSuccess
)
1424 M_BMPHANDLERDATA
->m_width
= xpmAttr
.width
;
1425 M_BMPHANDLERDATA
->m_height
= xpmAttr
.height
;
1427 M_BMPHANDLERDATA
->m_bpp
= bpp
; // mono as well?
1430 unsigned int depthRet
;
1432 unsigned int widthRet
, heightRet
, borderWidthRet
;
1433 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
1434 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
1436 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") );
1439 XpmFreeAttributes(&xpmAttr
);
1441 M_BMPHANDLERDATA
->m_pixmap
= (WXPixmap
) pixmap
;
1445 M_BMPHANDLERDATA
->m_mask
= new wxMask
;
1446 M_BMPHANDLERDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1447 M_BMPHANDLERDATA
->m_mask
->SetDisplay( xdisplay
);
1458 wxXPMDecoder decoder
;
1459 wxImage
image(decoder
.ReadData(bits
));
1461 return bitmap
->CreateFromImage(image
);
1469 // ----------------------------------------------------------------------------
1471 // ----------------------------------------------------------------------------
1473 class WXDLLEXPORT wxXBMDataHandler
: public wxBitmapHandler
1475 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler
)
1477 inline wxXBMDataHandler()
1479 m_name
= "XBM data";
1480 m_extension
= "xbm";
1481 m_type
= wxBITMAP_TYPE_XBM_DATA
;
1484 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1485 int desiredWidth
, int desiredHeight
)
1488 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
1489 int type
, const wxPalette
*palette
= NULL
)
1492 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
1493 int width
, int height
, int depth
= 1);
1496 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler
, wxBitmapHandler
);
1498 bool wxXBMDataHandler::Create( wxBitmap
*bitmap
, void *bits
,
1499 long WXUNUSED(flags
),
1500 int width
, int height
, int depth
)
1503 if (!bitmap
->GetRefData())
1504 bitmap
->SetRefData( new wxBitmapRefData() );
1506 M_BMPHANDLERDATA
->m_display
= wxGlobalDisplay();
1508 Display
*xdisplay
= (Display
*) M_BMPHANDLERDATA
->m_display
;
1510 int xscreen
= DefaultScreen( xdisplay
);
1511 Window xroot
= RootWindow( xdisplay
, xscreen
);
1513 M_BMPHANDLERDATA
->m_mask
= (wxMask
*) NULL
;
1514 M_BMPHANDLERDATA
->m_bitmap
=
1515 (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
,
1516 (char *) bits
, width
, height
);
1517 M_BMPHANDLERDATA
->m_width
= width
;
1518 M_BMPHANDLERDATA
->m_height
= height
;
1519 M_BMPHANDLERDATA
->m_bpp
= 1;
1523 wxCHECK_MSG( M_BMPHANDLERDATA
->m_bitmap
, FALSE
,
1524 wxT("couldn't create bitmap") );
1527 void wxBitmap::InitStandardHandlers()
1529 AddHandler(new wxXBMDataHandler
);
1531 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1532 AddHandler(new wxXPMFileHandler
);
1534 AddHandler(new wxXPMDataHandler
);