[Xcb] BitBlt like method?

Nadeem Syed nadeem0319 at hotmail.com
Wed Nov 25 18:18:04 PST 2009




Here is what I put together with the help of everyone =) I've also
added in it as a test code with Xlib to compare speeds and etc...
Posted the code at: http://pastebin.com/f34fa2d3f aswell for better
view.



[CODE]



/**

 * Author: Nadeem Syed

 * Desc: Gets a color from the specified point(x, y) from the window id provided

 * 

 * @see http://lists.freedesktop.org/archives/xcb/2009-November/005343.html

 */



// Standard

#include <iostream>

#include <sys/time.h>



// Xlib's

#include <X11/Xlib.h>



// XCB's

#include <xcb/xcb.h>

#include <xcb/xproto.h>

#include <xcb/xcb_image.h>



#define WINDOW_ID_TO_USE 65011716



typedef struct BOUNDS {

    uint16_t x;

    uint16_t y;

    uint16_t width;

    uint16_t height;

} BOUNDS;



void GetColor(unsigned int color, bool useXCB);



XImage* Xlib_GetImage(Window screen, BOUNDS &bounds);

unsigned int Xlib_GetColor(int x, int y, XImage *img);

int Xlib_CountColors(void);



xcb_image_t* XCB_GetImage(xcb_window_t window, BOUNDS &bounds, uint8_t* &data);

unsigned int XCB_GetColor(int x, int y, xcb_image_t *img, uint8_t *data);

int XCB_CountColors(void);





/* 

    return interval of time (uses time.h) 

*/

double get_time (void) {

    struct timeval timev;           

    gettimeofday(&timev, NULL);

    return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);

}



int main() {

    double start, xcbt, xlibt;

    

    start = get_time();

    GetColor(0, true);

    xcbt = get_time() - start;

    start = get_time();

    GetColor(0, false);

    xlibt = get_time() - start;

    std::cout << "XCB GetColor Time = " << xcbt << std::endl;

    std::cout << "Xlib GetColor Time = " << xlibt << std::endl;

    

    std::cout << "--- Counting Colors ---" << std::endl;

    start = get_time();

    int xcb_count = 0;

    for (int i = 0; i <= 19; i++)

        xcb_count = XCB_CountColors();

    xcbt = (get_time() - start) / 19;

    std::cout << "XCB Colors Counted = " << xcb_count << std::endl;

    std::cout << "XCB Time Took AVG  = " << xcbt << std::endl;

    start = get_time();

    int xlib_count;

    for (int i = 0; i <= 19; i++)

        xlib_count = Xlib_CountColors();

    xlibt = (get_time() - start) / 19;

    std::cout << "Xlib Colors Counted = " << xlib_count << std::endl;

    std::cout << "Xlib Time Took AVG  = " << xlibt << std::endl;

    

    return 0;

}



/**

 * Gets color using the specified library. (true = XCB, false = Xlib)

 */

void GetColor(unsigned int color, bool useXCB) {

    if (!useXCB) {

        BOUNDS bounds;

        XImage *img = Xlib_GetImage(WINDOW_ID_TO_USE, bounds);

        unsigned int c = Xlib_GetColor(39, 39, img);

        std::cout << "Xlib Get Color = " << c << std::endl;

    } else {

        uint8_t *data;

        BOUNDS bounds;

        xcb_image_t *img = XCB_GetImage(WINDOW_ID_TO_USE, bounds, data);

        unsigned int c = XCB_GetColor(39, 39, img, data);

        std::cout << "XCB Get Color = " << c << std::endl;

    }

}



/**

 * Xlib - Get Image

 */

XImage* Xlib_GetImage(Window screen, BOUNDS &bounds) {

    XImage* bitmap;

    Display *display = XOpenDisplay(NULL);

    XWindowAttributes attr;

    XGetWindowAttributes(display, screen, &attr);

    bounds.x = attr.x;

    bounds.y = attr.y;

    bounds.width = attr.width - bounds.x;

    bounds.height = attr.height - bounds.y;

    bitmap = XGetImage(display, screen, bounds.x, bounds.y, 

                       bounds.width, bounds.height, AllPlanes, ZPixmap);

    return bitmap;

}



/**

 * Xlib - Get Color

 */

unsigned int Xlib_GetColor(int x, int y, XImage *img) {

    unsigned char* data = (unsigned char*)img->data;

    int bpp = img->bits_per_pixel / 8;

    int loc = (y * bpp * img->width) + (bpp * x);

    return (data[loc] << 16) | (data[loc+1] << 8) | (data[loc+2]);

}



/**

 * Xlib - Count Colors

 */

int Xlib_CountColors(void) {

    BOUNDS bounds;

    XImage *img = Xlib_GetImage(WINDOW_ID_TO_USE, bounds);

    int color_count = 0;//bounds.width * bounds.height - 2;

    for (int w = 0; w < bounds.width; w++) {

        for (int h = 0; h < bounds.height; h++) {

            //std::cout << 

            Xlib_GetColor(w, h, img);

            color_count++;

        }

    }

    return color_count;

}



/**

 * XCB - Get image

 */

xcb_image_t* XCB_GetImage(xcb_window_t window, BOUNDS &bounds, uint8_t* &data) {

    xcb_connection_t *c = xcb_connect(NULL, NULL);

    xcb_image_format_t format = XCB_IMAGE_FORMAT_Z_PIXMAP;    

    xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(c, window);

    xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(c, gcookie, NULL);

    bounds.x = greply->x;

    bounds.y = greply->y;

    bounds.width = greply->width - bounds.x;

    bounds.height = greply->height - bounds.y;

    xcb_get_image_cookie_t cookie = xcb_get_image(c, format, window, 

                                                  bounds.x, bounds.y, 

                                                  bounds.width, bounds.height, 

                                                  ~0);

    xcb_get_image_reply_t *reply = xcb_get_image_reply(c, cookie, NULL);

    data = xcb_get_image_data(reply);

    return xcb_image_create_native(c, greply->width, greply->height, format, 

                                   reply->depth, NULL, ~0, data);

}



/**

 * XCB - Get Color

 */

unsigned int XCB_GetColor(int x, int y, xcb_image_t *img, uint8_t *data) {

    int bpp = img->bpp / 8;

    int loc = (y * bpp * img->width) + (bpp * x);

    return (data[loc] << 16) | (data[loc+1] << 8) | data[loc+2];

}



/**

 * XCB - Count Colors

 */

int XCB_CountColors(void) {

    uint8_t *data;

    BOUNDS bounds;

    xcb_image_t *img = XCB_GetImage(WINDOW_ID_TO_USE, bounds, data);

    int color_count = 0;//bounds.width * bounds.height - 2;

    for (int w = 0; w < bounds.width; w++) {

        for (int h = 0; h < bounds.height; h++) {

            //std::cout << 

            XCB_GetColor(w, h, img, data);

            color_count++;

        }

    }

    return color_count;

}



[/CODE]


-- Some of my results

XCB Get Color = 2829099
Xlib Get Color = 2829099
XCB GetColor Time = 0.0155019
Xlib GetColor Time = 0.0166171
--- Counting Colors ---
XCB Colors Counted = 160321
XCB Time Took AVG  = 0.0175713
Xlib Colors Counted = 160321
Xlib Time Took AVG  = 0.0190249

XCB Get Color = 2829099
Xlib Get Color = 2829099
XCB GetColor Time = 0.0114239
Xlib GetColor Time = 0.0141051
--- Counting Colors ---
XCB Colors Counted = 160321
XCB Time Took AVG  = 0.0172025
Xlib Colors Counted = 160321
Xlib Time Took AVG  = 0.020595

XCB Get Color = 2829099
Xlib Get Color = 2829099
XCB GetColor Time = 0.0126981
Xlib GetColor Time = 0.013911
--- Counting Colors ---
XCB Colors Counted = 160321
XCB Time Took AVG  = 0.0174001
Xlib Colors Counted = 160321
Xlib Time Took AVG  = 0.0196311





~ Thanks

 		 	   		  
_________________________________________________________________
Ready. Set. Get a great deal on Windows 7. See fantastic deals on Windows 7 now
http://go.microsoft.com/?linkid=9691818
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freedesktop.org/archives/xcb/attachments/20091125/6f7f05e9/attachment-0001.htm 


More information about the Xcb mailing list