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:
开发者_Go百科 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;
}
0xff
means "the hexadecimal number ff
" - in other words, the integer 255
, which has the binary representation 00000000000000000000000011111111
(when using 32-bit integers). The &
operator performs a bitwise AND operation. a & b
will give you an integer with a bit pattern that has a 0
in all positions where b
has a 0
, while in all positions where b
has a 1
, the corresponding bit value from a
is used (this also goes the other way around). For example, the bitwise AND of 10110111
and 00001101
is 00000101
.
In this case, pval & 0xff
will give you the rightmost 8 bits from pval
.
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, 000
seems to indicate the range "white to blue", while 001
indicates "red to yellow"), and the rightmost eight bits indicate a color in this range.
int lb = pval & 0xff;
This operation performs a bitwise AND operation on pval with 0xff.
Because 0xff is represented as 1's in the lower 8 bits of the integer, it effectively masks pval so it leaves only the value in the last 8 bits.
So, in other words it just ignores all the rest of the integer value, and does a simple modulo 256 calculation. But usually it's just for retrieving the last bits.
pval is an int, but the lowest 8 bits have a special meaning, and the rest of the bits have another
3 1
1 6 8 0
+--------+--------+--------+--------+
pval = |aaaaaaaa|aaaaaaaa|aaaaaaaa|bbbbbbbb|
+--------+--------+--------+--------+
The code 'int lb = pval & 0xff' returns just the value of the 'b' bits.
pval >> 8 returns the value of the 'a' bits.
As to what the code is doing, it seems to me that it's converting some data to a colour map so it can be displayed. There's not enough information to be really sure though.
精彩评论