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
;
101 int xscreen
= DefaultScreen( xdisplay
);
102 Window xroot
= RootWindow( xdisplay
, xscreen
);
103 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
104 int bpp
= DefaultDepth( xdisplay
, xscreen
);
106 m_bitmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, image
.GetWidth(), image
.GetHeight(), 1 );
107 GC gc
= XCreateGC( xdisplay
, (Pixmap
) m_bitmap
, 0, NULL
);
109 XSetForeground( xdisplay
, gc
, WhitePixel(xdisplay
,xscreen
) );
110 XSetFillStyle( xdisplay
, gc
, FillSolid
);
111 XFillRectangle( xdisplay
, (Pixmap
) m_bitmap
, gc
, 0, 0, image
.GetWidth(), image
.GetHeight() );
113 unsigned char *data
= image
.GetData();
116 unsigned char red
= colour
.Red();
117 unsigned char green
= colour
.Green();
118 unsigned char blue
= colour
.Blue();
120 XVisualInfo vinfo_template
;
123 vinfo_template
.visual
= xvisual
;
124 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
125 vinfo_template
.depth
= bpp
;
128 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
129 wxASSERT_MSG( vi
, wxT("No visual info") );
131 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
135 green
= green
& 0xf8;
141 green
= green
& 0xfc;
147 green
= green
& 0xf0;
151 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
153 for (int j
= 0; j
< image
.GetHeight(); j
++)
157 for (i
= 0; i
< image
.GetWidth(); i
++)
159 if ((data
[index
] == red
) &&
160 (data
[index
+1] == green
) &&
161 (data
[index
+2] == blue
))
170 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
177 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
180 XFreeGC( xdisplay
, gc
);
189 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
192 wxPalette
*pal
= bitmap
.GetPalette();
194 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
196 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
198 return Create(bitmap
, wxColour(r
, g
, b
));
201 bool wxMask::Create( const wxBitmap
& bitmap
)
206 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
210 if (!bitmap
.Ok()) return FALSE
;
212 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
214 m_display
= bitmap
.GetDisplay();
216 int xscreen
= DefaultScreen( (Display
*) m_display
);
217 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
219 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
221 if (!m_bitmap
) return FALSE
;
223 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
225 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
226 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
228 XFreeGC( (Display
*) m_display
, gc
);
237 //-----------------------------------------------------------------------------
239 //-----------------------------------------------------------------------------
241 class wxBitmapRefData
: public wxObjectRefData
249 WXDisplay
*m_display
;
254 wxPalette
*m_palette
;
257 wxBitmapRefData::wxBitmapRefData()
262 m_mask
= (wxMask
*) NULL
;
266 m_palette
= (wxPalette
*) NULL
;
269 wxBitmapRefData::~wxBitmapRefData()
271 if (m_pixmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_pixmap
);
272 if (m_bitmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
273 if (m_mask
) delete m_mask
;
274 if (m_palette
) delete m_palette
;
277 //-----------------------------------------------------------------------------
279 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
281 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
287 wxBitmap::wxBitmap( int width
, int height
, int depth
)
289 Create( width
, height
, depth
);
292 bool wxBitmap::Create( int width
, int height
, int depth
)
296 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
298 m_refData
= new wxBitmapRefData();
300 M_BMPDATA
->m_display
= wxGlobalDisplay();
302 wxASSERT_MSG( M_BMPDATA
->m_display
, wxT("No display") );
304 int xscreen
= DefaultScreen( (Display
*) M_BMPDATA
->m_display
);
305 Window xroot
= RootWindow( (Display
*) M_BMPDATA
->m_display
, xscreen
);
307 int bpp
= DefaultDepth( (Display
*) M_BMPDATA
->m_display
, xscreen
);
308 if (depth
== -1) depth
= bpp
;
310 wxCHECK_MSG( (depth
== bpp
) ||
311 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
313 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
314 M_BMPDATA
->m_width
= width
;
315 M_BMPDATA
->m_height
= height
;
318 M_BMPDATA
->m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
319 M_BMPDATA
->m_bpp
= bpp
;
321 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Bitmap creation failed") );
325 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, 1 );
327 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
329 M_BMPDATA
->m_bpp
= 1;
333 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, depth
);
335 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Pixmap creation failed") );
337 M_BMPDATA
->m_bpp
= depth
;
343 bool wxBitmap::CreateFromXpm( const char **bits
)
349 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
351 m_refData
= new wxBitmapRefData();
353 M_BMPDATA
->m_display
= wxGlobalDisplay();
355 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
357 int xscreen
= DefaultScreen( xdisplay
);
358 Window xroot
= RootWindow( xdisplay
, xscreen
);
360 int bpp
= DefaultDepth( xdisplay
, xscreen
);
362 XpmAttributes xpmAttr
;
363 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
368 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
, &pixmap
, &mask
, &xpmAttr
);
370 if (ErrorStatus
== XpmSuccess
)
372 M_BMPDATA
->m_width
= xpmAttr
.width
;
373 M_BMPDATA
->m_height
= xpmAttr
.height
;
375 M_BMPDATA
->m_bpp
= bpp
; // mono as well?
378 unsigned int depthRet
;
380 unsigned int widthRet
, heightRet
, borderWidthRet
;
381 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
382 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
384 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") )
387 XpmFreeAttributes(&xpmAttr
);
389 M_BMPDATA
->m_pixmap
= (WXPixmap
) pixmap
;
393 M_BMPDATA
->m_mask
= new wxMask
;
394 M_BMPDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
395 M_BMPDATA
->m_mask
->SetDisplay( xdisplay
);
406 wxXPMDecoder decoder
;
407 wxImage
image(decoder
.ReadData(bits
));
409 return CreateFromImage(image
);
417 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
422 wxASSERT_MSG(image
.Ok(), "Invalid wxImage passed to wxBitmap::CreateFromImage.");
426 int w
= image
.GetWidth();
427 int h
= image
.GetHeight();
429 if (!Create(w
, h
, depth
))
432 // Unfortunately the mask has to be screen-depth since
433 // 1-bpp bitmaps don't seem to be supported
434 // TODO: implement transparent drawing, presumably
435 // by doing several blits as per the Windows
436 // implementation because Nano-X doesn't support
438 // TODO: could perhaps speed this function up
439 // by making a buffer of pixel values,
440 // and then calling GrArea to write that to the
441 // pixmap. See demos/nxroach.c.
443 bool hasMask
= image
.HasMask();
445 GC pixmapGC
= GrNewGC();
446 Pixmap pixmap
= (Pixmap
) GetPixmap();
449 Pixmap maskPixmap
= 0;
451 unsigned char maskR
= 0;
452 unsigned char maskG
= 0;
453 unsigned char maskB
= 0;
457 maskR
= image
.GetMaskRed();
458 maskG
= image
.GetMaskGreen();
459 maskB
= image
.GetMaskBlue();
462 maskPixmap
= GrNewPixmap(w
, h
, 0);
467 wxMask
* mask
= new wxMask
;
468 mask
->SetBitmap((WXPixmap
) maskPixmap
);
473 GR_COLOR lastPixmapColour
= 0;
474 GR_COLOR lastMaskColour
= 0;
477 for (i
= 0; i
< w
; i
++)
479 for (j
= 0; j
< h
; j
++)
481 unsigned char red
= image
.GetRed(i
, j
);
482 unsigned char green
= image
.GetGreen(i
, j
);
483 unsigned char blue
= image
.GetBlue(i
, j
);
485 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
487 // Efficiency measure
488 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
490 GrSetGCForeground(pixmapGC
, colour
);
491 lastPixmapColour
= colour
;
494 GrPoint(pixmap
, pixmapGC
, i
, j
);
498 // scan the bitmap for the transparent colour and set the corresponding
499 // pixels in the mask to BLACK and the rest to WHITE
500 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
502 colour
= GR_RGB(0, 0, 0);
506 colour
= GR_RGB(255, 255, 255);
508 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
510 GrSetGCForeground(maskGC
, colour
);
511 lastMaskColour
= colour
;
513 GrPoint(maskPixmap
, maskGC
, i
, j
);
518 GrDestroyGC(pixmapGC
);
528 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
529 wxCHECK_MSG( depth
== -1, FALSE
, wxT("invalid bitmap depth") )
531 m_refData
= new wxBitmapRefData();
533 M_BMPDATA
->m_display
= wxGlobalDisplay();
535 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
537 int xscreen
= DefaultScreen( xdisplay
);
538 Window xroot
= RootWindow( xdisplay
, xscreen
);
539 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
541 int bpp
= DefaultDepth( xdisplay
, xscreen
);
543 int width
= image
.GetWidth();
544 int height
= image
.GetHeight();
545 M_BMPDATA
->m_width
= width
;
546 M_BMPDATA
->m_height
= height
;
548 if (depth
!= 1) depth
= bpp
;
549 M_BMPDATA
->m_bpp
= depth
;
553 wxFAIL_MSG( "mono images later" );
559 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
560 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
562 if (data_image
->data
== NULL
)
564 wxLogError( wxT("Out of memory.") ); // TODO clean
568 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
572 XImage
*mask_image
= (XImage
*) NULL
;
575 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
576 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
578 if (mask_image
->data
== NULL
)
580 wxLogError( wxT("Out of memory.") ); // TODO clean
584 wxMask
*mask
= new wxMask();
585 mask
->SetDisplay( xdisplay
);
586 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
593 XVisualInfo vinfo_template
;
596 vinfo_template
.visual
= xvisual
;
597 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
598 vinfo_template
.depth
= bpp
;
601 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
602 wxASSERT_MSG( vi
, wxT("No visual info") );
604 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
605 if (bpp
< 8) bpp
= 8;
609 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
610 byte_order b_o
= RGB
;
614 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
615 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RBG
;
616 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
617 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
618 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
619 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
624 int r_mask
= image
.GetMaskRed();
625 int g_mask
= image
.GetMaskGreen();
626 int b_mask
= image
.GetMaskBlue();
628 unsigned char* data
= image
.GetData();
629 wxASSERT_MSG( data
, "No image data" );
631 bool hasMask
= image
.HasMask();
634 for (int y
= 0; y
< height
; y
++)
636 for (int x
= 0; x
< width
; x
++)
647 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
648 XPutPixel( mask_image
, x
, y
, 0 );
650 XPutPixel( mask_image
, x
, y
, 1 );
659 if (wxTheApp
->m_colorCube
)
661 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
665 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
666 GdkColor
*colors
= cmap
->colors
;
667 int max
= 3 * (65536);
669 for (int i
= 0; i
< cmap
->size
; i
++)
671 int rdiff
= (r
<< 8) - colors
[i
].red
;
672 int gdiff
= (g
<< 8) - colors
[i
].green
;
673 int bdiff
= (b
<< 8) - colors
[i
].blue
;
674 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
675 if (sum
< max
) { pixel
= i
; max
= sum
; }
679 XPutPixel( data_image
, x
, y
, pixel
);
687 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
688 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
689 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
690 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
691 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
692 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
694 XPutPixel( data_image
, x
, y
, pixel
);
702 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
703 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
704 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
705 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
706 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
707 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
709 XPutPixel( data_image
, x
, y
, pixel
);
714 // I actually don't know if for 16-bit displays, it is alway the green
715 // component or the second component which has 6 bits.
719 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
720 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
721 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
722 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
723 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
724 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
726 XPutPixel( data_image
, x
, y
, pixel
);
735 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
736 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
737 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
738 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
739 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
740 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
742 XPutPixel( data_image
, x
, y
, pixel
);
751 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
752 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
754 XSync(wxGlobalDisplay(), False
);
757 XDestroyImage( data_image
);
758 XFreeGC( xdisplay
, gc
);
764 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
765 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
767 XDestroyImage( mask_image
);
768 XFreeGC( xdisplay
, gc
);
777 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
782 while (!(mask
& 0x1))
795 wxImage
wxBitmap::ConvertToImage() const
799 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
801 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
802 wxASSERT_MSG( xdisplay
, wxT("No display") );
804 int xscreen
= DefaultScreen( xdisplay
);
805 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
807 int bpp
= DefaultDepth( xdisplay
, xscreen
);
810 wxGetImageFromDrawable((Pixmap
) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image
);
814 XImage
*x_image
= NULL
;
817 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
819 GetWidth(), GetHeight(),
820 AllPlanes
, ZPixmap
);
824 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
826 GetWidth(), GetHeight(),
827 AllPlanes
, ZPixmap
);
830 wxFAIL_MSG( wxT("Ill-formed bitmap") );
833 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
835 image
.Create( GetWidth(), GetHeight() );
836 char unsigned *data
= image
.GetData();
840 XDestroyImage( x_image
);
841 wxFAIL_MSG( wxT("couldn't create image") );
845 XImage
*x_image_mask
= NULL
;
848 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
850 GetWidth(), GetHeight(),
851 AllPlanes
, ZPixmap
);
853 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
856 int red_shift_right
= 0;
857 int green_shift_right
= 0;
858 int blue_shift_right
= 0;
859 int red_shift_left
= 0;
860 int green_shift_left
= 0;
861 int blue_shift_left
= 0;
862 bool use_shift
= FALSE
;
868 XVisualInfo vinfo_template
;
871 vinfo_template
.visual
= xvisual
;
872 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
873 vinfo_template
.depth
= bpp
;
876 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
877 wxASSERT_MSG( vi
, wxT("No visual info") );
879 int red_prec
,green_prec
,blue_prec
;
880 int red_shift
,green_shift
,blue_shift
;
881 wxCalcPrecAndShift( vi
->red_mask
, &red_shift
, &red_prec
);
882 wxCalcPrecAndShift( vi
->green_mask
, &green_shift
, &green_prec
);
883 wxCalcPrecAndShift( vi
->blue_mask
, &blue_shift
, &blue_prec
);
884 if (bpp
== 16) bpp
= red_prec
+ green_prec
+ blue_prec
;
886 red_shift_right
= red_shift
;
887 red_shift_left
= 8-red_prec
;
888 green_shift_right
= green_shift
;
889 green_shift_left
= 8-green_prec
;
890 blue_shift_right
= blue_shift
;
891 blue_shift_left
= 8-blue_prec
;
894 use_shift
= (vi
->visual
->c_class
== TrueColor
) || (vi
->visual
->c_class
== DirectColor
);
907 // GdkColormap *cmap = gtk_widget_get_default_colormap();
910 for (int j
= 0; j
< GetHeight(); j
++)
912 for (int i
= 0; i
< GetWidth(); i
++)
914 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
932 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
933 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
934 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
937 else if (cmap
->colors
)
939 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
940 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
941 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
946 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
951 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
964 XDestroyImage( x_image
);
965 if (x_image_mask
) XDestroyImage( x_image_mask
);
971 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
976 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
978 LoadFile( filename
, type
);
981 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
) )
984 m_refData
= new wxBitmapRefData();
986 M_BMPDATA
->m_display
= wxGlobalDisplay();
988 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
990 int xscreen
= DefaultScreen( xdisplay
);
991 Window xroot
= RootWindow( xdisplay
, xscreen
);
993 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
994 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
, (char *) bits
, width
, height
);
995 M_BMPDATA
->m_width
= width
;
996 M_BMPDATA
->m_height
= height
;
997 M_BMPDATA
->m_bpp
= 1;
999 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1002 wxBitmap::~wxBitmap()
1006 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
1008 if ( m_refData
!= bmp
.m_refData
)
1014 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1016 return m_refData
== bmp
.m_refData
;
1019 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1021 return m_refData
!= bmp
.m_refData
;
1024 bool wxBitmap::Ok() const
1026 return (m_refData
!= NULL
);
1029 int wxBitmap::GetHeight() const
1031 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1033 return M_BMPDATA
->m_height
;
1036 int wxBitmap::GetWidth() const
1038 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1040 return M_BMPDATA
->m_width
;
1043 int wxBitmap::GetDepth() const
1045 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1047 return M_BMPDATA
->m_bpp
;
1050 wxMask
*wxBitmap::GetMask() const
1052 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1054 return M_BMPDATA
->m_mask
;
1057 void wxBitmap::SetMask( wxMask
*mask
)
1059 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1061 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1063 M_BMPDATA
->m_mask
= mask
;
1066 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1072 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1074 wxCHECK_MSG( Ok() &&
1075 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1076 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1077 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1079 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1080 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1083 wxFAIL_MSG( "wxBitmap::GetSubBitmap not yet implemented" );
1086 if (ret
.GetPixmap())
1088 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1089 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1090 gdk_gc_destroy( gc
);
1094 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1095 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1096 gdk_gc_destroy( gc
);
1101 wxMask
*mask
= new wxMask
;
1102 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1104 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1105 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
1106 gdk_gc_destroy( gc
);
1108 ret
.SetMask( mask
);
1115 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
1117 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1119 // Try to save the bitmap via wxImage handlers:
1121 wxImage
image( *this );
1122 if (image
.Ok()) return image
.SaveFile( name
, type
);
1128 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
1132 if (!wxFileExists(name
)) return FALSE
;
1135 if (type
== wxBITMAP_TYPE_XPM
)
1139 m_refData
= new wxBitmapRefData();
1141 M_BMPDATA
->m_display
= wxGlobalDisplay();
1143 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
1145 int xscreen
= DefaultScreen( xdisplay
);
1146 Window xroot
= RootWindow( xdisplay
, xscreen
);
1148 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1150 XpmAttributes xpmAttr
;
1151 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1156 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
, (char*) name
.c_str(), &pixmap
, &mask
, &xpmAttr
);
1158 if (ErrorStatus
== XpmSuccess
)
1160 M_BMPDATA
->m_width
= xpmAttr
.width
;
1161 M_BMPDATA
->m_height
= xpmAttr
.height
;
1163 M_BMPDATA
->m_bpp
= bpp
; // mono as well?
1165 XpmFreeAttributes(&xpmAttr
);
1167 M_BMPDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1171 M_BMPDATA
->m_mask
= new wxMask
;
1172 M_BMPDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1173 M_BMPDATA
->m_mask
->SetDisplay( xdisplay
);
1184 wxXPMDecoder decoder
;
1185 wxFileInputStream
stream(name
);
1188 wxImage
image(decoder
.ReadFile(stream
));
1190 return CreateFromImage(image
);
1206 else // try if wxImage can load it
1209 if (!image
.LoadFile( name
, type
)) return FALSE
;
1210 if (image
.Ok()) *this = image
.ConvertToBitmap();
1217 wxPalette
*wxBitmap::GetPalette() const
1219 if (!Ok()) return (wxPalette
*) NULL
;
1221 return M_BMPDATA
->m_palette
;
1224 void wxBitmap::SetHeight( int height
)
1226 if (!m_refData
) m_refData
= new wxBitmapRefData();
1228 M_BMPDATA
->m_height
= height
;
1231 void wxBitmap::SetWidth( int width
)
1233 if (!m_refData
) m_refData
= new wxBitmapRefData();
1235 M_BMPDATA
->m_width
= width
;
1238 void wxBitmap::SetDepth( int depth
)
1240 if (!m_refData
) m_refData
= new wxBitmapRefData();
1242 M_BMPDATA
->m_bpp
= depth
;
1245 void wxBitmap::SetPixmap( WXPixmap pixmap
)
1247 if (!m_refData
) m_refData
= new wxBitmapRefData();
1249 M_BMPDATA
->m_pixmap
= pixmap
;
1252 void wxBitmap::SetBitmap( WXPixmap bitmap
)
1254 if (!m_refData
) m_refData
= new wxBitmapRefData();
1256 M_BMPDATA
->m_bitmap
= bitmap
;
1259 WXPixmap
wxBitmap::GetPixmap() const
1261 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1263 return M_BMPDATA
->m_pixmap
;
1266 WXPixmap
wxBitmap::GetBitmap() const
1268 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1270 return M_BMPDATA
->m_bitmap
;
1273 WXDisplay
*wxBitmap::GetDisplay() const
1275 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1277 return M_BMPDATA
->m_display
;
1281 // Copy from the drawable to the wxImage
1282 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1284 GR_SCREEN_INFO sinfo
;
1286 GR_PIXELVAL
*pixels
;
1287 GR_PALETTE
* palette
= NULL
;
1288 unsigned char rgb
[3], *pp
;
1290 GrGetScreenInfo(&sinfo
);
1292 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1293 if(!(palette
= (GR_PALETTE
*) malloc(sizeof(GR_PALETTE
)))) {
1296 GrGetSystemPalette(palette
);
1299 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1304 image
.Create(width
, height
);
1306 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1310 for(x
= 0; x
< sinfo
.cols
; x
++) {
1312 pp
= (unsigned char *)pixels
+
1313 ((x
+ (y
* sinfo
.cols
)) *
1314 sizeof(GR_PIXELVAL
));
1316 switch(sinfo
.pixtype
) {
1317 /* FIXME: These may need modifying on big endian. */
1318 case MWPF_TRUECOLOR0888
:
1319 case MWPF_TRUECOLOR888
:
1325 rgb
[0] = palette
->palette
[pp
[0]].r
;
1326 rgb
[1] = palette
->palette
[pp
[0]].g
;
1327 rgb
[2] = palette
->palette
[pp
[0]].b
;
1329 case MWPF_TRUECOLOR565
:
1330 rgb
[0] = pp
[1] & 0xf8;
1331 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1332 ((pp
[0] & 0xe0) >> 3);
1333 rgb
[2] = (pp
[0] & 0x1f) << 3;
1335 case MWPF_TRUECOLOR555
:
1336 rgb
[0] = (pp
[1] & 0x7c) << 1;
1337 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1338 ((pp
[0] & 0xe0) >> 2);
1339 rgb
[2] = (pp
[0] & 0x1f) << 3;
1341 case MWPF_TRUECOLOR332
:
1342 rgb
[0] = pp
[0] & 0xe0;
1343 rgb
[1] = (pp
[0] & 0x1c) << 3;
1344 rgb
[2] = (pp
[0] & 0x03) << 6;
1347 fprintf(stderr
, "Unsupported pixel "
1352 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1357 if(palette
) free(palette
);
1363 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1364 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1366 unsigned char rgb
[3], *pp
;
1368 pp
= (unsigned char*) & pixel
;
1370 switch (sinfo
.pixtype
)
1372 /* FIXME: These may need modifying on big endian. */
1373 case MWPF_TRUECOLOR0888
:
1374 case MWPF_TRUECOLOR888
:
1380 rgb
[0] = palette
->palette
[pp
[0]].r
;
1381 rgb
[1] = palette
->palette
[pp
[0]].g
;
1382 rgb
[2] = palette
->palette
[pp
[0]].b
;
1384 case MWPF_TRUECOLOR565
:
1385 rgb
[0] = pp
[1] & 0xf8;
1386 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1387 ((pp
[0] & 0xe0) >> 3);
1388 rgb
[2] = (pp
[0] & 0x1f) << 3;
1390 case MWPF_TRUECOLOR555
:
1391 rgb
[0] = (pp
[1] & 0x7c) << 1;
1392 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1393 ((pp
[0] & 0xe0) >> 2);
1394 rgb
[2] = (pp
[0] & 0x1f) << 3;
1396 case MWPF_TRUECOLOR332
:
1397 rgb
[0] = pp
[0] & 0xe0;
1398 rgb
[1] = (pp
[0] & 0x1c) << 3;
1399 rgb
[2] = (pp
[0] & 0x03) << 6;
1402 fprintf(stderr
, "Unsupported pixel format\n");