#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrender.h>
#include <stdio.h>

Display *disp;
int scr;
Window root;
Pixmap px_src,px_dst;
GC gc_src, gc_dst;
XImage *i;
unsigned char *ptr;
int ls, x, y;
XGCValues gcv;
XRenderPictFormat *xrf;
XRenderPictureAttributes xrp_attr;
Picture src,dst;
XTrapezoid tz; 

static void dump(XImage*i)
{
	unsigned char * ptr = i->data;
	int x, y, ls = i->bytes_per_line;
	for ( y = 0; y < i->height; y++) {
		unsigned char * p = ptr;
		for ( x = 0; x < i->width; x++)  {
			unsigned long c = i->f.get_pixel(i, x, y);
			printf("%08lx ", c);
		}
		ptr+=ls;
		printf("\n");
	}
}

int main(int argc, char ** argv)
{

	disp = XOpenDisplay(NULL);
	scr = DefaultScreen( disp);
	root = RootWindow( disp, scr);

	px_src = XCreatePixmap( disp, root, 3, 2, 32);
	gc_src = XCreateGC( disp, px_src, 0, NULL);
	px_dst = XCreatePixmap( disp, root, 6, 4, 32);
	gc_dst = XCreateGC( disp, px_dst, 0, NULL);

	xrf = XRenderFindStandardFormat(disp, PictStandardARGB32);
	xrp_attr.repeat = RepeatNormal;
	src = XRenderCreatePicture( disp, px_src, xrf, CPRepeat, &xrp_attr);
	dst = XRenderCreatePicture( disp, px_dst, xrf, 0, NULL);

	XSetForeground( disp, gc_src, 0x00000000 );
	XDrawPoint( disp, px_src, gc_src, 0, 0);
	XSetForeground( disp, gc_src, 0x80000000 );
	XDrawPoint( disp, px_src, gc_src, 1, 0);
	XSetForeground( disp, gc_src, 0xFF000000 );
	XDrawPoint( disp, px_src, gc_src, 2, 0);
	XSetForeground( disp, gc_src, 0x00FFFFFF );
	XDrawPoint( disp, px_src, gc_src, 0, 1);
	XSetForeground( disp, gc_src, 0x80FFFFFF );
	XDrawPoint( disp, px_src, gc_src, 1, 1);
	XSetForeground( disp, gc_src, 0xFFFFFFFF );
	XDrawPoint( disp, px_src, gc_src, 2, 1);

	XSetForeground( disp, gc_dst, 0xFFFFFFFF );
	XFillRectangle( disp, px_dst, gc_dst, 0, 0, 6, 4);

	tz.left.p1.x  = tz.left.p2.x  = XDoubleToFixed(0.0);
	tz.right.p1.x = tz.right.p2.x = XDoubleToFixed(6.0);
	tz.top        = tz.left.p2.y  = tz.right.p2.y = XDoubleToFixed(0.0);
	tz.bottom     = tz.left.p1.y  = tz.right.p1.y = XDoubleToFixed(4.0);
	XRenderCompositeTrapezoids(disp, PictOpOver, src, dst, XRenderFindStandardFormat(disp, PictStandardA8), 0, 0, &tz, 1);
	XFlush(disp);
	i = XGetImage( disp, px_dst, 0, 0, 6, 4, AllPlanes, ZPixmap);
	dump(i);
	XDestroyImage( i);



	XFreeGC(disp, gc_src);
	XFreeGC( disp, gc_dst);
	XRenderFreePicture( disp, src);
	XRenderFreePicture( disp, dst);
	XFreePixmap(disp, px_src);
	XFreePixmap(disp, px_dst);

	XCloseDisplay(disp);
	return 0;
}
