Advertisement
Guest User

Nadeem

a guest
Nov 25th, 2009
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. /**
  2.  * Author: Nadeem Syed
  3.  * Desc: Gets a color from the specified point(x, y) from the window id provided
  4.  *
  5.  * @see http://lists.freedesktop.org/archives/xcb/2009-November/005343.html
  6.  */
  7.  
  8. // Standard
  9. #include <iostream>
  10. #include <sys/time.h>
  11.  
  12. // Xlib's
  13. #include <X11/Xlib.h>
  14.  
  15. // XCB's
  16. #include <xcb/xcb.h>
  17. #include <xcb/xproto.h>
  18. #include <xcb/xcb_image.h>
  19.  
  20. #define WINDOW_ID_TO_USE 65011716
  21.  
  22. typedef struct BOUNDS {
  23.     uint16_t x;
  24.     uint16_t y;
  25.     uint16_t width;
  26.     uint16_t height;
  27. } BOUNDS;
  28.  
  29. void GetColor(unsigned int color, bool useXCB);
  30.  
  31. XImage* Xlib_GetImage(Window screen, BOUNDS &bounds);
  32. unsigned int Xlib_GetColor(int x, int y, XImage *img);
  33. int Xlib_CountColors(void);
  34.  
  35. xcb_image_t* XCB_GetImage(xcb_window_t window, BOUNDS &bounds, uint8_t* &data);
  36. unsigned int XCB_GetColor(int x, int y, xcb_image_t *img, uint8_t *data);
  37. int XCB_CountColors(void);
  38.  
  39.  
  40. /*
  41.     return interval of time (uses time.h)
  42. */
  43. double get_time (void) {
  44.     struct timeval timev;          
  45.     gettimeofday(&timev, NULL);
  46.     return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
  47. }
  48.  
  49. int main() {
  50.     double start, xcbt, xlibt;
  51.    
  52.     start = get_time();
  53.     GetColor(0, true);
  54.     xcbt = get_time() - start;
  55.     start = get_time();
  56.     GetColor(0, false);
  57.     xlibt = get_time() - start;
  58.     std::cout << "XCB GetColor Time = " << xcbt << std::endl;
  59.     std::cout << "Xlib GetColor Time = " << xlibt << std::endl;
  60.    
  61.     std::cout << "--- Counting Colors ---" << std::endl;
  62.     start = get_time();
  63.     int xcb_count = 0;
  64.     for (int i = 0; i <= 19; i++)
  65.         xcb_count = XCB_CountColors();
  66.     xcbt = (get_time() - start) / 19;
  67.     std::cout << "XCB Colors Counted = " << xcb_count << std::endl;
  68.     std::cout << "XCB Time Took AVG  = " << xcbt << std::endl;
  69.     start = get_time();
  70.     int xlib_count;
  71.     for (int i = 0; i <= 19; i++)
  72.         xlib_count = Xlib_CountColors();
  73.     xlibt = (get_time() - start) / 19;
  74.     std::cout << "Xlib Colors Counted = " << xlib_count << std::endl;
  75.     std::cout << "Xlib Time Took AVG  = " << xlibt << std::endl;
  76.    
  77.     return 0;
  78. }
  79.  
  80. /**
  81.  * Gets color using the specified library. (true = XCB, false = Xlib)
  82.  */
  83. void GetColor(unsigned int color, bool useXCB) {
  84.     if (!useXCB) {
  85.         BOUNDS bounds;
  86.         XImage *img = Xlib_GetImage(WINDOW_ID_TO_USE, bounds);
  87.         unsigned int c = Xlib_GetColor(39, 39, img);
  88.         std::cout << "Xlib Get Color = " << c << std::endl;
  89.     } else {
  90.         uint8_t *data;
  91.         BOUNDS bounds;
  92.         xcb_image_t *img = XCB_GetImage(WINDOW_ID_TO_USE, bounds, data);
  93.         unsigned int c = XCB_GetColor(39, 39, img, data);
  94.         std::cout << "XCB Get Color = " << c << std::endl;
  95.     }
  96. }
  97.  
  98. /**
  99.  * Xlib - Get Image
  100.  */
  101. XImage* Xlib_GetImage(Window screen, BOUNDS &bounds) {
  102.     XImage* bitmap;
  103.     Display *display = XOpenDisplay(NULL);
  104.     XWindowAttributes attr;
  105.     XGetWindowAttributes(display, screen, &attr);
  106.     bounds.x = attr.x;
  107.     bounds.y = attr.y;
  108.     bounds.width = attr.width - bounds.x;
  109.     bounds.height = attr.height - bounds.y;
  110.     bitmap = XGetImage(display, screen, bounds.x, bounds.y,
  111.                        bounds.width, bounds.height, AllPlanes, ZPixmap);
  112.     return bitmap;
  113. }
  114.  
  115. /**
  116.  * Xlib - Get Color
  117.  */
  118. unsigned int Xlib_GetColor(int x, int y, XImage *img) {
  119.     unsigned char* data = (unsigned char*)img->data;
  120.     int bpp = img->bits_per_pixel / 8;
  121.     int loc = (y * bpp * img->width) + (bpp * x);
  122.     return (data[loc] << 16) | (data[loc+1] << 8) | (data[loc+2]);
  123. }
  124.  
  125. /**
  126.  * Xlib - Count Colors
  127.  */
  128. int Xlib_CountColors(void) {
  129.     BOUNDS bounds;
  130.     XImage *img = Xlib_GetImage(WINDOW_ID_TO_USE, bounds);
  131.     int color_count = 0;//bounds.width * bounds.height - 2;
  132.     for (int w = 0; w < bounds.width; w++) {
  133.         for (int h = 0; h < bounds.height; h++) {
  134.             //std::cout <<
  135.             Xlib_GetColor(w, h, img);
  136.             color_count++;
  137.         }
  138.     }
  139.     return color_count;
  140. }
  141.  
  142. /**
  143.  * XCB - Get image
  144.  */
  145. xcb_image_t* XCB_GetImage(xcb_window_t window, BOUNDS &bounds, uint8_t* &data) {
  146.     xcb_connection_t *c = xcb_connect(NULL, NULL);
  147.     xcb_image_format_t format = XCB_IMAGE_FORMAT_Z_PIXMAP; 
  148.     xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(c, window);
  149.     xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(c, gcookie, NULL);
  150.     bounds.x = greply->x;
  151.     bounds.y = greply->y;
  152.     bounds.width = greply->width - bounds.x;
  153.     bounds.height = greply->height - bounds.y;
  154.     xcb_get_image_cookie_t cookie = xcb_get_image(c, format, window,
  155.                                                   bounds.x, bounds.y,
  156.                                                   bounds.width, bounds.height,
  157.                                                   ~0);
  158.     xcb_get_image_reply_t *reply = xcb_get_image_reply(c, cookie, NULL);
  159.     data = xcb_get_image_data(reply);
  160.     return xcb_image_create_native(c, greply->width, greply->height, format,
  161.                                    reply->depth, NULL, ~0, data);
  162. }
  163.  
  164. /**
  165.  * XCB - Get Color
  166.  */
  167. unsigned int XCB_GetColor(int x, int y, xcb_image_t *img, uint8_t *data) {
  168.     int bpp = img->bpp / 8;
  169.     int loc = (y * bpp * img->width) + (bpp * x);
  170.     return (data[loc] << 16) | (data[loc+1] << 8) | data[loc+2];
  171. }
  172.  
  173. /**
  174.  * XCB - Count Colors
  175.  */
  176. int XCB_CountColors(void) {
  177.     uint8_t *data;
  178.     BOUNDS bounds;
  179.     xcb_image_t *img = XCB_GetImage(WINDOW_ID_TO_USE, bounds, data);
  180.     int color_count = 0;//bounds.width * bounds.height - 2;
  181.     for (int w = 0; w < bounds.width; w++) {
  182.         for (int h = 0; h < bounds.height; h++) {
  183.             //std::cout <<
  184.             XCB_GetColor(w, h, img, data);
  185.             color_count++;
  186.         }
  187.     }
  188.     return color_count;
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement