programmedtodie
programmedtodie

Reputation: 23

CImg Reading Image

I am working on a University Project that is to make a game like Bejewled. We are using OpenGL and CImg. The Project file has the following function in it:

    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. 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:

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'.

Upvotes: 1

Views: 958

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207345

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:

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. This re-orders them into RGB triplets - presumably because that's how you'll need them for OpenGL.

Upvotes: -1

Related Questions