Colors XLib窗口背景具有反转的颜色
在Linux中,我的“PNG图像窗口背景”项目就快完成了。我使用纯X11API和最小值来加载图像。问题是背景是原始PNG图像的负片,我不知道会有什么问题 这基本上就是加载图像然后创建pixmap并将背景应用于窗口的代码:Colors XLib窗口背景具有反转的颜色,colors,background,window,png,xlib,Colors,Background,Window,Png,Xlib,在Linux中,我的“PNG图像窗口背景”项目就快完成了。我使用纯X11API和最小值来加载图像。问题是背景是原始PNG图像的负片,我不知道会有什么问题 这基本上就是加载图像然后创建pixmap并将背景应用于窗口的代码: // required headers // global variables Display *display; Window window; int window_width = 600; int window_height = 400; // main entry po
// 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);
}
//所需的标题
//全局变量
显示*显示;
窗口窗口;
int窗口_宽度=600;
内窗高度=400;
//主要入口点
//用lodePNG加载图像(我没有修改它的代码)
矢量图像;
无符号宽度、高度;
//解码
无符号错误=lodepng::decode(图像、宽度、高度,“bg.png”);
如果(!错误)
{
//这里是我将图像应用于背景的地方
Screen*Screen=NULL;
屏幕=默认屏幕显示(显示);
//创建pixmap
Pixmap Pixmap=XCreatePixmap(
展示,,
XDefaultRootWindow(显示),
宽度,
高度,
默认深度(显示,0)
);
//创建图形上下文
xgc值gr_值;
gr_values.function=GXcopy;
gr_values.background=屏幕上的白色像素(显示);
//从解码的PNG图像创建图像
XImage*XImage=XCreateImage(
展示,,
CopyFromParent,
显示平面(显示,0),
ZPixmap,
0,
(字符*)和图像,
宽度,
高度,
32,
4*宽度
);
//将图像放入pixmap中
XPutImage(
展示,,
pixmap,
gr_上下文,
西玛吉,
0, 0,
0, 0,
窗宽,
窗高
);
//设置窗口背景
XSetWindowBackgroundPixmap(显示、窗口、pixmap);
//释放使用过的资源
XFreePixmap(显示,pixmap);
XFreeGC(显示、gr_上下文);
}
图像被解码(有可能解码不好),然后应用到背景上,但正如我所说,图像的颜色是反向的,我不知道为什么
更多信息
解码后,我将同一图像编码到一个与解码图像相同的PNG文件中,因此问题似乎与LodePNG无关,而是与我使用XLib将其放置在窗口中的方式有关
更多信息
现在,我将反转图像与原始图像进行了比较,发现在我的代码中,RGB被转换为BGR。如果原始图像上的一个像素是95、102、119,而反向图像上的一个像素是119、102、95。我找到了解决方案。我不确定这是不是最好的办法,但肯定是更简单的办法