Roman Dmitrienko
Roman Dmitrienko

Reputation: 4215

Xlib - draw only set bits of a bitmap on a window

I'm trying to draw a bitmap on a window. I only need to draw the pixels corresponding to 1 bits in the bitmap; those corresponding to 0 bits shall be left intact. I'm using code like this:

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);

(yes, I know that directly specifying colors like I do is just wrong, but it is OK for me right now).

Obviously, the 0 bits of the bitmap are drawn with background color (black, that is). I want them not to be drawn at all. What is the best approach to implementing that? Is there any way to specify a mask or something?

Upvotes: 2

Views: 2531

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 119857

First, for non-transparent bitmaps you don't need to create a full-depth image from bitmap data. Create one-bit pixmap and use XCopyPlane.

For transparent bitmaps you need to manipulate your GC's clip mask.

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);

Upvotes: 4

Related Questions