32bit to 24bit

October 14, 2007 on 2:30 pm | In Personal, Programming | By QBasicer |

We all know that using 32bit formats can be a bit cumbersome sometimes. I devised a little experiment. First I took a 32bit .TGA file, and created a gradient with some transparency (partial and full), and then wrote two algorithms to convert 32bits to 24bits without loose alpha information. I attemped to use a JPG as a carrier format, but more on that later. Note that I had to use PNGs due to image restrictions. Mouse over/hover to add a blue background strip/background to see the transparency.
Original
Original Image

Algorithm 1
By taking each 8bit channel, I convert it to a 6 bit representation. I then pack four 6 bit channels right after each other into an interger, creating a 24 bit representation. I then extract three 8 bit numbers from this, RGB. This creates quite a bit colour distortion.
Resultant JPG
Algorithm 1 JPG
I then did the reverse, to try and get the original 32 bit file.:
Resultant File
Algorithm 1 Resultant

Algorithm 2
I was noticeable dissatisfied with the scrambled output of algorithm 1. I had planned that this would happen, but then I realized that it would create hard edges, where JPG would do more lossy compression and trash the original even more. I then devised a way to save the original file as much as possible, while keeping alpha. In my new algorithm, I shrink each channel down to 6 bits from 8, and left align it in each 8bit colour channel. I then took the top 3 bits of the alpha channel, and stored it in bit 2 of the RGB, and then the lower 6 bits of the alpha in the 1st bit of the RGB channels. As the top 3 bits were the most important, storing them in a higher position would probably make them safer. Storing the alpha in the bottom two bits of each channel allowed the original image to appear as if I just removed the transparency. The 6 bits I use for alpha almost unnoticed. Here’s the JPG of algorithm 2:
Resultant JPG
Algorithm 2 JPG
Here you can see that the jpg looks pretty decent.
Resultant File
Algorithm 2 Resultant
While you can see the original colours better, the JPG algorithm decided that the last two bits of each channel were completely unimportant, and got rid of them completely, or randomized them.

Conclusion
This just shows how much JPG throws away lossesly even on maximum compression. It’s interesting to note that if I use a lossess intermediate file format other than JPG, that the algorithms (both) work flawlessly. It may be worth trying to store the alpha channel information (or at least one bit of it) further up inside the original image in attempt not to have it trashed so much. Perhaps I’ll look into an algorithm 3.
Resultant when a Lossless Intermediary File Format is Used (Algorithm 2)
Algorithm 2 Resultant using Lossless Intermediary

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress with Pool theme design by Borja Fernandez. I rewrote the CSS because I'm cool like that.
Entries and comments feeds. Valid XHTML and CSS. ^Top^