【问题标题】:XLib Window background has colours invertedXLib 窗口背景颜色反转
【发布时间】:2014-01-30 19:42:32
【问题描述】:

我在 Linux 中的小“PNG 图像的窗口背景”项目快到了。我使用纯 X11 API 和最小的LodePNG 来加载图像。问题是背景是原始PNG图像的负片,我不知道可能是什么问题。

这基本上是加载图像然后创建像素图并将背景应用于窗口的代码:

// required headers
// global variables
Display *display;
Window window;
int window_width = 600;
int window_height = 400;

// main entry point

// load the image with lodePNG (I didn't modify its code)
vector<unsigned char> image;
unsigned width, height;

//decode
unsigned error = lodepng::decode(image, width, height, "bg.png");
if(!error)
{
    // And here is where I apply the image to the background
    Screen* screen = NULL;
    screen = DefaultScreenOfDisplay(display);

    // Creating the pixmap
    Pixmap pixmap = XCreatePixmap(
        display, 
        XDefaultRootWindow(display), 
        width, 
        height, 
        DefaultDepth(display, 0)
    );

    // Creating the graphic context
    XGCValues gr_values;
    gr_values.function = GXcopy;
    gr_values.background = WhitePixelOfScreen(display);

    // Creating the image from the decoded PNG image
    XImage *ximage = XCreateImage(
        display, 
        CopyFromParent, 
        DisplayPlanes(display, 0), 
        ZPixmap, 
        0, 
        (char*)&image, 
        width, 
        height, 
        32, 
        4 * width
    );

    // Place the image into the pixmap
    XPutImage(
        display, 
        pixmap, 
        gr_context, 
        ximage, 
        0, 0, 
        0, 0, 
        window_width, 
        window_height
    );

    // Set the window background
    XSetWindowBackgroundPixmap(display, window, pixmap);

    // Free up used resources
    XFreePixmap(display, pixmap);
    XFreeGC(display, gr_context);
}

图像被解码(并且有可能被错误解码)然后它被应用到背景上,但是,正如我所说,图像颜色是反转的,我不知道为什么。

更多信息

解码后,我将相同的图像编码为与解码后的图像相同的 PNG 文件,因此看起来问题与 LodePNG 无关,而与我使用 XLib 将其放置在窗口上的方式有关。

更多信息 现在我将倒置图像与原始图像进行比较,发现在我的代码中某处将 RGB 转换为 BGR。如果原始图像上的一个像素是 95, 102, 119 在倒置的图像上是 119, 102, 95

【问题讨论】:

    标签: colors background window png xlib


    【解决方案1】:

    我找到了解决方案here。我不确定是否是最好的方法,但肯定更简单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2012-02-05
      相关资源
      最近更新 更多