// gcc test.c -o test.bin -L/usr/X11R6/lib -lXrender -lXcomposite -lX11 -lXext -lImlib2
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define AlphaMask	0xFF000000
#define ColorMask	0x00FFFFFF
#define RedMask	0x00FF0000
#define GreenMask	0x0000FF00
#define BlueMask	0x000000FF

Window               id,root;
Display              *dpy;
Visual               *visual;
GC                   gc;
XGCValues            values;
XEvent               event;
Cursor               cursor;
int                  screen, depth;
Colormap             colmap;
Window               root;
XSetWindowAttributes a;
XRenderColor         XRcolor;
Pixmap               pixmap, mask;
XGlyphInfo           ext;
unsigned long        wmask, atr;
int                  image_width, image_height, i, j, k, l, scr, count, count_a;
Imlib_Image          image;
XRenderPictFormat    *format_win, *format_pix, *format_mask, fw, fp, fm;
Picture              p_win, p_pix, p_mask;
XRenderColor         rc;
XRenderPictureAttributes pwin, ppix, pmask;
char*                display_string = NULL;
unsigned char *buffer;
unsigned int r, g, b, al;
unsigned char *AlphaBuffer;
unsigned int *ColorBuffer;

DATA32 *Imlib_data;
XImage ximage, *xmask;

/*
// Borrowed from Carl Worth's argb_cairo.c
static Visual *
find_argb_visual (Display *dpy, int scr)
{
    XVisualInfo	*xvi;
    XVisualInfo	template;
    int nvi, i;
    XRenderPictFormat *format;
    Visual *visual;

    template.screen = scr;
    template.depth = 32;
    template.class = TrueColor;
    xvi = XGetVisualInfo (dpy,
			  VisualScreenMask |
			  VisualDepthMask |
			  VisualClassMask,
			  &template,
			  &nvi);
    if (xvi == NULL)
	return NULL;

    visual = NULL;
    for (i = 0; i < nvi; i++)
    {
	   format = XRenderFindVisualFormat (dpy, xvi[i].visual);
	   if (format->type == PictTypeDirect && format->direct.alphaMask)
	   {
		  visual = xvi[i].visual;
		  break;
	   }
    }
    XFree (xvi);
    return visual;
}
*/
int
main(){

    display_string = getenv("DISPLAY");
    if (!(dpy = (Display*) XOpenDisplay(display_string))) {
	   fprintf(stderr, "Couldn't open display '%s'\n",
			 (display_string ? display_string : "NULL"));
	   return 1;
    }

    scr    = DefaultScreen(dpy);
    root   = RootWindow(dpy, scr);
    visual = DefaultVisual(dpy, screen);
    depth  = DefaultDepth(dpy,scr);
    colmap = DefaultColormap(dpy, scr);
/*
    XCompositeRedirectSubwindows( dpy, root,
						    CompositeRedirectAutomatic );
*/
    imlib_context_set_display(dpy);
    imlib_context_set_visual(visual);
    imlib_context_set_colormap(colmap);

    a.override_redirect = 1;
    a.background_pixel  = 0xffff;
    a.event_mask = ExposureMask | StructureNotifyMask | ButtonPressMask | KeyPressMask;
    a.border_pixel = 0;
    //a.colormap = colmap;
    wmask = CWEventMask | CWBackPixel | CWBorderPixel |  CWOverrideRedirect;

    image = imlib_load_image("test.png");
    imlib_context_set_image(image);

    image_width  = imlib_image_get_width();
    image_height = imlib_image_get_height();
    Imlib_data = imlib_image_get_data_for_reading_only();

    id = XCreateWindow(dpy, root, 850, 650, image_width, image_height,
				    0, depth, InputOutput,
				    visual, wmask, &a);

//    AlphaBuffer = malloc(image_width*image_height*sizeof(unsigned char));
    ColorBuffer = malloc(image_width*image_height*4*sizeof(unsigned int));

    printf("width: %i\nheight: %i\n", image_width, image_height);

    k=0, count=0, count_a=0;

    printf(" Original\t\tAlpha\n");
    printf(" ========\t\t=========\n");
    for(i=0; i>16 & 0xff;

		  ColorBuffer[i*image_width+j+count+1] =
			 (Imlib_data[i*image_width+j] & GreenMask)>>8 & 0xff;

		  ColorBuffer[i*image_width+j+count+2] =
			 (Imlib_data[i*image_width+j] & BlueMask) & 0xff;

		  //count+=3;

//			 (Imlib_data[i*image_width+j] & ColorMask)>>16;

/*
		  ColorBuffer[i*image_width+j] =
			 (Imlib_data[i*image_width+j] & RedMask) >> 16;

		  ColorBuffer[i*image_width+j+1] =
			 (Imlib_data[i*image_width+j] & GreenMask) >> 8;

		  ColorBuffer[i*image_width+j+2] =
			 (Imlib_data[i*image_width+j] & BlueMask);

		  ColorBuffer[i*image_width+j+3] = 0;
*/

//		  ColorBuffer[i*image_width+j] = (Imlib_data[i*image_width+j] & AlphaMask) >> 24;
		  //printf("%x", ColorBuffer[i*image_width+j]);

		  if(i == 53 && j < 10)
			 printf("%x\t0x%x\t\t0x%x\t\t0x%x\n",
				   Imlib_data[i*image_width+j],
				   ColorBuffer[i*image_width+j+count],
				   ColorBuffer[i*image_width+j+count+1],
				   ColorBuffer[i*image_width+j+count+2]);

	   }
	   //count+=2;
    }

    pixmap = XCreatePixmap(dpy, id, image_width, image_height, depth);

    switch(visual->class){
	   case TrueColor:
		  printf("TrueColor\n");
		  break;
    }


    xmask = XCreateImage(dpy, visual, depth, ZPixmap, 0, NULL,
					image_width,
					image_height,
					32,
					308);
//308);

    xmask->data = ColorBuffer;

    printf("visual red_mask: 0x%x\n", visual->red_mask);
    printf("Bytes per line: %i\n", xmask->bytes_per_line);
    printf("Bytes per pixel: %i\n", xmask->bits_per_pixel);

//image_width*sizeof(unsigned int));
//image_width);

    cursor = XCreateFontCursor (dpy, XC_hand2);
    XDefineCursor(dpy, id, cursor);

    XMapWindow(dpy, id);
    XSync(dpy, False);

    values.foreground = 0xffff;
    values.background = 0x0;
    gc = XCreateGC (dpy, id, GCForeground | GCBackground, &values);


//    XPutImage(dpy, xmask, gc, pixmap, 0, 0, 0, 0, image_width, image_height);

///    XCopyArea(dpy, xmask, pixmap, gc, 0, 0, image_width, image_height, 0, 0);

    do{
	   XNextEvent(dpy,&event);
	   XClearWindow(dpy, id);

//	   XPutImage(dpy, pixmap, gc, xmask, 0, 0, 0, 0, image_width, image_height);
//	   XSetWindowBackgroundPixmap(dpy, id, pixmap);


	   XPutImage(dpy, id, gc,
			   xmask, 0, 0, 0, 0,
			   image_width, image_height);
/*
	   if(event.type == ButtonPress){
		  XClearWindow(dpy, id);
		  XPutImage(dpy, pixmap, gc, xmask, 0, 0, 0, 0, image_width, image_height);
//	   XSetWindowBackgroundPixmap(dpy, id, pixmap);
	   }

	   XClearArea (dpy, id, 0, 0, 0, 0, False);
	   XRenderComposite (dpy,
					 PictOpSrc,
					 p_pix,
					 p_mask,
					 p_win,
					 0, 0, 0, 0, 0, 0,
					 image_width, image_height);
*/
    }while(event.type != ButtonPress);

//    XRenderFreePicture (dpy, p_win);
//    XRenderFreePicture (dpy, p_mask);
//    XRenderFreePicture (dpy, p_pix);
//    XFreePixmap(dpy, pixmap);
//    XFreePixmap(dpy, mask);

    //XFree(&ximage);
//    free((char*)AlphaBuffer);
    free((char*)ColorBuffer);
    free((DATA32*)Imlib_data);
    XCloseDisplay(dpy);
    return 0;
}

//    pixmap = XCreatePixmap(dpy, id, image_width, image_height, 24);
/*
    mask = XCreatePixmap(dpy, id, 1, 1, 32);

    pwin.repeat = True;
    pwin.poly_edge = PolyEdgeSmooth;
    pwin.poly_mode = PolyModePrecise;

    atr = CPRepeat | CPPolyEdge | CPPolyMode;
#if 0
CPAlphaMap | CPAlphaXOrigin | CPAlphaYOrigin	|
	   CPClipXOrigin | CPClipYOrigin | CPClipMask |
	   CPGraphicsExposure | CPSubwindowMode | CPPolyEdge |
	   CPPolyMode | CPDither | CPComponentAlpha | CPLastBit;
#endif

    //pwin.alpha_map = p_mask;
    */
//    imlib_image_set_has_alpha(1);
//    imlib_context_set_anti_alias(1);
//    imlib_context_set_drawable(pixmap);
/*
    pixmap = XCreatePixmap(dpy, id,
					  image_width,
					  image_height,
					  32);

    p_pix = XRenderCreatePicture(dpy, pixmap, format_pix, 0, 0);

    xmask = XCreateImage(dpy, DefaultVisual(dpy, scr),
					 32,
					 ZPixmap,
					 0,
					 (char*)AlphaBuffer,
					 image_width,
					 image_height,
					 32,
					 image_width*sizeof(unsigned char));


    mask = XCreatePixmap(dpy, id,
					  image_width,
					  image_height,
					  32);


    p_mask = XRenderCreatePicture(dpy, mask, format_mask, 0, 0);


    p_win =  XRenderCreatePicture(dpy, id, format_mask, 0, 0);

*/
//    printf("si\n");

/*
    ximage->display = dpy;
    ximage->width = image_width;
    ximage->height = image_height;
    ximage->xoffset = 0;
    ximage->format = ZPixmap;
    ximage->data = (char*)ColorBuffer;
    ximage->byte_order = 0;
    ximage->bitmap_unit = 32;
    ximage->bitmap_bit_order = 0;
    ximage->bitmap_pad = 32;
    ximage->depth = 32;
    ximage->bytes_per_line = 0;
    ximage->bits_per_pixel = 32;
    ximage->red_mask = 0xffff;
    ximage->green_mask = 0xffff;
    ximage->blue_mask = 0xffff;

    XInitImage(ximage);
*/
//    pixmap = XCreatePixmapFromBitmapData(dpy, id, ColorBuffer, image_width, image_height
//									, WhitePixel (dpy, scr),
//								 BlackPixel (dpy, scr), 32
//    );

//    mask = XCreateBitmapFromData(dpy, id, (char*)AlphaBuffer, image_width, image_height
//								 WhitePixel (dpy, scr),
//								 BlackPixel (dpy, scr));
//							);
/*

    mask = XCreatePixmapFromBitmapData(dpy, id, AlphaBuffer, image_width, image_height,
								 WhitePixel (dpy, scr),
								 BlackPixel (dpy, scr), 32);

    pixmap = XCreatePixmap(dpy, id, image_width, image_height, 32);
    mask = XCreatePixmap(dpy, id, 1, 1, 32);

    pixmap = XCreateBitmapFromData(dpy, id, ColorBuffer, image_width, image_height);
    mask = XCreateBitmapFromData(dpy, id, AlphaBuffer, image_width, image_height);
*/
/*
    p_mask = XRenderCreatePicture(dpy, mask, format_pix, atr, &pwin);

    pwin.repeat    = True;
    pwin.poly_edge = PolyEdgeSmooth;
    pwin.poly_mode = PolyModePrecise;
//    pwin.alpha_map = p_mask;

    atr = CPRepeat | CPPolyEdge | CPPolyMode; // | CPAlphaMap;

    p_pix = XRenderCreatePicture(dpy, pixmap, format_pix, atr, &pwin);
    p_win = XRenderCreatePicture(dpy, id, format_pix, 0, 0);
*/

    Source: geocities.com/ucho_trabajo/xcomp

               ( geocities.com/ucho_trabajo)