I know, this is a silly question, but since I need this so often, I just want to double check that I made no mistake.
Working with most shader languages, a texture can store $8$ bits per channels, which in the shader programm appear as value in the range range $r_8:=\{0, 1/255, 2/255,\dots,1\}$. What I want to do is to transform between a single $32$ bit floating point value in $(0, 1)$ (I don't care about the floating point specifics here and just think of it as real number) and a $4$-tuple of values in $r_8$, so $\text{split}:(0,1)\to r_8^4$ and $\text{comb}:r_8^4\to(0,1)$.
I specifically want that $\text{comb}(\text{split}(x))=\text{round}(256^4 x)/256^4$ for almost all $x$ (I don't care about the ones lying on boundaries between bins).
My implementation is the following:
- $\text{split}(x)_i := \text{floor}(256^ix)/(255\cdot 256^{i-1})$
- $\text{comb}(x_0,\dots,x_3):=255(x_0/256+\dots+x_3/256^4)+256^4/2$
Is this correct or did I overlook something?
Edit
I realized that the first part should be $$\text{split}(x)_i:=\lfloor(256(256^ix-\lfloor256^ix\rfloor)\rfloor/255$$