2012-05-09 59 views
2

我想在窗口上繪製一個位圖。我只需要在位圖中繪製與1位對應的像素;那些對應於0位的應該保持不變。我用這樣的代碼:Xlib - 在窗口上只繪製一個位圖的位組

XImage *image; 
uint8_t bitmap[8192]; /* 256 x 256/8 */ 

memset(bitmap, 0xaa, sizeof(bitmap)); 

XSetForeground(g_display, g_gc, 0x000000ff); 
XSetBackground(g_display, g_gc, 0x00000000); 

image = XCreateImage(g_display, g_visual, 1, XYBitmap, 
    0, (char *)bitmap, 256, 256, 32, 0); 
XPutImage(g_display, g_wnd, g_gc, image, 0, 0, 10, 10, 255, 255); 
XFree(image); 

(是的,我知道,直接指定顏色像我這樣做是錯誤的,但它是我的OK現在)。

顯然,位圖的0位是用背景色(即黑色)繪製的。我希望他們不要被吸引。什麼是最好的方法來實現呢?有什麼方法可以指定一個面具或什麼東西?

回答

3

首先,對於非透明位圖,您不需要從位圖數據創建完整深度圖像。創建一位像素圖並使用XCopyPlane

對於透明位圖,您需要操作GC的剪輯蒙版。

Pixmap bitmap; 
...; /* create 1-bit-deep pixmap */ 
XSetClipMask(display, gc, bitmap); 
XSetClipOrigin(display, gc, x, y); 
/* 1-bits of the bitmap are filled with the foreground color, 
    and 0-bits are left intact */ 
XFillRectangle(display, window, gc, x, y, width, heiht);