I am working on a C++ code. Not very sure what the code below is trying to do. Would like someone’s help on it.
int pval = t_gamma[depth[i]];
int lb = pval & 0xff;
switch (pval>>8) {
case 0:
depth_mid[3*i+0] = 255;
depth_mid[3*i+1] = 255-lb;
depth_mid[3*i+2] = 255-lb;
break;
case 1:
depth_mid[3*i+0] = 255;
depth_mid[3*i+1] = lb;
depth_mid[3*i+2] = 0;
break;
case 2:
depth_mid[3*i+0] = 255-lb;
depth_mid[3*i+1] = 255;
depth_mid[3*i+2] = 0;
break;
case 3:
depth_mid[3*i+0] = 0;
depth_mid[3*i+1] = 255;
depth_mid[3*i+2] = lb;
break;
case 4:
depth_mid[3*i+0] = 0;
depth_mid[3*i+1] = 255-lb;
depth_mid[3*i+2] = 255;
break;
case 5:
depth_mid[3*i+0] = 0;
depth_mid[3*i+1] = 0;
depth_mid[3*i+2] = 255-lb;
break;
default:
depth_mid[3*i+0] = 0;
depth_mid[3*i+1] = 0;
depth_mid[3*i+2] = 0;
break;
}
0xffmeans “the hexadecimal numberff” – in other words, the integer255, which has the binary representation00000000000000000000000011111111(when using 32-bit integers). The&operator performs a bitwise AND operation.a & bwill give you an integer with a bit pattern that has a0in all positions wherebhas a0, while in all positions wherebhas a1, the corresponding bit value fromais used (this also goes the other way around). For example, the bitwise AND of10110111and00001101is00000101.In this case,
pval & 0xffwill give you the rightmost 8 bits frompval.As for what the code does, I believe it tries to transform color values from a special format to standard RGB: the color values seem to contain eleven bits, where three of them indicate a color range (for example,
000seems to indicate the range “white to blue”, while001indicates “red to yellow”), and the rightmost eight bits indicate a color in this range.