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
);
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 //-----------------------------------------------------------------------------
268 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
270 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
276 wxBitmap::wxBitmap( int width
, int height
, int depth
)
278 Create( width
, height
, depth
);
281 bool wxBitmap::Create( int width
, int height
, int depth
)
285 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
287 m_refData
= new wxBitmapRefData();
289 M_BMPDATA
->m_display
= wxGlobalDisplay();
291 wxASSERT_MSG( M_BMPDATA
->m_display
, wxT("No display") );
293 int xscreen
= DefaultScreen( (Display
*) M_BMPDATA
->m_display
);
294 Window xroot
= RootWindow( (Display
*) M_BMPDATA
->m_display
, xscreen
);
296 int bpp
= DefaultDepth( (Display
*) M_BMPDATA
->m_display
, xscreen
);
297 if (depth
== -1) depth
= bpp
;
299 wxCHECK_MSG( (depth
== bpp
) ||
300 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
302 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
303 M_BMPDATA
->m_width
= width
;
304 M_BMPDATA
->m_height
= height
;
307 M_BMPDATA
->m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
308 M_BMPDATA
->m_bpp
= bpp
;
310 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Bitmap creation failed") );
314 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, 1 );
316 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
318 M_BMPDATA
->m_bpp
= 1;
322 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, depth
);
324 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Pixmap creation failed") );
326 M_BMPDATA
->m_bpp
= depth
;
332 bool wxBitmap::CreateFromXpm( const char **bits
)
338 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
340 m_refData
= new wxBitmapRefData();
342 M_BMPDATA
->m_display
= wxGlobalDisplay();
344 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
346 int xscreen
= DefaultScreen( xdisplay
);
347 Window xroot
= RootWindow( xdisplay
, xscreen
);
349 int bpp
= DefaultDepth( xdisplay
, xscreen
);
351 XpmAttributes xpmAttr
;
352 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
357 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
, &pixmap
, &mask
, &xpmAttr
);
359 if (ErrorStatus
== XpmSuccess
)
361 M_BMPDATA
->m_width
= xpmAttr
.width
;
362 M_BMPDATA
->m_height
= xpmAttr
.height
;
364 M_BMPDATA
->m_bpp
= bpp
; // mono as well?
367 unsigned int depthRet
;
369 unsigned int widthRet
, heightRet
, borderWidthRet
;
370 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
371 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
373 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") )
376 XpmFreeAttributes(&xpmAttr
);
378 M_BMPDATA
->m_pixmap
= (WXPixmap
) pixmap
;
382 M_BMPDATA
->m_mask
= new wxMask
;
383 M_BMPDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
384 M_BMPDATA
->m_mask
->SetDisplay( xdisplay
);
395 wxXPMDecoder decoder
;
396 wxImage
image(decoder
.ReadData(bits
));
398 return CreateFromImage(image
);
406 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
411 wxASSERT_MSG(image
.Ok(), "Invalid wxImage passed to wxBitmap::CreateFromImage.");
415 int w
= image
.GetWidth();
416 int h
= image
.GetHeight();
418 if (!Create(w
, h
, depth
))
421 // Unfortunately the mask has to be screen-depth since
422 // 1-bpp bitmaps don't seem to be supported
423 // TODO: implement transparent drawing, presumably
424 // by doing several blits as per the Windows
425 // implementation because Nano-X doesn't support
427 // TODO: could perhaps speed this function up
428 // by making a buffer of pixel values,
429 // and then calling GrArea to write that to the
430 // pixmap. See demos/nxroach.c.
432 bool hasMask
= image
.HasMask();
434 GC pixmapGC
= GrNewGC();
435 Pixmap pixmap
= (Pixmap
) GetPixmap();
438 Pixmap maskPixmap
= 0;
440 unsigned char maskR
= 0;
441 unsigned char maskG
= 0;
442 unsigned char maskB
= 0;
446 maskR
= image
.GetMaskRed();
447 maskG
= image
.GetMaskGreen();
448 maskB
= image
.GetMaskBlue();
451 maskPixmap
= GrNewPixmap(w
, h
, 0);
456 wxMask
* mask
= new wxMask
;
457 mask
->SetBitmap((WXPixmap
) maskPixmap
);
462 GR_COLOR lastPixmapColour
= 0;
463 GR_COLOR lastMaskColour
= 0;
466 for (i
= 0; i
< w
; i
++)
468 for (j
= 0; j
< h
; j
++)
470 unsigned char red
= image
.GetRed(i
, j
);
471 unsigned char green
= image
.GetGreen(i
, j
);
472 unsigned char blue
= image
.GetBlue(i
, j
);
474 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
476 // Efficiency measure
477 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
479 GrSetGCForeground(pixmapGC
, colour
);
480 lastPixmapColour
= colour
;
483 GrPoint(pixmap
, pixmapGC
, i
, j
);
487 // scan the bitmap for the transparent colour and set the corresponding
488 // pixels in the mask to BLACK and the rest to WHITE
489 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
491 colour
= GR_RGB(0, 0, 0);
495 colour
= GR_RGB(255, 255, 255);
497 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
499 GrSetGCForeground(maskGC
, colour
);
500 lastMaskColour
= colour
;
502 GrPoint(maskPixmap
, maskGC
, i
, j
);
507 GrDestroyGC(pixmapGC
);
517 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
518 wxCHECK_MSG( depth
== -1, FALSE
, wxT("invalid bitmap depth") )
520 m_refData
= new wxBitmapRefData();
522 M_BMPDATA
->m_display
= wxGlobalDisplay();
524 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
526 int xscreen
= DefaultScreen( xdisplay
);
527 Window xroot
= RootWindow( xdisplay
, xscreen
);
528 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
530 int bpp
= wxTheApp
->m_visualDepth
;
532 int width
= image
.GetWidth();
533 int height
= image
.GetHeight();
534 M_BMPDATA
->m_width
= width
;
535 M_BMPDATA
->m_height
= height
;
537 if (depth
!= 1) depth
= bpp
;
538 M_BMPDATA
->m_bpp
= depth
;
542 wxFAIL_MSG( "mono images later" );
548 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
549 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
551 if (data_image
->data
== NULL
)
553 wxLogError( wxT("Out of memory.") ); // TODO clean
557 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
561 XImage
*mask_image
= (XImage
*) NULL
;
564 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
565 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
567 if (mask_image
->data
== NULL
)
569 wxLogError( wxT("Out of memory.") ); // TODO clean
573 wxMask
*mask
= new wxMask();
574 mask
->SetDisplay( xdisplay
);
575 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
580 if (bpp
< 8) bpp
= 8;
584 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
585 byte_order b_o
= RGB
;
589 if ((wxTheApp
->m_visualRedMask
> wxTheApp
->m_visualGreenMask
) && (wxTheApp
->m_visualGreenMask
> wxTheApp
->m_visualBlueMask
)) b_o
= RGB
;
590 else if ((wxTheApp
->m_visualRedMask
> wxTheApp
->m_visualBlueMask
) && (wxTheApp
->m_visualBlueMask
> wxTheApp
->m_visualGreenMask
)) b_o
= RBG
;
591 else if ((wxTheApp
->m_visualBlueMask
> wxTheApp
->m_visualRedMask
) && (wxTheApp
->m_visualRedMask
> wxTheApp
->m_visualGreenMask
)) b_o
= BRG
;
592 else if ((wxTheApp
->m_visualBlueMask
> wxTheApp
->m_visualGreenMask
) && (wxTheApp
->m_visualGreenMask
> wxTheApp
->m_visualRedMask
)) b_o
= BGR
;
593 else if ((wxTheApp
->m_visualGreenMask
> wxTheApp
->m_visualRedMask
) && (wxTheApp
->m_visualRedMask
> wxTheApp
->m_visualBlueMask
)) b_o
= GRB
;
594 else if ((wxTheApp
->m_visualGreenMask
> wxTheApp
->m_visualBlueMask
) && (wxTheApp
->m_visualBlueMask
> wxTheApp
->m_visualRedMask
)) b_o
= GBR
;
597 int r_mask
= image
.GetMaskRed();
598 int g_mask
= image
.GetMaskGreen();
599 int b_mask
= image
.GetMaskBlue();
601 unsigned char* data
= image
.GetData();
602 wxASSERT_MSG( data
, "No image data" );
604 unsigned char *colorCube
= wxTheApp
->m_colorCube
;
606 bool hasMask
= image
.HasMask();
609 for (int y
= 0; y
< height
; y
++)
611 for (int x
= 0; x
< width
; x
++)
622 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
623 XPutPixel( mask_image
, x
, y
, 0 );
625 XPutPixel( mask_image
, x
, y
, 1 );
633 pixel
= colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
634 XPutPixel( data_image
, x
, y
, pixel
);
642 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
643 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
644 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
645 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
646 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
647 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
649 XPutPixel( data_image
, x
, y
, pixel
);
657 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
658 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
659 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
660 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
661 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
662 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
664 XPutPixel( data_image
, x
, y
, pixel
);
669 // I actually don't know if for 16-bit displays, it is alway the green
670 // component or the second component which has 6 bits.
674 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
675 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
676 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
677 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
678 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
679 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
681 XPutPixel( data_image
, x
, y
, pixel
);
690 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
691 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
692 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
693 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
694 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
695 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
697 XPutPixel( data_image
, x
, y
, pixel
);
706 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
707 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
708 XDestroyImage( data_image
);
709 XFreeGC( xdisplay
, gc
);
715 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
716 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
718 XDestroyImage( mask_image
);
719 XFreeGC( xdisplay
, gc
);
728 wxImage
wxBitmap::ConvertToImage() const
732 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
734 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
735 wxASSERT_MSG( xdisplay
, wxT("No display") );
737 int bpp
= wxTheApp
->m_visualDepth
;
740 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
744 XImage
*x_image
= NULL
;
747 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
749 GetWidth(), GetHeight(),
750 AllPlanes
, ZPixmap
);
754 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
756 GetWidth(), GetHeight(),
757 AllPlanes
, ZPixmap
);
760 wxFAIL_MSG( wxT("Ill-formed bitmap") );
763 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
765 image
.Create( GetWidth(), GetHeight() );
766 char unsigned *data
= image
.GetData();
770 XDestroyImage( x_image
);
771 wxFAIL_MSG( wxT("couldn't create image") );
775 XImage
*x_image_mask
= NULL
;
778 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
780 GetWidth(), GetHeight(),
781 AllPlanes
, ZPixmap
);
783 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
786 int red_shift_right
= 0;
787 int green_shift_right
= 0;
788 int blue_shift_right
= 0;
789 int red_shift_left
= 0;
790 int green_shift_left
= 0;
791 int blue_shift_left
= 0;
792 bool use_shift
= FALSE
;
796 red_shift_right
= wxTheApp
->m_visualRedShift
;
797 red_shift_left
= 8-wxTheApp
->m_visualRedPrec
;
798 green_shift_right
= wxTheApp
->m_visualGreenShift
;
799 green_shift_left
= 8-wxTheApp
->m_visualGreenPrec
;
800 blue_shift_right
= wxTheApp
->m_visualBlueShift
;
801 blue_shift_left
= 8-wxTheApp
->m_visualBluePrec
;
803 use_shift
= (wxTheApp
->m_visualType
== GrayScale
) || (wxTheApp
->m_visualType
!= PseudoColor
);
811 XColor
*colors
= (XColor
*) wxTheApp
->m_visualColormap
;
813 int width
= GetWidth();
814 int height
= GetHeight();
816 for (int j
= 0; j
< height
; j
++)
818 for (int i
= 0; i
< width
; i
++)
820 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
838 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
839 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
840 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
844 data
[pos
] = colors
[pixel
].red
>> 8;
845 data
[pos
+1] = colors
[pixel
].green
>> 8;
846 data
[pos
+2] = colors
[pixel
].blue
>> 8;
850 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
855 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
868 XDestroyImage( x_image
);
869 if (x_image_mask
) XDestroyImage( x_image_mask
);
875 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
880 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
882 LoadFile( filename
, type
);
885 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
) )
888 m_refData
= new wxBitmapRefData();
890 M_BMPDATA
->m_display
= wxGlobalDisplay();
892 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
894 int xscreen
= DefaultScreen( xdisplay
);
895 Window xroot
= RootWindow( xdisplay
, xscreen
);
897 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
898 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
, (char *) bits
, width
, height
);
899 M_BMPDATA
->m_width
= width
;
900 M_BMPDATA
->m_height
= height
;
901 M_BMPDATA
->m_bpp
= 1;
903 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
906 wxBitmap::~wxBitmap()
910 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
912 if ( m_refData
!= bmp
.m_refData
)
918 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
920 return m_refData
== bmp
.m_refData
;
923 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
925 return m_refData
!= bmp
.m_refData
;
928 bool wxBitmap::Ok() const
930 return (m_refData
!= NULL
);
933 int wxBitmap::GetHeight() const
935 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
937 return M_BMPDATA
->m_height
;
940 int wxBitmap::GetWidth() const
942 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
944 return M_BMPDATA
->m_width
;
947 int wxBitmap::GetDepth() const
949 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
951 return M_BMPDATA
->m_bpp
;
954 wxMask
*wxBitmap::GetMask() const
956 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
958 return M_BMPDATA
->m_mask
;
961 void wxBitmap::SetMask( wxMask
*mask
)
963 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
965 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
967 M_BMPDATA
->m_mask
= mask
;
970 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
976 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
979 (rect
.x
>= 0) && (rect
.y
>= 0) &&
980 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
981 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
983 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
984 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
987 wxFAIL_MSG( "wxBitmap::GetSubBitmap not yet implemented" );
992 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
993 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
994 gdk_gc_destroy( gc
);
998 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
999 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1000 gdk_gc_destroy( gc
);
1005 wxMask
*mask
= new wxMask
;
1006 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1008 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1009 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
1010 gdk_gc_destroy( gc
);
1012 ret
.SetMask( mask
);
1019 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
1021 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1023 // Try to save the bitmap via wxImage handlers:
1025 wxImage
image( *this );
1026 if (image
.Ok()) return image
.SaveFile( name
, type
);
1032 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
1036 if (!wxFileExists(name
)) return FALSE
;
1039 if (type
== wxBITMAP_TYPE_XPM
)
1043 m_refData
= new wxBitmapRefData();
1045 M_BMPDATA
->m_display
= wxGlobalDisplay();
1047 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
1049 int xscreen
= DefaultScreen( xdisplay
);
1050 Window xroot
= RootWindow( xdisplay
, xscreen
);
1052 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1054 XpmAttributes xpmAttr
;
1055 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1060 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
, (char*) name
.c_str(), &pixmap
, &mask
, &xpmAttr
);
1062 if (ErrorStatus
== XpmSuccess
)
1064 M_BMPDATA
->m_width
= xpmAttr
.width
;
1065 M_BMPDATA
->m_height
= xpmAttr
.height
;
1067 M_BMPDATA
->m_bpp
= bpp
; // mono as well?
1069 XpmFreeAttributes(&xpmAttr
);
1071 M_BMPDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1075 M_BMPDATA
->m_mask
= new wxMask
;
1076 M_BMPDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1077 M_BMPDATA
->m_mask
->SetDisplay( xdisplay
);
1088 wxXPMDecoder decoder
;
1089 wxFileInputStream
stream(name
);
1092 wxImage
image(decoder
.ReadFile(stream
));
1094 return CreateFromImage(image
);
1110 else // try if wxImage can load it
1113 if (!image
.LoadFile( name
, type
)) return FALSE
;
1114 if (image
.Ok()) *this = image
.ConvertToBitmap();
1121 wxPalette
*wxBitmap::GetPalette() const
1123 if (!Ok()) return (wxPalette
*) NULL
;
1125 return M_BMPDATA
->m_palette
;
1128 void wxBitmap::SetHeight( int height
)
1130 if (!m_refData
) m_refData
= new wxBitmapRefData();
1132 M_BMPDATA
->m_height
= height
;
1135 void wxBitmap::SetWidth( int width
)
1137 if (!m_refData
) m_refData
= new wxBitmapRefData();
1139 M_BMPDATA
->m_width
= width
;
1142 void wxBitmap::SetDepth( int depth
)
1144 if (!m_refData
) m_refData
= new wxBitmapRefData();
1146 M_BMPDATA
->m_bpp
= depth
;
1149 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1151 if (!m_refData
) m_refData
= new wxBitmapRefData();
1153 M_BMPDATA
->m_pixmap
= pixmap
;
1156 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1158 if (!m_refData
) m_refData
= new wxBitmapRefData();
1160 M_BMPDATA
->m_bitmap
= bitmap
;
1163 WXPixmap
wxBitmap::GetPixmap() const
1165 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1167 return M_BMPDATA
->m_pixmap
;
1170 WXPixmap
wxBitmap::GetBitmap() const
1172 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1174 return M_BMPDATA
->m_bitmap
;
1177 WXDisplay
*wxBitmap::GetDisplay() const
1179 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1181 return M_BMPDATA
->m_display
;
1185 // Copy from the drawable to the wxImage
1186 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1188 GR_SCREEN_INFO sinfo
;
1190 GR_PIXELVAL
*pixels
;
1191 GR_PALETTE
* palette
= NULL
;
1192 unsigned char rgb
[3], *pp
;
1194 GrGetScreenInfo(&sinfo
);
1196 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1197 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1200 GrGetSystemPalette(palette
);
1203 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1208 image
.Create(width
, height
);
1210 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1214 for(x
= 0; x
< sinfo
.cols
; x
++) {
1216 pp
= (unsigned char *)pixels
+
1217 ((x
+ (y
* sinfo
.cols
)) *
1218 sizeof(GR_PIXELVAL
));
1220 switch(sinfo
.pixtype
) {
1221 /* FIXME: These may need modifying on big endian. */
1222 case MWPF_TRUECOLOR0888
:
1223 case MWPF_TRUECOLOR888
:
1229 rgb
[0] = palette
->palette
[pp
[0]].r
;
1230 rgb
[1] = palette
->palette
[pp
[0]].g
;
1231 rgb
[2] = palette
->palette
[pp
[0]].b
;
1233 case MWPF_TRUECOLOR565
:
1234 rgb
[0] = pp
[1] & 0xf8;
1235 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1236 ((pp
[0] & 0xe0) >> 3);
1237 rgb
[2] = (pp
[0] & 0x1f) << 3;
1239 case MWPF_TRUECOLOR555
:
1240 rgb
[0] = (pp
[1] & 0x7c) << 1;
1241 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1242 ((pp
[0] & 0xe0) >> 2);
1243 rgb
[2] = (pp
[0] & 0x1f) << 3;
1245 case MWPF_TRUECOLOR332
:
1246 rgb
[0] = pp
[0] & 0xe0;
1247 rgb
[1] = (pp
[0] & 0x1c) << 3;
1248 rgb
[2] = (pp
[0] & 0x03) << 6;
1251 fprintf(stderr
, "Unsupported pixel "
1256 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1261 if(palette
) free(palette
);
1267 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1268 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1270 unsigned char rgb
[3], *pp
;
1272 pp
= (unsigned char*) & pixel
;
1274 switch (sinfo
.pixtype
)
1276 /* FIXME: These may need modifying on big endian. */
1277 case MWPF_TRUECOLOR0888
:
1278 case MWPF_TRUECOLOR888
:
1284 rgb
[0] = palette
->palette
[pp
[0]].r
;
1285 rgb
[1] = palette
->palette
[pp
[0]].g
;
1286 rgb
[2] = palette
->palette
[pp
[0]].b
;
1288 case MWPF_TRUECOLOR565
:
1289 rgb
[0] = pp
[1] & 0xf8;
1290 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1291 ((pp
[0] & 0xe0) >> 3);
1292 rgb
[2] = (pp
[0] & 0x1f) << 3;
1294 case MWPF_TRUECOLOR555
:
1295 rgb
[0] = (pp
[1] & 0x7c) << 1;
1296 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1297 ((pp
[0] & 0xe0) >> 2);
1298 rgb
[2] = (pp
[0] & 0x1f) << 3;
1300 case MWPF_TRUECOLOR332
:
1301 rgb
[0] = pp
[0] & 0xe0;
1302 rgb
[1] = (pp
[0] & 0x1c) << 3;
1303 rgb
[2] = (pp
[0] & 0x03) << 6;
1306 fprintf(stderr
, "Unsupported pixel format\n");