Original Post
Hello!
The context of the following is storing two 4-bit values in a 8-bit unsigned integer. The integer is a color channel, which is saved into an image file. If I just shift one of the numbers left and OR it to the other, the lower order number is distorted dramatically by image compression, whereas the higher order number is preserved well enough.
Think of the above as storing the binary digits like so: AAAABBBB. Where AAAA and BBBB are 4bit numbers.
What I want to do instead, is store it like ABABABAB. I'd need a very light function to pack the two values together, and another for unpacking.
So, 1010 and 1001 would become 1 1 0 0 1 0 0 1.
The way I'd normally do it is with masking and shifting, eg.
So here is the problem: the unpack function needs to be written in a Cg fragment shader program, with no support for bitwise operators, and a fairly low limit on the number of times in which you can call arithmetic instructions. While the shift operators do not exist, they are simulated easily with multiplication and division.
What I really need is something as simple multiplying either of the four bit numbers by some constant which staggers the bits, and something which I can divide the 8bit number by to produce either of the four bit numbers.
Any thoughts?
The context of the following is storing two 4-bit values in a 8-bit unsigned integer. The integer is a color channel, which is saved into an image file. If I just shift one of the numbers left and OR it to the other, the lower order number is distorted dramatically by image compression, whereas the higher order number is preserved well enough.
Think of the above as storing the binary digits like so: AAAABBBB. Where AAAA and BBBB are 4bit numbers.
What I want to do instead, is store it like ABABABAB. I'd need a very light function to pack the two values together, and another for unpacking.
So, 1010 and 1001 would become 1 1 0 0 1 0 0 1.
The way I'd normally do it is with masking and shifting, eg.
So here is the problem: the unpack function needs to be written in a Cg fragment shader program, with no support for bitwise operators, and a fairly low limit on the number of times in which you can call arithmetic instructions. While the shift operators do not exist, they are simulated easily with multiplication and division.
What I really need is something as simple multiplying either of the four bit numbers by some constant which staggers the bits, and something which I can divide the 8bit number by to produce either of the four bit numbers.
Any thoughts?