#include #include #include #include #include #include #include #include #include #include "xtools.h" /* * Dump an image */ XImage *x_window_dump(Display *dpy, Window window) { XWindowAttributes win_info; if( ! XGetWindowAttributes(dpy, window, &win_info) ) assert(!"Unable to get window attributes"); XImage *xi = XGetImage(dpy, window, 0, 0, win_info.width, win_info.height, AllPlanes, ZPixmap); return xi; } /** * Convert a name to a Window id */ Window x_wname_to_wid(Display *dpy, Window top, char *name) { Window *children, dummy; unsigned int nchildren; int i; Window w=0; char *window_name; if ( XFetchName(dpy, top, &window_name) && !strncmp(window_name, name, strlen(name)) ) return(top); if ( ! XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren) ) return 0; for (i=0; i> 16) & 0xFF; *g = (pix >> 8) & 0xFF; *b = pix & 0xFF; } /** * Dump a XImage on disk */ int x_imagedump_save(XImage *xi, FILE *out) { fwrite(xi, sizeof(XImage), 1, out); fwrite(xi->data, xi->bytes_per_line, xi->height, out); return 1; } /** * Load an XImage from disk * FIXME: I think this leaks memory */ XImage *x_imagedump_load(FILE *in) { XImage *xi = (XImage*)malloc(sizeof(XImage));; fread(xi, sizeof(XImage), 1, in); xi->data = (char*)malloc(xi->bytes_per_line*xi->height); fread(xi->data, xi->bytes_per_line, xi->height, in); XInitImage(xi); return xi; } /** * Convert an Ximage to a png file */ void x_image_to_png(XImage *xi, char *path) { gdImagePtr im; im = gdImageCreate(xi->width, xi->height); int back = gdImageColorAllocate(im, 0, 0, 0); int color = gdImageColorAllocate(im, 255, 0, 0); int x, y; for(y=0; yheight; y++) { for(x=0; xwidth; x++) { short r, g, b; pixel_t pix = XGetPixel(xi, x, y); x_pixel_to_rgb(pix, &r, &g, &b); /*printf("putting pixel %d, %d, %d at (%d, %d)\n", (int)r, (int)g, (int)b, x, y);*/ int color = gdImageColorResolve(im, (int)r, (int)g, (int)b); gdImageSetPixel(im, x, y, color); } } /* Copy GD picture to file */ FILE *pngout = fopen(path, "wb"); gdImagePng(im, pngout); gdImageDestroy(im); fclose(pngout); } #define max(a,b) (((a) > (b)) ? (a) : (b)) #define min(a,b) (((a) < (b)) ? (a) : (b)) #define abs(a) (((a)<0) ? -(a) : (a)) #define sign(a) (((a)<0) ? -1 : (a)>0 ? 1 : 0) /** * Draw a line on an SDL surface */ void line32(SDL_Surface *s, int x1, int y1, int x2, int y2, Uint32 color) { int d; int x; int y; int ax; int ay; int sx; int sy; int dx; int dy; Uint8 *lineAddr; Sint32 yOffset; dx = x2 - x1; ax = abs(dx) << 1; sx = sign(dx); dy = y2 - y1; ay = abs(dy) << 1; sy = sign(dy); yOffset = sy * s->pitch; x = x1; y = y1; lineAddr = ((Uint8 *)(s->pixels)) + (y * s->pitch); if (ax>ay) { /* x dominant */ d = ay - (ax >> 1); for (;;) { *((Uint32 *)(lineAddr + (x << 2))) = (Uint32)color; if (x == x2) { return; } if (d>=0) { y += sy; lineAddr += yOffset; d -= ax; } x += sx; d += ay; } } else { /* y dominant */ d = ax - (ay >> 1); for (;;) { *((Uint32 *)(lineAddr + (x << 2))) = (Uint32)color; if (y == y2) { return; } if (d>=0) { x += sx; d -= ay; } y += sy; lineAddr += yOffset; d += ax; } } }