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_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
316 M_BMPDATA
->m_bpp
= bpp
;
318 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, 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
))
429 // Unfortunately the mask has to be screen-depth since
430 // 1-bpp bitmaps don't seem to be supported
431 // TODO: implement transparent drawing, presumably
432 // by doing several blits as per the Windows
433 // implementation because Nano-X doesn't support
436 bool hasMask
= image
.HasMask();
438 GC pixmapGC
= GrNewGC();
439 Pixmap pixmap
= (Pixmap
) GetPixmap();
442 Pixmap maskPixmap
= 0;
444 unsigned char maskR
= 0;
445 unsigned char maskG
= 0;
446 unsigned char maskB
= 0;
450 maskR
= image
.GetMaskRed();
451 maskG
= image
.GetMaskGreen();
452 maskB
= image
.GetMaskBlue();
455 maskPixmap
= GrNewPixmap(w
, h
, 0);
460 wxMask
* mask
= new wxMask
;
461 mask
->SetBitmap((WXPixmap
) maskPixmap
);
466 GR_COLOR lastPixmapColour
= 0;
467 GR_COLOR lastMaskColour
= 0;
470 for (i
= 0; i
< w
; i
++)
472 for (j
= 0; j
< h
; j
++)
474 unsigned char red
= image
.GetRed(i
, j
);
475 unsigned char green
= image
.GetGreen(i
, j
);
476 unsigned char blue
= image
.GetBlue(i
, j
);
478 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
480 // Efficiency measure
481 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
483 GrSetGCForeground(pixmapGC
, colour
);
484 lastPixmapColour
= colour
;
487 GrPoint(pixmap
, pixmapGC
, i
, j
);
491 // scan the bitmap for the transparent colour and set the corresponding
492 // pixels in the mask to BLACK and the rest to WHITE
493 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
495 colour
= GR_RGB(0, 0, 0);
499 colour
= GR_RGB(255, 255, 255);
501 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
503 GrSetGCForeground(maskGC
, colour
);
504 lastMaskColour
= colour
;
506 GrPoint(maskPixmap
, maskGC
, i
, j
);
511 GrDestroyGC(pixmapGC
);
521 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
522 wxCHECK_MSG( depth
== -1, FALSE
, wxT("invalid bitmap depth") )
524 m_refData
= new wxBitmapRefData();
526 M_BMPDATA
->m_display
= wxGlobalDisplay();
528 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
530 int xscreen
= DefaultScreen( xdisplay
);
531 Window xroot
= RootWindow( xdisplay
, xscreen
);
532 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
534 int bpp
= DefaultDepth( xdisplay
, xscreen
);
536 int width
= image
.GetWidth();
537 int height
= image
.GetHeight();
538 M_BMPDATA
->m_width
= width
;
539 M_BMPDATA
->m_height
= height
;
541 if (depth
!= 1) depth
= bpp
;
542 M_BMPDATA
->m_bpp
= depth
;
546 wxFAIL_MSG( "mono images later" );
552 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
553 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
555 if (data_image
->data
== NULL
)
557 wxLogError( wxT("Out of memory.") ); // TODO clean
561 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
565 XImage
*mask_image
= (XImage
*) NULL
;
568 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
569 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
571 if (mask_image
->data
== NULL
)
573 wxLogError( wxT("Out of memory.") ); // TODO clean
577 wxMask
*mask
= new wxMask();
578 mask
->SetDisplay( xdisplay
);
579 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
586 XVisualInfo vinfo_template
;
589 vinfo_template
.visual
= xvisual
;
590 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
591 vinfo_template
.depth
= bpp
;
594 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
595 wxASSERT_MSG( vi
, wxT("No visual info") );
597 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
598 if (bpp
< 8) bpp
= 8;
602 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
603 byte_order b_o
= RGB
;
607 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
608 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RBG
;
609 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
610 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
611 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
612 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
617 int r_mask
= image
.GetMaskRed();
618 int g_mask
= image
.GetMaskGreen();
619 int b_mask
= image
.GetMaskBlue();
621 unsigned char* data
= image
.GetData();
622 wxASSERT_MSG( data
, "No image data" );
624 bool hasMask
= image
.HasMask();
627 for (int y
= 0; y
< height
; y
++)
629 for (int x
= 0; x
< width
; x
++)
640 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
641 XPutPixel( mask_image
, x
, y
, 0 );
643 XPutPixel( mask_image
, x
, y
, 1 );
652 if (wxTheApp
->m_colorCube
)
654 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
658 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
659 GdkColor
*colors
= cmap
->colors
;
660 int max
= 3 * (65536);
662 for (int i
= 0; i
< cmap
->size
; i
++)
664 int rdiff
= (r
<< 8) - colors
[i
].red
;
665 int gdiff
= (g
<< 8) - colors
[i
].green
;
666 int bdiff
= (b
<< 8) - colors
[i
].blue
;
667 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
668 if (sum
< max
) { pixel
= i
; max
= sum
; }
672 XPutPixel( data_image
, x
, y
, pixel
);
680 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
681 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
682 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
683 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
684 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
685 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
687 XPutPixel( data_image
, x
, y
, pixel
);
695 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
696 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
697 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
698 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
699 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
700 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
702 XPutPixel( data_image
, x
, y
, pixel
);
707 // I actually don't know if for 16-bit displays, it is alway the green
708 // component or the second component which has 6 bits.
712 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
713 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
714 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
715 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
716 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
717 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
719 XPutPixel( data_image
, x
, y
, pixel
);
728 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
729 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
730 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
731 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
732 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
733 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
735 XPutPixel( data_image
, x
, y
, pixel
);
744 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
745 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
747 XSync(wxGlobalDisplay(), False
);
750 XDestroyImage( data_image
);
751 XFreeGC( xdisplay
, gc
);
757 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
758 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
760 XDestroyImage( mask_image
);
761 XFreeGC( xdisplay
, gc
);
770 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
775 while (!(mask
& 0x1))
788 wxImage
wxBitmap::ConvertToImage() const
792 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
794 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
795 wxASSERT_MSG( xdisplay
, wxT("No display") );
797 int xscreen
= DefaultScreen( xdisplay
);
798 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
800 int bpp
= DefaultDepth( xdisplay
, xscreen
);
803 int w
= image
.GetWidth();
804 int h
= image
.GetHeight();
807 memDC
.SelectObject(*this);
811 // Warning: this is very inefficient.
812 // TODO: use GrReadArea to get an array of pixels
814 for (i
= 0; i
< w
; i
++)
816 for (j
= 0; j
< h
; j
++)
818 memDC
.GetPixel(i
, j
, & pixelCol
);
820 // TODO: make wxColour accessors more efficient
821 // by inlining, if possible
823 pixelCol
.Red(), pixelCol
.Green(),
827 memDC
.SelectObject(wxNullBitmap
);
833 XImage
*x_image
= NULL
;
836 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
838 GetWidth(), GetHeight(),
839 AllPlanes
, ZPixmap
);
843 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
845 GetWidth(), GetHeight(),
846 AllPlanes
, ZPixmap
);
849 wxFAIL_MSG( wxT("Ill-formed bitmap") );
852 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
854 image
.Create( GetWidth(), GetHeight() );
855 char unsigned *data
= image
.GetData();
859 XDestroyImage( x_image
);
860 wxFAIL_MSG( wxT("couldn't create image") );
864 XImage
*x_image_mask
= NULL
;
867 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
869 GetWidth(), GetHeight(),
870 AllPlanes
, ZPixmap
);
872 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
875 int red_shift_right
= 0;
876 int green_shift_right
= 0;
877 int blue_shift_right
= 0;
878 int red_shift_left
= 0;
879 int green_shift_left
= 0;
880 int blue_shift_left
= 0;
881 bool use_shift
= FALSE
;
887 XVisualInfo vinfo_template
;
890 vinfo_template
.visual
= xvisual
;
891 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
892 vinfo_template
.depth
= bpp
;
895 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
896 wxASSERT_MSG( vi
, wxT("No visual info") );
898 int red_prec
,green_prec
,blue_prec
;
899 int red_shift
,green_shift
,blue_shift
;
900 wxCalcPrecAndShift( vi
->red_mask
, &red_shift
, &red_prec
);
901 wxCalcPrecAndShift( vi
->green_mask
, &green_shift
, &green_prec
);
902 wxCalcPrecAndShift( vi
->blue_mask
, &blue_shift
, &blue_prec
);
903 if (bpp
== 16) bpp
= red_prec
+ green_prec
+ blue_prec
;
905 red_shift_right
= red_shift
;
906 red_shift_left
= 8-red_prec
;
907 green_shift_right
= green_shift
;
908 green_shift_left
= 8-green_prec
;
909 blue_shift_right
= blue_shift
;
910 blue_shift_left
= 8-blue_prec
;
913 use_shift
= (vi
->visual
->c_class
== TrueColor
) || (vi
->visual
->c_class
== DirectColor
);
926 // GdkColormap *cmap = gtk_widget_get_default_colormap();
929 for (int j
= 0; j
< GetHeight(); j
++)
931 for (int i
= 0; i
< GetWidth(); i
++)
933 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
951 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
952 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
953 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
956 else if (cmap
->colors
)
958 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
959 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
960 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
965 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
970 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
983 XDestroyImage( x_image
);
984 if (x_image_mask
) XDestroyImage( x_image_mask
);
990 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
995 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
997 LoadFile( filename
, type
);
1000 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
) )
1003 m_refData
= new wxBitmapRefData();
1005 M_BMPDATA
->m_display
= wxGlobalDisplay();
1007 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
1009 int xscreen
= DefaultScreen( xdisplay
);
1010 Window xroot
= RootWindow( xdisplay
, xscreen
);
1012 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1013 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
, (char *) bits
, width
, height
);
1014 M_BMPDATA
->m_width
= width
;
1015 M_BMPDATA
->m_height
= height
;
1016 M_BMPDATA
->m_bpp
= 1;
1018 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1021 wxBitmap::~wxBitmap()
1025 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
1027 if ( m_refData
!= bmp
.m_refData
)
1033 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1035 return m_refData
== bmp
.m_refData
;
1038 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1040 return m_refData
!= bmp
.m_refData
;
1043 bool wxBitmap::Ok() const
1045 return (m_refData
!= NULL
);
1048 int wxBitmap::GetHeight() const
1050 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1052 return M_BMPDATA
->m_height
;
1055 int wxBitmap::GetWidth() const
1057 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1059 return M_BMPDATA
->m_width
;
1062 int wxBitmap::GetDepth() const
1064 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1066 return M_BMPDATA
->m_bpp
;
1069 wxMask
*wxBitmap::GetMask() const
1071 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1073 return M_BMPDATA
->m_mask
;
1076 void wxBitmap::SetMask( wxMask
*mask
)
1078 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1080 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1082 M_BMPDATA
->m_mask
= mask
;
1085 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1091 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1093 wxCHECK_MSG( Ok() &&
1094 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1095 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1096 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1098 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1099 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1102 wxFAIL_MSG( "wxBitmap::GetSubBitmap not yet implemented" );
1105 if (ret
.GetPixmap())
1107 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1108 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1109 gdk_gc_destroy( gc
);
1113 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1114 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1115 gdk_gc_destroy( gc
);
1120 wxMask
*mask
= new wxMask
;
1121 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1123 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1124 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
1125 gdk_gc_destroy( gc
);
1127 ret
.SetMask( mask
);
1134 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
1136 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1138 // Try to save the bitmap via wxImage handlers:
1140 wxImage
image( *this );
1141 if (image
.Ok()) return image
.SaveFile( name
, type
);
1147 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
1151 if (!wxFileExists(name
)) return FALSE
;
1154 if (type
== wxBITMAP_TYPE_XPM
)
1158 m_refData
= new wxBitmapRefData();
1160 M_BMPDATA
->m_display
= wxGlobalDisplay();
1162 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
1164 int xscreen
= DefaultScreen( xdisplay
);
1165 Window xroot
= RootWindow( xdisplay
, xscreen
);
1167 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1169 XpmAttributes xpmAttr
;
1170 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1175 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
, (char*) name
.c_str(), &pixmap
, &mask
, &xpmAttr
);
1177 if (ErrorStatus
== XpmSuccess
)
1179 M_BMPDATA
->m_width
= xpmAttr
.width
;
1180 M_BMPDATA
->m_height
= xpmAttr
.height
;
1182 M_BMPDATA
->m_bpp
= bpp
; // mono as well?
1184 XpmFreeAttributes(&xpmAttr
);
1186 M_BMPDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1190 M_BMPDATA
->m_mask
= new wxMask
;
1191 M_BMPDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1192 M_BMPDATA
->m_mask
->SetDisplay( xdisplay
);
1203 wxXPMDecoder decoder
;
1204 wxFileInputStream
stream(name
);
1207 wxImage
image(decoder
.ReadFile(stream
));
1209 return CreateFromImage(image
);
1225 else // try if wxImage can load it
1228 if (!image
.LoadFile( name
, type
)) return FALSE
;
1229 if (image
.Ok()) *this = image
.ConvertToBitmap();
1236 wxPalette
*wxBitmap::GetPalette() const
1238 if (!Ok()) return (wxPalette
*) NULL
;
1240 return M_BMPDATA
->m_palette
;
1243 void wxBitmap::SetHeight( int height
)
1245 if (!m_refData
) m_refData
= new wxBitmapRefData();
1247 M_BMPDATA
->m_height
= height
;
1250 void wxBitmap::SetWidth( int width
)
1252 if (!m_refData
) m_refData
= new wxBitmapRefData();
1254 M_BMPDATA
->m_width
= width
;
1257 void wxBitmap::SetDepth( int depth
)
1259 if (!m_refData
) m_refData
= new wxBitmapRefData();
1261 M_BMPDATA
->m_bpp
= depth
;
1264 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1266 if (!m_refData
) m_refData
= new wxBitmapRefData();
1268 M_BMPDATA
->m_pixmap
= pixmap
;
1271 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1273 if (!m_refData
) m_refData
= new wxBitmapRefData();
1275 M_BMPDATA
->m_bitmap
= bitmap
;
1278 WXPixmap
wxBitmap::GetPixmap() const
1280 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1282 return M_BMPDATA
->m_pixmap
;
1285 WXPixmap
wxBitmap::GetBitmap() const
1287 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1289 return M_BMPDATA
->m_bitmap
;
1292 WXDisplay
*wxBitmap::GetDisplay() const
1294 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1296 return M_BMPDATA
->m_display
;