views:

49

answers:

1

At the moment I am working on an on screen display project with black, white and transparent pixels. (This is an open source project: http://code.google.com/p/super-osd; that shows the 256x192 pixel set/clear OSD in development but I'm migrating to a white/black/clear OSD.)

Since each pixel is black, white or transparent I can use a simple 2 bit/4 state encoding where I store the black/white selection and the transparent selection. So I would have a truth table like this (x = don't care):

B/W  T      
 x   0    pixel is transparent
 0   1    pixel is black
 1   1    pixel is white

However as can be clearly seen this wastes one bit when the pixel is transparent. I'm designing for a memory constrained microcontroller, so whenever I can save memory it is good.

So I'm trying to think of a way to pack these 3 states into some larger unit (say, a byte.) I am open to using lookup tables to decode and encode the data, so a complex algorithm can be used, but it cannot depend on the states of the pixels before or after the current unit/byte (this rules out any proper data compression algorithm) and the size must be consistent; that is, a scene with all transparent pixels must be the same as a scene with random noise. I was imagining something on the level of densely packed decimal which packs 3 x 4-bit (0-9) BCD numbers in only 10 bits with something like 24 states remaining out of the 1024, which is great. So does anyone have any ideas?

Any suggestions? Thanks!

+2  A: 

In a byte (256 possible values) you can store 5 of your three-bit values. One way to look at it: three to the fifth power is 243, slightly less than 256. The fact that it's slightly less also shows that you're not wasting much of a fraction of a bit (hardly any, either).

For encoding five of your 3-bit "digits" into a byte, think of taking a number in base 3 made from your five "digits" in succession -- the resulting value is guaranteed to be less than 243 and therefore directly storable in a byte. Similarly, for decoding, do the base-3 conversion of a byte's value.

Alex Martelli
I am curious as to how I would pack them more, but thanks for the info. 8 bits sounds very viable. But, the question is asking how can I fit a 2 bit number which only occupies 3 states into 8 bits, 5 times? (EDIT: just read your edit, will post in a sec.)
Thomas O
Good idea on the base-3 thing; I will whip up a python program and see how it goes.
Thomas O
@Thomas, good idea on using Python for the purpose;-).
Alex Martelli
That's about as good as you're going to get. I don't think you'll get any better compression until you start using 192 bits (121 3-state values vs. 120 with 8 bits).
Gabe
My method is to always get it working in Python first before converting it to C or Assembly, because you know with Python you're not going to get obscure compiler bugs or memory violations or otherwise. Python is a great language and I prefer to code in it for everything, but that's unfortunately not always possible... :(
Thomas O
I have finished the algorithm. See this Python program: http://dl.dropbox.com/u/1134084/pixpak.pyThis packs 5 pixels (10 bits uncompressed) into 8 bits.
Thomas O