home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
progbas
/
huffman.arj
/
DECOM.BAS
< prev
next >
Wrap
BASIC Source File
|
1991-12-25
|
3KB
|
135 lines
'$DYNAMIC
'Huffman decompressor
'decompresses a screen 13 picture
'Rich Geldreich
'December 24, 1991
'You may use or modify this program in any way you wish.
DEFINT A-Z
DECLARE FUNCTION Getbit ()
DECLARE FUNCTION GetByte ()
'striped down version of the tree
'this is loaded in from the compressed file
TYPE NodeType
NextLeft AS INTEGER
NextRight AS INTEGER
Typ AS INTEGER
Contents AS INTEGER
END TYPE
'set up some usefull constants
CONST Byte = -1, Number = 0
CONST True = -1, False = NOT True
'dim all vars; REDIM is used for all the data required
REDIM Node(600) AS NodeType
REDIM SHARED ByteBuffer(32760) AS STRING * 2
DIM SHARED Position
DIM SHARED Powers(8), Exps(9), BitsIn, BytesOut
'set up the bit twiddling numbers for getbit and getbyte
FOR A = 1 TO 8: Powers(A) = 2 ^ (8 - A): NEXT
FOR A = 9 TO 0 STEP -1: Exps(A) = 2 ^ A: NEXT
'load in all the data: note that the data is stored in pairs of two
'bytes in the array to allow 64k (QB only lets us have 32767
'elements in a string array)
DEF SEG = VARSEG(ByteBuffer(0))
BLOAD "screen.huf", VARPTR(ByteBuffer(0))
'set some vars to trick the GetBit sub to get data from file
'on its very first call
BitsIn = 8: BytesOut = 1
TempChar = 0
Position = 0
'Index is the number of nodes, RootIndex is the node at the top level where
'everything starts
Index = GetByte
RootIndex = GetByte
'loads in the tree
FOR A = 0 TO Index
Node(A).NextLeft = GetByte
Node(A).NextRight = GetByte
'sees if the node contains any usefull data(a letter)
IF Getbit <> 0 THEN
Node(A).Typ = Byte
Node(A).Contents = 0
FOR B = 1 TO 8
IF Getbit <> 0 THEN
Node(A).Contents = Node(A).Contents + Powers(B)
END IF
NEXT
ELSE
'nope, it just has a number
'since the number isn't needed, it's not sent so it doesn't
'waste space
Node(A).Typ = Number
END IF
NEXT
'switch to screen 13 VGA
SCREEN 13, , 0, 0
'show our progress
LINE (0, 0)-(319, 199), 14, BF
'start decompressing
FOR X = 0 TO 319
FOR Y = 0 TO 199
'switch pointer to top
Current = RootIndex
'go down the tree
DO
'see if we go left or right
IF Getbit <> 0 THEN
Current = Node(Current).NextRight
ELSE
Current = Node(Current).NextLeft
END IF
'stop when we reach a letter
LOOP UNTIL Node(Current).Typ = Byte
'set a pixel
PSET (X, Y), Node(Current).Contents
NEXT
NEXT
'beep when where all done
BEEP
A$ = INPUT$(1)
END
REM $STATIC
'gets a single bit from buffer
FUNCTION Getbit STATIC
BitsIn = BitsIn + 1
IF BitsIn = 9 THEN
BytesOut = BytesOut + 1
IF BytesOut = 2 THEN
TempByte$ = ByteBuffer(Position)
Position = Position + 1
BytesOut = 0
TempChar = ASC(TempByte$)
NextChar = ASC(RIGHT$(TempByte$, 1))
ELSE
TempChar = NextChar
END IF
BitsIn = 1
END IF
Getbit = TempChar AND Powers(BitsIn)
END FUNCTION
'gets ten bits from file
FUNCTION GetByte
B = 0
FOR A = 9 TO 0 STEP -1
IF Getbit <> 0 THEN
B = B + Exps(A)
END IF
NEXT
GetByte = B
END FUNCTION