home *** CD-ROM | disk | FTP | other *** search
- ┌──────────────────────────────────┐
- │ An Example of │
- │ Huffman Compression │
- │By Rich Geldreich December 24,1991│
- └──────────────────────────────────┘
-
-
- Hello! You just downloaded two QuickBASIC 4.5 programs
- that compress/decompress VGA screens(screen 13) with a
- Huffman compression algorithm.
- Okay. It's not that fast- but I have been optimizing it
- for two or three months now and it is about as good as I can
- get it. I didn't have much information about Huffman
- compression, just the bare basics. It works, however.
- Huffman compression doesn't even touch LZW compression-
- similar to the type used in GIF pictures and PKZIP type
- protocols. I decompressed many pictures and recompressed them
- with my Huffman compressor and find that they are always
- larger than their GIF brothers. Well, Huffman relies on
- characters being used more than other characters for its
- compression, while GIF's type relies on patterns and repeated
- byte sequences for its compression. While one Huffman code
- represents one character, one LZW code can represent more
- than one character. Well, you got it, so let me explain it. I
- think it's pretty neat- hell, it uses a tree. Just with the
- Christmas spirit(if it isn't near Christmas when you read
- this, than that's too bad!).
-
- Lets say we wanted to compress the following letters
- with huffman compression:
-
- ABCBDCAAAAB
-
- 1. Count how many times each character is used in the file:
-
- A- 5 times.
- B- 3 times.
- C- 2 times.
- D- 1 time.
-
-
- 2. Start building trees...
- (*=not used)
-
- 5 3 2 1
- / \ / \ / \ / \
- A * B * C * D *
-
- 3. Find the two lowest "nodes" on the top level and combine
- them:
- (1 and 2 are the 2 lowest)
-
- 5 3 3
- / \ / \ / \
- A * B * C D
-
- 4. Repeat step 3 until only one node is at the top...
- (the two 3's are the lowest)
-
-
- 5 6
- / \ / \
- A * B 3
- / \
- C D
-
- (Now combine those two)
-
-
- 11
- / \
- A 6
- / \
- B 3
- / \
- C D
-
-
-
- 5. To build the codes that represent these characters go down
- the tree- each time we go left use a "1" each time we go
- right use a "0" (or the other way 'round- doesn't make a
- difference as long as the decompressor does the same thing as
- the compressor).
-
-
- A is 1
- B is 01
- C is 001
- D is 000
-
- 6. Now things start to get fuzzy. I'll explain what my
- compressor does.
-
- 7. Send the tree. There are two ways to send it- in its very
- beginning stage(before it was combined) or when it is all
- combined. Sending it combined *should* take up less space-
- but who knows. If it sent uncombined, then the decompressor
- has to combine it. My compressor sends it all combined with
- balls, lights, ooops- wrong subject! (all right:cheap joke
- every text file has a cheap joke somewhere!)
-
- 8. Start compressing the data. Here is what our sample text
- compresses to:
- ABCBDCAAAAB (in Ascii this would take up 88 bits)
- 10100101000001111101 (20 bits)
-
- Well, looks like our compression ratio was (88-20)/88 or
- 68/88 or 34/44 or 17/22- which is about 17/20 or around
- 85%(sorry, don't have my calculator).
-
- 9. That's it! If you understood this, than you also realize
- that this makes a really useful coding system for sending
- secret messages; pulses of light, beeps, scratches on wood...
- The only hard part is getting the tree over. There is a type
- of compression called "Adaptive Huffman Compression" that
- doesn't have to send a tree- but I have only scratched the
- surface of that one. I think the decompressor assumes a
- predefined character distribution and updates the tree as it
- decompresses.
-
-
-
- There are two QuickBASIC 4.5 programs, in ascii format,
- that compress a VGA screen 13 (320x200x256) picture. If you
- only have EGA then change all the screen 13's in both of the
- programs to screen 7's and change the C=rnd*255 to C=rnd*15
- in the compressor to allow for the limited amount of colors,
- and hopefully everything else should take care of itself.
- The compressor doesn't send a palette or anything- I first
- made it to work with files but that was horrendously slow.
- Give the compressor time- 'specially on slow systems, because
- making/combining/& scanning the tree don't go all that quick!
- As the compressor sends out codes, a line will disappear from
- the screen from left to right(I know- the usually was is up
- to down but I wanted to be different!). Note that compiling
- it makes it much faster(or at least on my system), but still
- too slow on by 12 mhz '286 to use it in a game program to
- decompress a title screen or something.
-
- To see my brainchild in action, execute COMPRESS.BAS
- first. After it churns out all of its data, execute
- DECOM.BAS. You *should* see the same screen you viewed
- before(remember- what can happen WILL happen; and it has!).
- I fully documented the programs so they shouldn't be that
- hard to follow. (Note: you may have to load QB with the /ah
- option?!, because it uses the '$dynamic metacommand.)
- If you have any question, problems, or any other bad
- news then send them to me at...
-
- Rich Geldreich
- 410 Market St.
- Gloucester City, New Jersey 08030
- (609)-456-0721
-
-
- You may use, modify, and change this program to your hearts
- desire. I don't care. It's my Christmas present to some poor
- fool out there that has been trying to compress things for
- months(and he's out there- I just know it...)