Invert the colors of a region in Xlib

不问归期 提交于 2020-04-30 07:34:50

问题


I want to know if it is possible to invert the colors of a region using Xlib. A region may be a rectangular area defined by two co-ordinates (x1, y1 to x2, y2). Below code is the latest modified version of the script that is used to invert a portion of active window, as per suggestions by JvO

import Xlib
from Xlib import X, display, Xutil

d = display.Display()

screen = d.screen()

bgsize = 20

act_win = d.get_input_focus().focus
wmname = act_win.get_wm_name()
wmclass = act_win.get_wm_class()
if wmclass is None and wmname is None:
act_win = act_win.query_tree().parent
wmname = act_win.get_wm_name()
print "Active Window: %s" % ( wmname, )

#
# Creating a pixmap of size 200x2000 from active window
#
pm = act_win.create_pixmap(200, 2000, screen.root_depth)

#
# Creating two graphics contexts, one to be inverted, and one normal
#
gc = pm.create_gc()

#
# Inverting the to be inverted graphics context
# Changes after looking at code at:
#  https://github.com/alexer/python-xlib/blob/master/Xlib/protocol/structs.py
#
gc.change(function = X.GXcopyInverted)

#
# Copying the content of act_win to pix map using to_invert_gc
#
# copy_area(gc, src_drawable, src_x, src_y, width, height, dst_x, dst_y)
#
pm.copy_area(gc, act_win, 0, 0, 200, 2000, 0, 0, onerror=None)

#
# Copying back the content of pixmap to act_win using norm_gc
#
# copy_area(gc, src_drawable, src_x, src_y, width, height, dst_x, dst_y)
#
gc.change(function = X.GXcopy)
act_win.copy_area(gc, pm, 0, 0, 200, 2000, 0, 0, onerror=None)

C version of the above code is below:

#include <X11/Xlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>

void main(void) {
    Display *dpy;
    Window root_window, focused;
    Pixmap pm;
    GC gc;
    XGCValues gcv, gcv_ret;
    int revert_to;
    XTextProperty text_prop;
    int screen;

    dpy = XOpenDisplay(":0.0");

    screen = DefaultScreen(dpy);

    root_window = RootWindow (dpy, DefaultScreen(dpy));
    XGetInputFocus(dpy, &focused, &revert_to);
    XGetWMName(dpy, focused, &text_prop);

    printf("Active Window name: %s\n", text_prop.value);
    pm = XCreatePixmap(dpy, focused, 200, 200, 1);
    gc = XCreateGC(dpy, focused, 0, NULL);

    gcv.function = GXcopyInverted;

    XChangeGC(dpy, gc, GCFunction, &gcv);

    XFlushGC(dpy, gc);

    XGetGCValues(dpy, gc, 1, &gcv_ret);
    printf("Function while copying from focused window to pixmap: %d\n", gcv_ret.function);

    XCopyArea(dpy, focused, pm, gc, 0, 0, 200, 200, 0, 0);

    gcv.function = GXcopy;

    XChangeGC(dpy, gc, GCFunction, &gcv);
    XFlushGC(dpy, gc);

    XGetGCValues(dpy, gc, 1, &gcv_ret);
    printf("Function while copying from pixmap to focused window: %d\n", gcv_ret.function);

    XCopyArea(dpy, pm, focused, gc, 50, 50, 200, 200, 0, 0); 
    XFlush(dpy);
}

Compiled the above code with gcc inv.c -o inv -lX11. Compilation succeeded, but running the code does not result in any part of focused window getting inverted

Following is the output:

urc@ubuntu-desktop: ~/Temporary Files$ ./inv
Active Window name: urc@ubuntu-desktop: ~/Temporary Files
Function while copying from focused window to pixmap: 12
Function while copying from pixmap to focused window: 3
urc@ubuntu-desktop: ~/Temporary Files$

The problem seems to be with a parameter named 'sub-window mode' parameter of GC set to ClipByChildren (which is by default). Setting it to IncludeInferiors made it work. Solution was found in the following link:

https://groups.google.com/forum/#!topic/comp.windows.x/_TcGJq2uhmI

And supporting code example at:

http://www.mit.edu/afs.new/sipb/user/web/src/xpunt/xpunt.c

Below is the working code now:

#include <X11/Xlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>

void sleep_ms(int milliseconds) // cross-platform sleep function
{
#ifdef WIN32
Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
usleep(milliseconds * 1000);
#endif
}


void main(void) {
    Display *dpy;
    Window root_window, focused, target, default_root_window;
    Pixmap pm;
    GC gc;
    XGCValues gcv, gcv_ret;
    int revert_to;
    XTextProperty text_prop;

    int screen, depth, whiteColor, blackColor;
    XEvent e;
    char text[1];
    KeySym key;

    dpy = XOpenDisplay(":0.0");
    screen = DefaultScreen(dpy);
    depth = DefaultDepth(dpy, screen);

    whiteColor = WhitePixel(dpy, screen);
    blackColor = BlackPixel(dpy, screen);

    root_window = RootWindow (dpy, screen);
    XGetInputFocus(dpy, &focused, &revert_to);
    XGetWMName(dpy, focused, &text_prop);

    printf("Active Window name: %s\n", text_prop.value);

    target = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 400, 400, 1, whiteColor, blackColor);

    XSelectInput(dpy, target, ButtonPressMask | StructureNotifyMask | ExposureMask | KeyPressMask | PropertyChangeMask | VisibilityChangeMask);
    XMapRaised(dpy, target);

    for(;;) {
        XNextEvent(dpy, &e);
        if (e.type == MapNotify)
            break;
    }

    pm = XCreatePixmap(dpy, focused, 200, 200, depth);
    gc = XCreateGC(dpy, focused, 0, NULL);
    XSetSubwindowMode(dpy, gc, IncludeInferiors);

    gcv.function = GXcopyInverted;

    XChangeGC(dpy, gc, GCFunction, &gcv);

    XFlushGC(dpy, gc);
    while(1) {

        XNextEvent(dpy, &e);
        if(e.type == Expose) {
            XSetInputFocus(dpy, target, RevertToNone, CurrentTime);
        } else if (e.type == MapNotify) {
            printf("Map notify\n");
        } else if (e.type == KeyPress) {
            XLookupString(&e.xkey, text, 255, &key,0);
            if (text[0] == 'c') {
                printf("Copying plane\n");
                XCopyArea(dpy, focused, target, gc, 0, 0, 200, 200, 0, 0);
                XFlush(dpy);
            }
            if (text[0] == 'q') {
                printf("Quitting ...\n");
                break;
            }
        }
    }
}

回答1:


You can try the following:

  • Create a Pixmap with the size of your region
  • Use XCopyArea to copy the area from your original region to the new Pixmap, setting the function attribute GXCopy of the GC (Graphic Context) to XGCopyInverted; use XChangeGC() for that.
  • Copy the pixmap back to the original, with a 'straight' copy.

Note that this will just bit-invert the RGB values, which may not look so good with color images but works fine for black and white (grayscale).




回答2:


Code below works now:

#include <X11/Xlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>

void sleep_ms(int milliseconds) // cross-platform sleep function
{
#ifdef WIN32
Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
usleep(milliseconds * 1000);
#endif
}


void main(void) {
    Display *dpy;
    Window root_window, focused, target, default_root_window;
    Pixmap pm;
    GC gc;
    XGCValues gcv, gcv_ret;
    int revert_to;
    XTextProperty text_prop;

    int screen, depth, whiteColor, blackColor;
    XEvent e;
    char text[1];
    KeySym key;

    dpy = XOpenDisplay(":0.0");
    screen = DefaultScreen(dpy);
    depth = DefaultDepth(dpy, screen);

    whiteColor = WhitePixel(dpy, screen);
    blackColor = BlackPixel(dpy, screen);

    root_window = RootWindow (dpy, screen);
    XGetInputFocus(dpy, &focused, &revert_to);
    XGetWMName(dpy, focused, &text_prop);

    printf("Active Window name: %s\n", text_prop.value);

    target = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 400, 400, 1, whiteColor, blackColor);

    XSelectInput(dpy, target, ButtonPressMask | StructureNotifyMask | ExposureMask | KeyPressMask | PropertyChangeMask | VisibilityChangeMask);
    XMapRaised(dpy, target);

    for(;;) {
        XNextEvent(dpy, &e);
        if (e.type == MapNotify)
            break;
    }

    pm = XCreatePixmap(dpy, focused, 200, 200, depth);
    gc = XCreateGC(dpy, focused, 0, NULL);
    XSetSubwindowMode(dpy, gc, IncludeInferiors);

    gcv.function = GXcopyInverted;

    XChangeGC(dpy, gc, GCFunction, &gcv);

    XFlushGC(dpy, gc);
    while(1) {

        XNextEvent(dpy, &e);
        if(e.type == Expose) {
            XSetInputFocus(dpy, target, RevertToNone, CurrentTime);
        } else if (e.type == MapNotify) {
            printf("Map notify\n");
        } else if (e.type == KeyPress) {
            XLookupString(&e.xkey, text, 255, &key,0);
            if (text[0] == 'c') {
                printf("Copying plane\n");
                XCopyArea(dpy, focused, target, gc, 0, 0, 200, 200, 0, 0);
                XFlush(dpy);
            }
            if (text[0] == 'q') {
                printf("Quitting ...\n");
                break;
            }
        }
    }
}


来源:https://stackoverflow.com/questions/39969855/invert-the-colors-of-a-region-in-xlib

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!