L
luser- -droog
I'm trying to convert byte-oriented bitmap data into
32bit words matching the endianness of the machine;
And I'm very confused. The crux is the triple for-loop
here.
compile with -I/usr/include/cairo -lcairo
mask.c:
#include <stdint.h>
#include <stdlib.h>
#include <cairo.h>
#include <X11/Xlib.h>
#include <cairo-xlib.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
int width = 240;
int height = 230;
Display *dis;
int scr;
int depth;
Visual *vis;
XSetWindowAttributes attr;
unsigned long attrmask;
Window win;
cairo_surface_t *surface;
cairo_t *cr;
dis = XOpenDisplay(NULL);
scr = DefaultScreen(dis);
depth = DefaultDepth(dis, scr);
vis = DefaultVisual(dis, scr);
attr.background_pixel = WhitePixel(dis, scr);
attr.border_pixel = BlackPixel(dis, scr);
attr.event_mask = ExposureMask | StructureNotifyMask |
ButtonPressMask;
attrmask = CWColormap | CWBackPixel | CWBorderPixel | CWEventMask;
win = XCreateWindow(dis, RootWindow(dis, scr),
200, 10, //pos
width, height, 5, //width height border
depth,
InputOutput,
vis,
attrmask, &attr);
XMapWindow(dis, win);
surface = cairo_xlib_surface_create(dis, win, vis, width, height);
cr = cairo_create(surface);
cairo_scale(cr, 10, 10); /* make a big turkey */
cairo_set_source_rgb(cr, 0, 0, 1);
{
unsigned char samp[] = {
0x00, 0x3B, 0x00,
0x00, 0x27, 0x00,
0x00, 0x24, 0x80,
0x0E, 0x49, 0x40,
0x11, 0x49, 0x20,
0x14, 0xB2, 0x20,
0x3C, 0xB6, 0x50,
0x75, 0xFE, 0x88,
0x17, 0xFF, 0x8C,
0x17, 0x5F, 0x14,
0x1C, 0x07, 0xE2,
0x38, 0x03, 0xC4,
0x70, 0x31, 0x82,
0xF8, 0xED, 0xFC,
0xB2, 0xBB, 0xC2,
0xBB, 0x6F, 0x84,
0x31, 0xBF, 0xC2,
0x18, 0xEA, 0x3C,
0x0E, 0x3E, 0x00,
0x07, 0xFC, 0x00,
0x03, 0xF8, 0x00,
0x1E, 0x18, 0x00,
0x1F, 0xF8, 0x00 };
int w = 24, h = 23, stride;
cairo_surface_t *mask;
unsigned char *data;
int i,j,k;
uint32_t u;
stride = cairo_format_stride_for_width(CAIRO_FORMAT_A1, w);
data = malloc(h * stride);
#if 0
/* try to make a vertical line */
u = 16;
for (i = 0; i < h * (stride/4); i++) {
*((unsigned long *)(data + i * stride)) = u;
}
#endif
/* convert bytes to 32bit quantities matching
endianness of the machine */
for (i = 0; i < h; i++) { /* each row */
for (j = 0; j < stride/4; j++) { /* each 32bit int in row
*/
u = 0;
for (k = 0; k < w/8; k++) { /* each 8bit byte in 32bit
int */
u << 8;
u |= samp[i*(w/8) + k];
}
*((uint32_t *)(data + i*stride + j*4)) = u;
}
}
mask = cairo_image_surface_create_for_data(data,
CAIRO_FORMAT_A1, w, h, stride);
cairo_mask_surface(cr, mask, 0, 0);
}
XFlush(dis);
sleep(20);
cairo_destroy(cr);
cairo_surface_destroy(surface);
return 0;
}
32bit words matching the endianness of the machine;
And I'm very confused. The crux is the triple for-loop
here.
compile with -I/usr/include/cairo -lcairo
mask.c:
#include <stdint.h>
#include <stdlib.h>
#include <cairo.h>
#include <X11/Xlib.h>
#include <cairo-xlib.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
int width = 240;
int height = 230;
Display *dis;
int scr;
int depth;
Visual *vis;
XSetWindowAttributes attr;
unsigned long attrmask;
Window win;
cairo_surface_t *surface;
cairo_t *cr;
dis = XOpenDisplay(NULL);
scr = DefaultScreen(dis);
depth = DefaultDepth(dis, scr);
vis = DefaultVisual(dis, scr);
attr.background_pixel = WhitePixel(dis, scr);
attr.border_pixel = BlackPixel(dis, scr);
attr.event_mask = ExposureMask | StructureNotifyMask |
ButtonPressMask;
attrmask = CWColormap | CWBackPixel | CWBorderPixel | CWEventMask;
win = XCreateWindow(dis, RootWindow(dis, scr),
200, 10, //pos
width, height, 5, //width height border
depth,
InputOutput,
vis,
attrmask, &attr);
XMapWindow(dis, win);
surface = cairo_xlib_surface_create(dis, win, vis, width, height);
cr = cairo_create(surface);
cairo_scale(cr, 10, 10); /* make a big turkey */
cairo_set_source_rgb(cr, 0, 0, 1);
{
unsigned char samp[] = {
0x00, 0x3B, 0x00,
0x00, 0x27, 0x00,
0x00, 0x24, 0x80,
0x0E, 0x49, 0x40,
0x11, 0x49, 0x20,
0x14, 0xB2, 0x20,
0x3C, 0xB6, 0x50,
0x75, 0xFE, 0x88,
0x17, 0xFF, 0x8C,
0x17, 0x5F, 0x14,
0x1C, 0x07, 0xE2,
0x38, 0x03, 0xC4,
0x70, 0x31, 0x82,
0xF8, 0xED, 0xFC,
0xB2, 0xBB, 0xC2,
0xBB, 0x6F, 0x84,
0x31, 0xBF, 0xC2,
0x18, 0xEA, 0x3C,
0x0E, 0x3E, 0x00,
0x07, 0xFC, 0x00,
0x03, 0xF8, 0x00,
0x1E, 0x18, 0x00,
0x1F, 0xF8, 0x00 };
int w = 24, h = 23, stride;
cairo_surface_t *mask;
unsigned char *data;
int i,j,k;
uint32_t u;
stride = cairo_format_stride_for_width(CAIRO_FORMAT_A1, w);
data = malloc(h * stride);
#if 0
/* try to make a vertical line */
u = 16;
for (i = 0; i < h * (stride/4); i++) {
*((unsigned long *)(data + i * stride)) = u;
}
#endif
/* convert bytes to 32bit quantities matching
endianness of the machine */
for (i = 0; i < h; i++) { /* each row */
for (j = 0; j < stride/4; j++) { /* each 32bit int in row
*/
u = 0;
for (k = 0; k < w/8; k++) { /* each 8bit byte in 32bit
int */
u << 8;
u |= samp[i*(w/8) + k];
}
*((uint32_t *)(data + i*stride + j*4)) = u;
}
}
mask = cairo_image_surface_create_for_data(data,
CAIRO_FORMAT_A1, w, h, stride);
cairo_mask_surface(cr, mask, 0, 0);
}
XFlush(dis);
sleep(20);
cairo_destroy(cr);
cairo_surface_destroy(surface);
return 0;
}