0

There are already many questions/answers about macro overloading. But, I cannot find a way to apply it to my particular problem.

I would like to conveniently assign values to my 3D images in C. For now, I do as follow:

#define IMGET(im,y,x,c) im.data[(y)+im.height*((x)+im.width*(c))]
#define IMSET(im,y,x,c,v) IMGET(im,y,x,c)=v

It works well.

But, I would like to use it also when I have black&white images, which are only 2D. Something, like that:

#define IMGET(im,y,x,c) im.data[(y)+im.height*((x)+im.width*(c))] //3D case
#define IMSET(im,y,x,c,v) IMGET(im,y,x,c)=v //3D case
#define IMGET(im,y,x) im.data[(y)+im.height*(x)] //2D case
#define IMSET(im,y,x,v) IMGET(im,y,x)=v //2D case

Is it possible?

Oli
  • 15,935
  • 7
  • 50
  • 66
  • What did you try? Are you referring to [this](http://stackoverflow.com/q/9183993/153285)? I don't see "many" items on this topic. – Potatoswatter May 22 '13 at 02:58
  • Here are some links that I have found http://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments http://stackoverflow.com/questions/679979/how-to-make-a-variadic-macro-variable-number-of-arguments http://stackoverflow.com/questions/5283197/c-macro-with-variable-number-of-arguments – Oli May 22 '13 at 03:45
  • 2
    I've [posted](http://stackoverflow.com/q/16683146/153285) a detailed, general explanation. Hope it helps. – Potatoswatter May 22 '13 at 03:48
  • Yes, it is possible. I'm not sure what kind of answer you're looking for here. – Richard May 22 '13 at 04:00
  • @Richard, I think that the answer of Potatowatter is what I am looking for. – Oli May 22 '13 at 04:04

1 Answers1

0

The comment of @Potatoswatter gives a very good and general solution. But it may be an overkill for my simple problem.

After looking at it, I have found the following solution:

#define IMGET(im,y,x,...) im.data[(y)+im.height*((x)+im.width*(__VA_ARGS__+0))]

Then, I can use this macro for all the following situations:

float val = IMGET(im,y,x,c);

or:

float val = IMGET(im,y,x,c);

And if I want to assingn, I do:

IMGET(im,y,x,c)=val;

or

IMGET(im,y,x)=val;
Oli
  • 15,935
  • 7
  • 50
  • 66