What is the Code2 and other concepts of variable encoding in Coding Redundancy?
In the first column, $r_k$ is the gray level. It ranges from 0 to 1, where 0 is 0% white (i.e., black) and 1 is 100% white. There are 8 gray levels arranged in regular intervals of 1/7.

Code 1 and Code 2 are two different ways of encoding the gray level. Code 1 is a fixed-length code (all codewords have the same length) whereas Code 2 is a variable-length code, specifically a prefix code, presumably a Huffman code for the distribution in the second column. Code 2 is better because the expected codeword length is 2.7 bits, which is smaller than the 3 bits that you get with Code 1.

Since Code 2 is a prefix code, decoding it is actually very simple, and requires no lookahead. (That's why prefix codes are sometimes called instantaneous codes.)