views:

208

answers:

1

Hello

I am trying to find C in the following function in CImg

C=B*L+(1-B)S

Where C, L and S are all RGB images of the same size (and B is a single channel grayscale matte)

I cannot figure out how to loop over the pixels. I've seen code like:

cimg_forXYZ(S,x,y,z) {... }

But I have never see than kind of syntax before, could it be a macro?

Any suggestions are welcome.

+3  A: 

If you look into the CImg header, you'll see that that code is, in fact, a macro that thunks down into:

#define cimg_forXY(img,x,y)       cimg_forY(img,y) cimg_forX(img,x)
#define cimg_forX(img,x)          for (int x=0; x<(int)((img).width); ++x)
#define cimg_forY(img,y)          for (int y=0; y<(int)((img).height); ++y)
#define cimg_forZ(img,z)          for (int z=0; z<(int)((img).depth); ++z)
#define cimg_forXYZ(img,x,y,z) cimg_forZ(img,z) cimg_forXY(img,x,y)

Which means that you will have the following loops:

for (int z=0; z<(int)((img).depth); ++z)
    for (int y=0; y<(int)((img).height); ++y)
        for (int x=0; x<(int)((img).width); ++x) {

}

So, now, what you probably want to do is reference either the x, y, and z coordinates, or better, the pointer into the data as you step through, like

cimg_library::CImg<float> image;
//assign, etc
float* ptr = image->ptr();
cimg_forXYZ(S, x, y, z){
    *ptr = *ptr + 10;
    ++ptr;
}

I encourage you to read the CImg header; it's quite elegant. You'll get a lot of functionality 'for free'.

mmr
Thanks, that cleared up a few things. Still I seem to get incorrect/inconsistent results.. hmm perhaps I should post another question
zenna
incorrect or inconsistent results can happen as a result of bit depth issues. For instance, that multiplication could easily do strange and bad things to you with anything other than float images (ie, byte images will easily overflow if you're not careful).
mmr