简体   繁体   English

CImg 阅读图像

[英]CImg Reading Image

I am working on a University Project that is to make a game like Bejewled.我正在做一个像 Bejewled 这样的游戏的大学项目。 We are using OpenGL and CImg.我们正在使用 OpenGL 和 CImg。 The Project file has the following function in it:项目文件中包含以下 function:

    void ReadImage(string imgname, vector<unsigned char> &imgArray)
{
    using namespace cimg_library;
    CImg<unsigned char> img(imgname.c_str());
    imgArray.resize(img.height() * img.width() * 3, 0);
    int k = 0;
    unsigned char *rp = img.data();
    unsigned char *gp = img.data() + img.height() * img.width();
    unsigned char *bp = gp + img.height() * img.width();

    for (int j = 0; j < img.width(); ++j)
    {
        int t = j;
        for (int i = 0; i < img.height(); ++i, t += img.width())
        {
            imgArray[k++] = rp[t];
            imgArray[k++] = gp[t];
            imgArray[k++] = bp[t];
        }
        //imgArray[i][j] = img[k++];
    }
}

As far as I understand, and as the name implies, this function is supposed to read an Image.据我了解,顾名思义,这个 function 应该读取图像。 But I can't figure out how to use it and how can I read an Image.但我不知道如何使用它以及如何读取图像。 I would appreciate it if someone could guide me.如果有人可以指导我,我将不胜感激。

EDIT: This is how I'm calling the function:编辑:这就是我调用 function 的方式:

vector<unsigned char> imgvec;
ReadImage("donut", imgvec);

which results in an error:这会导致错误:

[CImg] *** CImgIOException *** [instance(0,0,0,0,00000000,non-shared)] CImg<unsigned char>::load(): Failed to open file 'donut'.

You pass it a string containing the filename of the image and a reference to a vector to hold the pixels.您向它传递一个包含图像文件名的字符串和对用于保存像素的向量的引用。 It opens the image, resizes your vector to accommodate 3 bytes of RGB per pixel and fills your vector with the pixels in the order:它打开图像,调整矢量大小以适应每个像素 3 个字节的 RGB,并按顺序用像素填充矢量:

RGBRGBRGBRGBRGB

That's all really.真的就是这样。 Basically, CImg holds the pixels in a planar fashion... all the red pixels, then all the green pixels, then all the blue.基本上,CImg 以平面方式保存像素……所有红色像素,然后是所有绿色像素,然后是所有蓝色像素。 This re-orders them into RGB triplets - presumably because that's how you'll need them for OpenGL .这会将它们重新排序为 RGB 三元组 - 大概是因为这就是OpenGL需要它们的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM