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?

4

1 回答 1

0

@Potatoswatter 的评论给出了一个非常好的和通用的解决方案。但这对于我的简单问题可能有点过头了。

看了之后,我找到了以下解决方案:

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

然后,我可以将此宏用于以下所有情况:

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

或者:

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

如果我想分配,我会:

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

或者

IMGET(im,y,x)=val;
于 2013-05-23T05:01:02.470 回答