在 Xlib 中反转区域的颜色 [英] Invert the colors of a region in Xlib

查看:54
本文介绍了在 Xlib 中反转区域的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用 Xlib 反转区域的颜色.区域可以是由两个坐标(x1,y1 到x2,y2)定义的矩形区域.下面的代码是脚本的最新修改版本,用于根据 JvO 的建议反转活动窗口的一部分

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版本如下:

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);
}

用 gcc inv.c -o inv -lX11 编译上面的代码.编译成功,但运行代码并没有导致焦点窗口的任何部分倒置

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

以下是输出:

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$

问题似乎在于 GC 的名为子窗口模式"的参数设置为 ClipByChildren(默认情况下).将其设置为 IncludeInferiors 使其工作.在以下链接中找到了解决方案:

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

和支持代码示例:

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;
            }
        }
    }
}

推荐答案

您可以尝试以下操作:

  • 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.

请注意,这只会对 RGB 值进行位反转,这对于彩色图像可能看起来不太好,但对于黑白(灰度)效果很好.

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).

这篇关于在 Xlib 中反转区域的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆