2014-01-30 22 views
0

我幾乎在那裏用我的小型「窗口背景PNG圖像」項目在Linux中。我使用純粹的X11 API和最小LodePNG加載圖像。問題是背景是原始PNG圖像的負面因素,我不知道可能是什麼問題。XLib窗口背景有倒轉的顏色

這基本上是加載圖像然後創建像素映像和應用背景,以窗口的代碼:

// 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踢球的方式爲了把它放在窗戶上。

更INFO 現在我比較與原來的反向圖象,並發現某處在我的代碼將RGB轉換爲BGR。如果原始圖像上的一個像素是倒置的一個像素,則它是119,102,95

回答

1

我找到了解決方案here。我不確定是否是最好的方法,但確實是比較簡單的。