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
37 #include "wx/xpmdecod.h"
38 #include "wx/wfstream.h"
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
55 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
58 Create( bitmap
, colour
);
61 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
64 Create( bitmap
, paletteIndex
);
67 wxMask::wxMask( const wxBitmap
& bitmap
)
76 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
79 bool wxMask::Create( const wxBitmap
& bitmap
,
80 const wxColour
& colour
)
85 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
89 m_display
= bitmap
.GetDisplay();
91 wxImage
image( bitmap
);
92 if (!image
.Ok()) return FALSE
;
94 m_display
= bitmap
.GetDisplay();
96 Display
*xdisplay
= (Display
*) m_display
;
98 int xscreen
= DefaultScreen( xdisplay
);
99 Window xroot
= RootWindow( xdisplay
, xscreen
);
100 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
101 int bpp
= DefaultDepth( 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 XVisualInfo vinfo_template
;
120 vinfo_template
.visual
= xvisual
;
121 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
122 vinfo_template
.depth
= bpp
;
125 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
126 wxASSERT_MSG( vi
, wxT("No visual info") );
128 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
132 green
= green
& 0xf8;
138 green
= green
& 0xfc;
144 green
= green
& 0xf0;
148 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
150 for (int j
= 0; j
< image
.GetHeight(); j
++)
154 for (i
= 0; i
< image
.GetWidth(); i
++)
156 if ((data
[index
] == red
) &&
157 (data
[index
+1] == green
) &&
158 (data
[index
+2] == blue
))
167 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
174 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
177 XFreeGC( xdisplay
, gc
);
186 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
189 wxPalette
*pal
= bitmap
.GetPalette();
191 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
193 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
195 return Create(bitmap
, wxColour(r
, g
, b
));
198 bool wxMask::Create( const wxBitmap
& bitmap
)
203 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
207 if (!bitmap
.Ok()) return FALSE
;
209 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
211 m_display
= bitmap
.GetDisplay();
213 int xscreen
= DefaultScreen( (Display
*) m_display
);
214 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
216 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
218 if (!m_bitmap
) return FALSE
;
220 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
222 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
223 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
225 XFreeGC( (Display
*) m_display
, gc
);
234 //-----------------------------------------------------------------------------
236 //-----------------------------------------------------------------------------
238 class wxBitmapRefData
: public wxObjectRefData
246 WXDisplay
*m_display
;
251 wxPalette
*m_palette
;
254 wxBitmapRefData::wxBitmapRefData()
259 m_mask
= (wxMask
*) NULL
;
263 m_palette
= (wxPalette
*) NULL
;
266 wxBitmapRefData::~wxBitmapRefData()
268 if (m_pixmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_pixmap
);
269 if (m_bitmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
270 if (m_mask
) delete m_mask
;
271 if (m_palette
) delete m_palette
;
274 //-----------------------------------------------------------------------------
276 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
278 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
284 wxBitmap::wxBitmap( int width
, int height
, int depth
)
286 Create( width
, height
, depth
);
289 bool wxBitmap::Create( int width
, int height
, int depth
)
293 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
295 m_refData
= new wxBitmapRefData();
297 M_BMPDATA
->m_display
= wxGlobalDisplay();
299 wxASSERT_MSG( M_BMPDATA
->m_display
, wxT("No display") );
301 int xscreen
= DefaultScreen( (Display
*) M_BMPDATA
->m_display
);
302 Window xroot
= RootWindow( (Display
*) M_BMPDATA
->m_display
, xscreen
);
304 int bpp
= DefaultDepth( (Display
*) M_BMPDATA
->m_display
, xscreen
);
305 if (depth
== -1) depth
= bpp
;
307 wxCHECK_MSG( (depth
== bpp
) ||
308 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
310 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
311 M_BMPDATA
->m_width
= width
;
312 M_BMPDATA
->m_height
= height
;
315 M_BMPDATA
->m_bitmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
316 M_BMPDATA
->m_bpp
= bpp
;
318 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
322 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, 1 );
324 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
326 M_BMPDATA
->m_bpp
= 1;
330 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, depth
);
332 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Pixmap creation failed") );
334 M_BMPDATA
->m_bpp
= depth
;
340 bool wxBitmap::CreateFromXpm( const char **bits
)
346 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
348 m_refData
= new wxBitmapRefData();
350 M_BMPDATA
->m_display
= wxGlobalDisplay();
352 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
354 int xscreen
= DefaultScreen( xdisplay
);
355 Window xroot
= RootWindow( xdisplay
, xscreen
);
357 int bpp
= DefaultDepth( xdisplay
, xscreen
);
359 XpmAttributes xpmAttr
;
360 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
365 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
, &pixmap
, &mask
, &xpmAttr
);
367 if (ErrorStatus
== XpmSuccess
)
369 M_BMPDATA
->m_width
= xpmAttr
.width
;
370 M_BMPDATA
->m_height
= xpmAttr
.height
;
372 M_BMPDATA
->m_bpp
= bpp
; // mono as well?
375 unsigned int depthRet
;
377 unsigned int widthRet
, heightRet
, borderWidthRet
;
378 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
379 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
381 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") )
384 XpmFreeAttributes(&xpmAttr
);
386 M_BMPDATA
->m_pixmap
= (WXPixmap
) pixmap
;
390 M_BMPDATA
->m_mask
= new wxMask
;
391 M_BMPDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
392 M_BMPDATA
->m_mask
->SetDisplay( xdisplay
);
403 wxXPMDecoder decoder
;
404 wxImage
image(decoder
.ReadData(bits
));
406 return CreateFromImage(image
);
414 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
419 wxASSERT_MSG(image
.Ok(), "Invalid wxImage passed to wxBitmap::CreateFromImage.");
423 int w
= image
.GetWidth();
424 int h
= image
.GetHeight();
426 if (!Create(w
, h
, depth
))
430 memDC
.SelectObject(*this);
432 // Warning: this is very inefficient.
434 pen
.SetStyle(wxSOLID
);
438 for (i
= 0; i
< w
; i
++)
440 for (j
= 0; j
< h
; j
++)
442 unsigned char red
= image
.GetRed(i
, j
);
443 unsigned char green
= image
.GetGreen(i
, j
);
444 unsigned char blue
= image
.GetBlue(i
, j
);
445 wxColour
colour(red
, green
, blue
);
447 pen
.SetColour(colour
);
449 memDC
.DrawPoint(i
, j
);
454 // scan the bitmap for the transparent colour and set the corresponding
455 // pixels in the mask to BLACK and the rest to WHITE
456 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
457 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
459 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
464 memDC
.SelectObject(wxNullBitmap
);
472 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
473 wxCHECK_MSG( depth
== -1, FALSE
, wxT("invalid bitmap depth") )
475 m_refData
= new wxBitmapRefData();
477 M_BMPDATA
->m_display
= wxGlobalDisplay();
479 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
481 int xscreen
= DefaultScreen( xdisplay
);
482 Window xroot
= RootWindow( xdisplay
, xscreen
);
483 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
485 int bpp
= DefaultDepth( xdisplay
, xscreen
);
487 int width
= image
.GetWidth();
488 int height
= image
.GetHeight();
489 M_BMPDATA
->m_width
= width
;
490 M_BMPDATA
->m_height
= height
;
492 if (depth
!= 1) depth
= bpp
;
493 M_BMPDATA
->m_bpp
= depth
;
497 wxFAIL_MSG( "mono images later" );
503 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
504 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
506 if (data_image
->data
== NULL
)
508 wxLogError( wxT("Out of memory.") ); // TODO clean
512 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
516 XImage
*mask_image
= (XImage
*) NULL
;
519 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
520 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
522 if (mask_image
->data
== NULL
)
524 wxLogError( wxT("Out of memory.") ); // TODO clean
528 wxMask
*mask
= new wxMask();
529 mask
->SetDisplay( xdisplay
);
530 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
537 XVisualInfo vinfo_template
;
540 vinfo_template
.visual
= xvisual
;
541 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
542 vinfo_template
.depth
= bpp
;
545 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
546 wxASSERT_MSG( vi
, wxT("No visual info") );
548 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
549 if (bpp
< 8) bpp
= 8;
553 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
554 byte_order b_o
= RGB
;
558 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
559 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RBG
;
560 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
561 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
562 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
563 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
568 int r_mask
= image
.GetMaskRed();
569 int g_mask
= image
.GetMaskGreen();
570 int b_mask
= image
.GetMaskBlue();
572 unsigned char* data
= image
.GetData();
573 wxASSERT_MSG( data
, "No image data" );
575 bool hasMask
= image
.HasMask();
578 for (int y
= 0; y
< height
; y
++)
580 for (int x
= 0; x
< width
; x
++)
591 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
592 XPutPixel( mask_image
, x
, y
, 0 );
594 XPutPixel( mask_image
, x
, y
, 1 );
603 if (wxTheApp
->m_colorCube
)
605 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
609 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
610 GdkColor
*colors
= cmap
->colors
;
611 int max
= 3 * (65536);
613 for (int i
= 0; i
< cmap
->size
; i
++)
615 int rdiff
= (r
<< 8) - colors
[i
].red
;
616 int gdiff
= (g
<< 8) - colors
[i
].green
;
617 int bdiff
= (b
<< 8) - colors
[i
].blue
;
618 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
619 if (sum
< max
) { pixel
= i
; max
= sum
; }
623 XPutPixel( data_image
, x
, y
, pixel
);
631 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
632 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
633 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
634 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
635 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
636 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
638 XPutPixel( data_image
, x
, y
, pixel
);
646 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
647 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
648 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
649 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
650 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
651 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
653 XPutPixel( data_image
, x
, y
, pixel
);
658 // I actually don't know if for 16-bit displays, it is alway the green
659 // component or the second component which has 6 bits.
663 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
664 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
665 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
666 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
667 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
668 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
670 XPutPixel( data_image
, x
, y
, pixel
);
679 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
680 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
681 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
682 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
683 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
684 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
686 XPutPixel( data_image
, x
, y
, pixel
);
695 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
696 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
698 XSync(wxGlobalDisplay(), False
);
701 XDestroyImage( data_image
);
702 XFreeGC( xdisplay
, gc
);
708 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
709 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
711 XDestroyImage( mask_image
);
712 XFreeGC( xdisplay
, gc
);
721 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
726 while (!(mask
& 0x1))
739 wxImage
wxBitmap::ConvertToImage() const
743 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
745 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
746 wxASSERT_MSG( xdisplay
, wxT("No display") );
748 int xscreen
= DefaultScreen( xdisplay
);
749 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
751 int bpp
= DefaultDepth( xdisplay
, xscreen
);
754 int w
= image
.GetWidth();
755 int h
= image
.GetHeight();
758 memDC
.SelectObject(*this);
762 // Warning: this is very inefficient.
764 for (i
= 0; i
< w
; i
++)
766 for (j
= 0; j
< h
; j
++)
768 memDC
.GetPixel(i
, j
, & pixelCol
);
770 // TODO: make wxColour accessors more efficient
771 // by inlining, if possible
773 pixelCol
.Red(), pixelCol
.Green(),
777 memDC
.SelectObject(wxNullBitmap
);
783 XImage
*x_image
= NULL
;
786 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
788 GetWidth(), GetHeight(),
789 AllPlanes
, ZPixmap
);
793 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
795 GetWidth(), GetHeight(),
796 AllPlanes
, ZPixmap
);
799 wxFAIL_MSG( wxT("Ill-formed bitmap") );
802 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
804 image
.Create( GetWidth(), GetHeight() );
805 char unsigned *data
= image
.GetData();
809 XDestroyImage( x_image
);
810 wxFAIL_MSG( wxT("couldn't create image") );
814 XImage
*x_image_mask
= NULL
;
817 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
819 GetWidth(), GetHeight(),
820 AllPlanes
, ZPixmap
);
822 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
825 int red_shift_right
= 0;
826 int green_shift_right
= 0;
827 int blue_shift_right
= 0;
828 int red_shift_left
= 0;
829 int green_shift_left
= 0;
830 int blue_shift_left
= 0;
831 bool use_shift
= FALSE
;
837 XVisualInfo vinfo_template
;
840 vinfo_template
.visual
= xvisual
;
841 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
842 vinfo_template
.depth
= bpp
;
845 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
846 wxASSERT_MSG( vi
, wxT("No visual info") );
848 int red_prec
,green_prec
,blue_prec
;
849 int red_shift
,green_shift
,blue_shift
;
850 wxCalcPrecAndShift( vi
->red_mask
, &red_shift
, &red_prec
);
851 wxCalcPrecAndShift( vi
->green_mask
, &green_shift
, &green_prec
);
852 wxCalcPrecAndShift( vi
->blue_mask
, &blue_shift
, &blue_prec
);
853 if (bpp
== 16) bpp
= red_prec
+ green_prec
+ blue_prec
;
855 red_shift_right
= red_shift
;
856 red_shift_left
= 8-red_prec
;
857 green_shift_right
= green_shift
;
858 green_shift_left
= 8-green_prec
;
859 blue_shift_right
= blue_shift
;
860 blue_shift_left
= 8-blue_prec
;
863 use_shift
= (vi
->visual
->c_class
== TrueColor
) || (vi
->visual
->c_class
== DirectColor
);
876 // GdkColormap *cmap = gtk_widget_get_default_colormap();
879 for (int j
= 0; j
< GetHeight(); j
++)
881 for (int i
= 0; i
< GetWidth(); i
++)
883 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
901 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
902 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
903 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
906 else if (cmap
->colors
)
908 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
909 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
910 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
915 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
920 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
933 XDestroyImage( x_image
);
934 if (x_image_mask
) XDestroyImage( x_image_mask
);
940 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
945 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
947 LoadFile( filename
, type
);
950 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
) )
953 m_refData
= new wxBitmapRefData();
955 M_BMPDATA
->m_display
= wxGlobalDisplay();
957 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
959 int xscreen
= DefaultScreen( xdisplay
);
960 Window xroot
= RootWindow( xdisplay
, xscreen
);
962 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
963 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
, (char *) bits
, width
, height
);
964 M_BMPDATA
->m_width
= width
;
965 M_BMPDATA
->m_height
= height
;
966 M_BMPDATA
->m_bpp
= 1;
968 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
971 wxBitmap::~wxBitmap()
975 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
977 if ( m_refData
!= bmp
.m_refData
)
983 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
985 return m_refData
== bmp
.m_refData
;
988 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
990 return m_refData
!= bmp
.m_refData
;
993 bool wxBitmap::Ok() const
995 return (m_refData
!= NULL
);
998 int wxBitmap::GetHeight() const
1000 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1002 return M_BMPDATA
->m_height
;
1005 int wxBitmap::GetWidth() const
1007 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1009 return M_BMPDATA
->m_width
;
1012 int wxBitmap::GetDepth() const
1014 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1016 return M_BMPDATA
->m_bpp
;
1019 wxMask
*wxBitmap::GetMask() const
1021 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1023 return M_BMPDATA
->m_mask
;
1026 void wxBitmap::SetMask( wxMask
*mask
)
1028 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1030 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1032 M_BMPDATA
->m_mask
= mask
;
1035 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1041 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1043 wxCHECK_MSG( Ok() &&
1044 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1045 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1046 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1048 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1049 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1052 wxFAIL_MSG( "wxBitmap::GetSubBitmap not yet implemented" );
1055 if (ret
.GetPixmap())
1057 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1058 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1059 gdk_gc_destroy( gc
);
1063 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1064 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1065 gdk_gc_destroy( gc
);
1070 wxMask
*mask
= new wxMask
;
1071 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1073 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1074 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
1075 gdk_gc_destroy( gc
);
1077 ret
.SetMask( mask
);
1084 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
1086 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1088 // Try to save the bitmap via wxImage handlers:
1090 wxImage
image( *this );
1091 if (image
.Ok()) return image
.SaveFile( name
, type
);
1097 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
1101 if (!wxFileExists(name
)) return FALSE
;
1104 if (type
== wxBITMAP_TYPE_XPM
)
1108 m_refData
= new wxBitmapRefData();
1110 M_BMPDATA
->m_display
= wxGlobalDisplay();
1112 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
1114 int xscreen
= DefaultScreen( xdisplay
);
1115 Window xroot
= RootWindow( xdisplay
, xscreen
);
1117 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1119 XpmAttributes xpmAttr
;
1120 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1125 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
, (char*) name
.c_str(), &pixmap
, &mask
, &xpmAttr
);
1127 if (ErrorStatus
== XpmSuccess
)
1129 M_BMPDATA
->m_width
= xpmAttr
.width
;
1130 M_BMPDATA
->m_height
= xpmAttr
.height
;
1132 M_BMPDATA
->m_bpp
= bpp
; // mono as well?
1134 XpmFreeAttributes(&xpmAttr
);
1136 M_BMPDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1140 M_BMPDATA
->m_mask
= new wxMask
;
1141 M_BMPDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1142 M_BMPDATA
->m_mask
->SetDisplay( xdisplay
);
1153 wxXPMDecoder decoder
;
1154 wxFileInputStream
stream(name
);
1157 wxImage
image(decoder
.ReadFile(stream
));
1159 return CreateFromImage(image
);
1175 else // try if wxImage can load it
1178 if (!image
.LoadFile( name
, type
)) return FALSE
;
1179 if (image
.Ok()) *this = image
.ConvertToBitmap();
1186 wxPalette
*wxBitmap::GetPalette() const
1188 if (!Ok()) return (wxPalette
*) NULL
;
1190 return M_BMPDATA
->m_palette
;
1193 void wxBitmap::SetHeight( int height
)
1195 if (!m_refData
) m_refData
= new wxBitmapRefData();
1197 M_BMPDATA
->m_height
= height
;
1200 void wxBitmap::SetWidth( int width
)
1202 if (!m_refData
) m_refData
= new wxBitmapRefData();
1204 M_BMPDATA
->m_width
= width
;
1207 void wxBitmap::SetDepth( int depth
)
1209 if (!m_refData
) m_refData
= new wxBitmapRefData();
1211 M_BMPDATA
->m_bpp
= depth
;
1214 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1216 if (!m_refData
) m_refData
= new wxBitmapRefData();
1218 M_BMPDATA
->m_pixmap
= pixmap
;
1221 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1223 if (!m_refData
) m_refData
= new wxBitmapRefData();
1225 M_BMPDATA
->m_bitmap
= bitmap
;
1228 WXPixmap
wxBitmap::GetPixmap() const
1230 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1232 return M_BMPDATA
->m_pixmap
;
1235 WXPixmap
wxBitmap::GetBitmap() const
1237 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1239 return M_BMPDATA
->m_bitmap
;
1242 WXDisplay
*wxBitmap::GetDisplay() const
1244 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1246 return M_BMPDATA
->m_display
;