将PNG图像转换为XImage格式

时间:2018-12-24 17:21:45

标签: image c++14 x11 opencv3.0 xorg

我想加载.png图像(使用OpenCV),然后使用XPutImage()将其复制到新窗口。但是为此,我必须首先将图像转换为XImage格式。

我能够使用下面的代码片段将XImage转换为.png。但是现在我对向后转换感到困惑。

那么有没有好的方法(或代码片段)将png文件转换为XImage格式?

对于XPutImage(): https://tronche.com/gui/x/xlib/graphics/XPutImage.html

 Mat img(ht,wd,CV_8UC3);

 XImage *image = XGetImage(d, w1, 0,0 , wd, ht, AllPlanes, ZPixmap);
 unsigned long rm = image->red_mask;
 unsigned long gm = image->green_mask;
 unsigned long bm = image->blue_mask;

 for (int x = 0; x < wd; x++)
    for (int y = 0; y < ht ; y++)
        {

        unsigned long pixel = XGetPixel(image,x,y);
        unsigned char blue = pixel & bm;
        unsigned char green = (pixel & gm) >> 8;
        unsigned char red = (pixel & rm) >> 16;     

        Vec3b color = img.at<Vec3b>(Point(x,y));         
                color[0] = blue;
        color[1] = green;
        color[2] = red;
        img.at<Vec3b>(Point(x,y)) = color;

    }   

imwrite("w1_img.png", img);

0 个答案:

没有答案