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/x11/private.h"
28 #include "wx/xpmdecod.h"
29 #include "wx/wfstream.h"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
46 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
49 Create( bitmap
, colour
);
52 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
55 Create( bitmap
, paletteIndex
);
58 wxMask::wxMask( const wxBitmap
& bitmap
)
67 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
70 bool wxMask::Create( const wxBitmap
& bitmap
,
71 const wxColour
& colour
)
75 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
79 m_display
= bitmap
.GetDisplay();
81 wxImage
image( bitmap
);
82 if (!image
.Ok()) return FALSE
;
84 m_display
= bitmap
.GetDisplay();
86 Display
*xdisplay
= (Display
*) m_display
;
88 int xscreen
= DefaultScreen( xdisplay
);
89 Window xroot
= RootWindow( xdisplay
, xscreen
);
90 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
91 int bpp
= DefaultDepth( xdisplay
, xscreen
);
93 m_bitmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, image
.GetWidth(), image
.GetHeight(), 1 );
94 GC gc
= XCreateGC( xdisplay
, (Pixmap
) m_bitmap
, 0, NULL
);
96 XSetForeground( xdisplay
, gc
, WhitePixel(xdisplay
,xscreen
) );
97 XSetFillStyle( xdisplay
, gc
, FillSolid
);
98 XFillRectangle( xdisplay
, (Pixmap
) m_bitmap
, gc
, 0, 0, image
.GetWidth(), image
.GetHeight() );
100 unsigned char *data
= image
.GetData();
103 unsigned char red
= colour
.Red();
104 unsigned char green
= colour
.Green();
105 unsigned char blue
= colour
.Blue();
107 XVisualInfo vinfo_template
;
110 vinfo_template
.visual
= xvisual
;
111 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
112 vinfo_template
.depth
= bpp
;
115 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
116 wxASSERT_MSG( vi
, wxT("No visual info") );
118 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
122 green
= green
& 0xf8;
128 green
= green
& 0xfc;
134 green
= green
& 0xf0;
138 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
140 for (int j
= 0; j
< image
.GetHeight(); j
++)
144 for (i
= 0; i
< image
.GetWidth(); i
++)
146 if ((data
[index
] == red
) &&
147 (data
[index
+1] == green
) &&
148 (data
[index
+2] == blue
))
157 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
164 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
167 XFreeGC( xdisplay
, gc
);
172 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
175 wxPalette
*pal
= bitmap
.GetPalette();
177 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
179 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
181 return Create(bitmap
, wxColour(r
, g
, b
));
184 bool wxMask::Create( const wxBitmap
& bitmap
)
188 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
192 if (!bitmap
.Ok()) return FALSE
;
194 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
196 m_display
= bitmap
.GetDisplay();
198 int xscreen
= DefaultScreen( (Display
*) m_display
);
199 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
201 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
203 if (!m_bitmap
) return FALSE
;
205 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
207 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
208 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
210 XFreeGC( (Display
*) m_display
, gc
);
215 //-----------------------------------------------------------------------------
217 //-----------------------------------------------------------------------------
219 class wxBitmapRefData
: public wxObjectRefData
227 WXDisplay
*m_display
;
232 wxPalette
*m_palette
;
235 wxBitmapRefData::wxBitmapRefData()
240 m_mask
= (wxMask
*) NULL
;
244 m_palette
= (wxPalette
*) NULL
;
247 wxBitmapRefData::~wxBitmapRefData()
249 if (m_pixmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_pixmap
);
250 if (m_bitmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
251 if (m_mask
) delete m_mask
;
252 if (m_palette
) delete m_palette
;
255 //-----------------------------------------------------------------------------
257 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
259 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
265 wxBitmap::wxBitmap( int width
, int height
, int depth
)
267 Create( width
, height
, depth
);
270 bool wxBitmap::Create( int width
, int height
, int depth
)
274 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
276 m_refData
= new wxBitmapRefData();
278 M_BMPDATA
->m_display
= wxGlobalDisplay();
280 wxASSERT_MSG( M_BMPDATA
->m_display
, wxT("No display") );
282 int xscreen
= DefaultScreen( (Display
*) M_BMPDATA
->m_display
);
283 Window xroot
= RootWindow( (Display
*) M_BMPDATA
->m_display
, xscreen
);
285 int bpp
= DefaultDepth( (Display
*) M_BMPDATA
->m_display
, xscreen
);
286 if (depth
== -1) depth
= bpp
;
288 wxCHECK_MSG( (depth
== bpp
) ||
289 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
291 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
292 M_BMPDATA
->m_width
= width
;
293 M_BMPDATA
->m_height
= height
;
296 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, 1 );
298 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
300 M_BMPDATA
->m_bpp
= 1;
304 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, depth
);
306 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Pixmap creation failed") );
308 M_BMPDATA
->m_bpp
= depth
;
314 bool wxBitmap::CreateFromXpm( const char **bits
)
320 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
322 m_refData
= new wxBitmapRefData();
324 M_BMPDATA
->m_display
= wxGlobalDisplay();
326 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
328 int xscreen
= DefaultScreen( xdisplay
);
329 Window xroot
= RootWindow( xdisplay
, xscreen
);
331 int bpp
= DefaultDepth( xdisplay
, xscreen
);
333 XpmAttributes xpmAttr
;
334 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
339 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
, &pixmap
, &mask
, &xpmAttr
);
341 if (ErrorStatus
== XpmSuccess
)
343 M_BMPDATA
->m_width
= xpmAttr
.width
;
344 M_BMPDATA
->m_height
= xpmAttr
.height
;
346 M_BMPDATA
->m_bpp
= bpp
; // mono as well?
349 unsigned int depthRet
;
351 unsigned int widthRet
, heightRet
, borderWidthRet
;
352 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
353 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
355 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") )
358 XpmFreeAttributes(&xpmAttr
);
360 M_BMPDATA
->m_pixmap
= (WXPixmap
) pixmap
;
364 M_BMPDATA
->m_mask
= new wxMask
;
365 M_BMPDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
366 M_BMPDATA
->m_mask
->SetDisplay( xdisplay
);
377 wxXPMDecoder decoder
;
378 wxImage
image(decoder
.ReadData(bits
));
380 return CreateFromImage(image
);
388 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
392 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
393 wxCHECK_MSG( depth
== -1, FALSE
, wxT("invalid bitmap depth") )
395 m_refData
= new wxBitmapRefData();
397 M_BMPDATA
->m_display
= wxGlobalDisplay();
399 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
401 int xscreen
= DefaultScreen( xdisplay
);
402 Window xroot
= RootWindow( xdisplay
, xscreen
);
403 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
405 int bpp
= DefaultDepth( xdisplay
, xscreen
);
407 int width
= image
.GetWidth();
408 int height
= image
.GetHeight();
409 M_BMPDATA
->m_width
= width
;
410 M_BMPDATA
->m_height
= height
;
412 if (depth
!= 1) depth
= bpp
;
413 M_BMPDATA
->m_bpp
= depth
;
417 wxFAIL_MSG( "mono images later" );
423 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
424 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
426 if (data_image
->data
== NULL
)
428 wxLogError( wxT("Out of memory.") ); // TODO clean
432 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
436 XImage
*mask_image
= (XImage
*) NULL
;
439 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
440 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
442 if (mask_image
->data
== NULL
)
444 wxLogError( wxT("Out of memory.") ); // TODO clean
448 wxMask
*mask
= new wxMask();
449 mask
->SetDisplay( xdisplay
);
450 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
457 XVisualInfo vinfo_template
;
460 vinfo_template
.visual
= xvisual
;
461 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
462 vinfo_template
.depth
= bpp
;
465 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
466 wxASSERT_MSG( vi
, wxT("No visual info") );
468 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
469 if (bpp
< 8) bpp
= 8;
473 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
474 byte_order b_o
= RGB
;
478 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
479 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RBG
;
480 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
481 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
482 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
483 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
488 int r_mask
= image
.GetMaskRed();
489 int g_mask
= image
.GetMaskGreen();
490 int b_mask
= image
.GetMaskBlue();
492 unsigned char* data
= image
.GetData();
493 wxASSERT_MSG( data
, "No image data" );
495 bool hasMask
= image
.HasMask();
498 for (int y
= 0; y
< height
; y
++)
500 for (int x
= 0; x
< width
; x
++)
511 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
512 XPutPixel( mask_image
, x
, y
, 0 );
514 XPutPixel( mask_image
, x
, y
, 1 );
523 if (wxTheApp
->m_colorCube
)
525 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
529 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
530 GdkColor
*colors
= cmap
->colors
;
531 int max
= 3 * (65536);
533 for (int i
= 0; i
< cmap
->size
; i
++)
535 int rdiff
= (r
<< 8) - colors
[i
].red
;
536 int gdiff
= (g
<< 8) - colors
[i
].green
;
537 int bdiff
= (b
<< 8) - colors
[i
].blue
;
538 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
539 if (sum
< max
) { pixel
= i
; max
= sum
; }
543 XPutPixel( data_image
, x
, y
, pixel
);
551 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
552 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
553 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
554 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
555 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
556 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
558 XPutPixel( data_image
, x
, y
, pixel
);
566 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
567 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
568 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
569 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
570 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
571 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
573 XPutPixel( data_image
, x
, y
, pixel
);
578 // I actually don't know if for 16-bit displays, it is alway the green
579 // component or the second component which has 6 bits.
583 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
584 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
585 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
586 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
587 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
588 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
590 XPutPixel( data_image
, x
, y
, pixel
);
599 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
600 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
601 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
602 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
603 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
604 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
606 XPutPixel( data_image
, x
, y
, pixel
);
615 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
616 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
618 XSync(wxGlobalDisplay(), False
);
621 XDestroyImage( data_image
);
622 XFreeGC( xdisplay
, gc
);
628 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
629 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
631 XDestroyImage( mask_image
);
632 XFreeGC( xdisplay
, gc
);
639 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
644 while (!(mask
& 0x1))
657 wxImage
wxBitmap::ConvertToImage() const
661 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
663 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
664 wxASSERT_MSG( xdisplay
, wxT("No display") );
666 int xscreen
= DefaultScreen( xdisplay
);
667 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
669 int bpp
= DefaultDepth( xdisplay
, xscreen
);
671 XImage
*x_image
= NULL
;
674 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
676 GetWidth(), GetHeight(),
677 AllPlanes
, ZPixmap
);
681 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
683 GetWidth(), GetHeight(),
684 AllPlanes
, ZPixmap
);
687 wxFAIL_MSG( wxT("Ill-formed bitmap") );
690 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
692 image
.Create( GetWidth(), GetHeight() );
693 char unsigned *data
= image
.GetData();
697 XDestroyImage( x_image
);
698 wxFAIL_MSG( wxT("couldn't create image") );
702 XImage
*x_image_mask
= NULL
;
705 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
707 GetWidth(), GetHeight(),
708 AllPlanes
, ZPixmap
);
710 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
713 int red_shift_right
= 0;
714 int green_shift_right
= 0;
715 int blue_shift_right
= 0;
716 int red_shift_left
= 0;
717 int green_shift_left
= 0;
718 int blue_shift_left
= 0;
719 bool use_shift
= FALSE
;
725 XVisualInfo vinfo_template
;
728 vinfo_template
.visual
= xvisual
;
729 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
730 vinfo_template
.depth
= bpp
;
733 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
734 wxASSERT_MSG( vi
, wxT("No visual info") );
736 int red_prec
,green_prec
,blue_prec
;
737 int red_shift
,green_shift
,blue_shift
;
738 wxCalcPrecAndShift( vi
->red_mask
, &red_shift
, &red_prec
);
739 wxCalcPrecAndShift( vi
->green_mask
, &green_shift
, &green_prec
);
740 wxCalcPrecAndShift( vi
->blue_mask
, &blue_shift
, &blue_prec
);
741 if (bpp
== 16) bpp
= red_prec
+ green_prec
+ blue_prec
;
743 red_shift_right
= red_shift
;
744 red_shift_left
= 8-red_prec
;
745 green_shift_right
= green_shift
;
746 green_shift_left
= 8-green_prec
;
747 blue_shift_right
= blue_shift
;
748 blue_shift_left
= 8-blue_prec
;
751 use_shift
= (vi
->visual
->c_class
== TrueColor
) || (vi
->visual
->c_class
== DirectColor
);
764 // GdkColormap *cmap = gtk_widget_get_default_colormap();
767 for (int j
= 0; j
< GetHeight(); j
++)
769 for (int i
= 0; i
< GetWidth(); i
++)
771 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
789 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
790 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
791 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
794 else if (cmap
->colors
)
796 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
797 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
798 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
803 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
808 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
821 XDestroyImage( x_image
);
822 if (x_image_mask
) XDestroyImage( x_image_mask
);
827 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
832 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
834 LoadFile( filename
, type
);
837 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
) )
839 m_refData
= new wxBitmapRefData();
841 M_BMPDATA
->m_display
= wxGlobalDisplay();
843 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
845 int xscreen
= DefaultScreen( xdisplay
);
846 Window xroot
= RootWindow( xdisplay
, xscreen
);
848 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
849 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
, (char *) bits
, width
, height
);
850 M_BMPDATA
->m_width
= width
;
851 M_BMPDATA
->m_height
= height
;
852 M_BMPDATA
->m_bpp
= 1;
854 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
857 wxBitmap::~wxBitmap()
861 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
863 if ( m_refData
!= bmp
.m_refData
)
869 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
871 return m_refData
== bmp
.m_refData
;
874 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
876 return m_refData
!= bmp
.m_refData
;
879 bool wxBitmap::Ok() const
881 return (m_refData
!= NULL
);
884 int wxBitmap::GetHeight() const
886 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
888 return M_BMPDATA
->m_height
;
891 int wxBitmap::GetWidth() const
893 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
895 return M_BMPDATA
->m_width
;
898 int wxBitmap::GetDepth() const
900 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
902 return M_BMPDATA
->m_bpp
;
905 wxMask
*wxBitmap::GetMask() const
907 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
909 return M_BMPDATA
->m_mask
;
912 void wxBitmap::SetMask( wxMask
*mask
)
914 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
916 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
918 M_BMPDATA
->m_mask
= mask
;
921 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
927 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
930 (rect
.x
>= 0) && (rect
.y
>= 0) &&
931 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
932 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
934 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
935 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
938 wxFAIL_MSG( "wxBitmap::GetSubBitmap not yet implemented" );
943 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
944 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
945 gdk_gc_destroy( gc
);
949 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
950 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
951 gdk_gc_destroy( gc
);
956 wxMask
*mask
= new wxMask
;
957 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
959 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
960 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
961 gdk_gc_destroy( gc
);
970 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
972 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
974 // Try to save the bitmap via wxImage handlers:
976 wxImage
image( *this );
977 if (image
.Ok()) return image
.SaveFile( name
, type
);
983 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
987 if (!wxFileExists(name
)) return FALSE
;
990 if (type
== wxBITMAP_TYPE_XPM
)
994 m_refData
= new wxBitmapRefData();
996 M_BMPDATA
->m_display
= wxGlobalDisplay();
998 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
1000 int xscreen
= DefaultScreen( xdisplay
);
1001 Window xroot
= RootWindow( xdisplay
, xscreen
);
1003 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1005 XpmAttributes xpmAttr
;
1006 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1011 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
, (char*) name
.c_str(), &pixmap
, &mask
, &xpmAttr
);
1013 if (ErrorStatus
== XpmSuccess
)
1015 M_BMPDATA
->m_width
= xpmAttr
.width
;
1016 M_BMPDATA
->m_height
= xpmAttr
.height
;
1018 M_BMPDATA
->m_bpp
= bpp
; // mono as well?
1020 XpmFreeAttributes(&xpmAttr
);
1022 M_BMPDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1026 M_BMPDATA
->m_mask
= new wxMask
;
1027 M_BMPDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1028 M_BMPDATA
->m_mask
->SetDisplay( xdisplay
);
1039 wxXPMDecoder decoder
;
1040 wxFileInputStream
stream(name
);
1043 wxImage
image(decoder
.Read(stream
));
1045 return CreateFromImage(image
);
1061 else // try if wxImage can load it
1064 if (!image
.LoadFile( name
, type
)) return FALSE
;
1065 if (image
.Ok()) *this = image
.ConvertToBitmap();
1072 wxPalette
*wxBitmap::GetPalette() const
1074 if (!Ok()) return (wxPalette
*) NULL
;
1076 return M_BMPDATA
->m_palette
;
1079 void wxBitmap::SetHeight( int height
)
1081 if (!m_refData
) m_refData
= new wxBitmapRefData();
1083 M_BMPDATA
->m_height
= height
;
1086 void wxBitmap::SetWidth( int width
)
1088 if (!m_refData
) m_refData
= new wxBitmapRefData();
1090 M_BMPDATA
->m_width
= width
;
1093 void wxBitmap::SetDepth( int depth
)
1095 if (!m_refData
) m_refData
= new wxBitmapRefData();
1097 M_BMPDATA
->m_bpp
= depth
;
1100 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1102 if (!m_refData
) m_refData
= new wxBitmapRefData();
1104 M_BMPDATA
->m_pixmap
= pixmap
;
1107 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1109 if (!m_refData
) m_refData
= new wxBitmapRefData();
1111 M_BMPDATA
->m_bitmap
= bitmap
;
1114 WXPixmap
wxBitmap::GetPixmap() const
1116 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1118 return M_BMPDATA
->m_pixmap
;
1121 WXPixmap
wxBitmap::GetBitmap() const
1123 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1125 return M_BMPDATA
->m_bitmap
;
1128 WXDisplay
*wxBitmap::GetDisplay() const
1130 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1132 return M_BMPDATA
->m_display
;