Skip to content

Commit c05c9d9

Browse files
committed
Reading frequency table from a file for decoding should work now, although it is ugly.
1 parent 660eed0 commit c05c9d9

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

arithmeticDecoder.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package main
22

3+
import "fmt"
4+
35
type ArithmeticDecoder struct {
46
/*
57
- inputBits : the bool array of input bits, that gets shifted for error calculations
68
- symbolInterval: is the calculated symbol interval, that falls between a symbol's high and low value in the model definition
9+
- high table is encoded in the file, low table is derived from the high table
710
- step,low,high : step, calculation helper used here for data retention
811
- output: the decoded byte array, decompressed file
912
*/
1013
inputBits []bool
14+
highTable []uint32
15+
lowTable []uint32
1116
symbolInterval uint32
1217
step uint32
1318
low uint32
@@ -16,6 +21,15 @@ type ArithmeticDecoder struct {
1621
}
1722

1823
//The first 256 32-bit bytes are our frequency table, or rather out HIGH table, containing the high value of each symbol
19-
func readFreqTable(data []byte) {
20-
24+
func (arithmeticDecoder *ArithmeticDecoder) readFreqTable(data []uint8) {
25+
highTable := make([]uint32, 256)
26+
for i := 4; i < 256*4; i += 4 {
27+
if i%4 == 0 {
28+
readBytes := data[i-4 : i]
29+
convertedInt := bytesToUint32(readBytes)
30+
highTable[(i-4)/4] = convertedInt
31+
}
32+
}
33+
fmt.Println(highTable)
34+
arithmeticDecoder.highTable = highTable
2135
}

binaryConversions.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ func argumentToBinary(argument string) []bool {
1616
}
1717
return bits
1818
}
19-
func byteToBitSlice(byteSlice uint32, length uint8) []bool {
19+
func byteToBitSlice(bytes uint32, length uint8) []bool {
2020
bits := make([]bool, length)
2121
var i uint8
2222
//The length of 32 means we're decoding a 32 bit sequence
23-
//Loop through the byte and turn it into bit sequence using AND and masking
23+
//Loop through the and turn it into bit sequence using AND and masking
2424
//Using an unsigned integer, so
2525
//7 -> 0
2626
for i = 0; i < length; i++ {
27-
mask := byte(1 << i)
28-
if (byteSlice & uint32(mask)) > 0 {
27+
mask := uint8(1 << i)
28+
if (bytes & uint32(mask)) > 0 {
2929
bits[(length-1)-i] = true
3030
} else {
3131
bits[(length-1)-i] = false
@@ -36,11 +36,11 @@ func byteToBitSlice(byteSlice uint32, length uint8) []bool {
3636
return bits
3737
}
3838

39-
//Takes a 8 sized bool slice and turns it into a single byte, or 4 bytes if we're converting length
40-
func bitSliceToByte(bitSlice *[]bool, length uint8) []byte {
39+
//Takes a length*8 sized bool slice and turns it into a single byte, or 4 bytes if we're converting length
40+
func bitSliceToByte(bitSlice *[]bool, length uint8) []uint8 {
4141
var i uint8 = 0
4242
var j uint8 = 0
43-
resultingBytes := make([]byte, length)
43+
resultingBytes := make([]uint8, length)
4444
for i = 0; i < length; i++ {
4545
for j = 0; j < 8; j++ {
4646
if (*bitSlice)[(i*8)+j] {
@@ -51,3 +51,11 @@ func bitSliceToByte(bitSlice *[]bool, length uint8) []byte {
5151

5252
return resultingBytes
5353
}
54+
func bytesToUint32(data []uint8) uint32 {
55+
var returnValue uint32 = 0
56+
returnValue |= uint32(data[3])
57+
returnValue |= uint32(data[2]) << 8
58+
returnValue |= uint32(data[1]) << 16
59+
returnValue |= uint32(data[0]) << 24
60+
return returnValue
61+
}

binaryIO.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ func readBinaryFile(arithmeticCoder *ArithmeticCoder, filepath string, operation
5050
} else {
5151
arithmeticCoder.intervalCalculation(data)
5252
}
53+
} else if operation == "d" {
54+
arithmeticDecoder.readFreqTable(data)
55+
5356
}
5457
bufferOverflow += bufferSize
5558
//So we're aware of indexes if the file is larger
@@ -58,7 +61,7 @@ func readBinaryFile(arithmeticCoder *ArithmeticCoder, filepath string, operation
5861
- if low < firstQuarter : output 01 and E3_COUNTER times bit 1
5962
- else : output 10 and E3_COUNTER times bit 0
6063
*/
61-
if !modelCreation {
64+
if !modelCreation && arithmeticCoder != nil {
6265
writeEncoded(arithmeticCoder)
6366
//fmt.Println("The rest:")
6467

main.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func main() {
1414
operation := os.Args[1]
1515
inputFile := os.Args[2]
1616
outputFile := os.Args[3]
17+
var upperLimit uint32 = 4294967295
1718
//Compression
1819
if operation == "c" {
1920
/*
@@ -29,17 +30,35 @@ func main() {
2930
//TODO: turn this into output byte array
3031
outputBits := make([]bool, 0)
3132
//4294967295
32-
var upperLimit uint32 = 4294967295
3333
//Initialize an arithmetic codec with empty values except for the upper limit, which has the value of 2^32-1
3434
//After creating the model is done, we go on to interval creation
35-
arithmeticCoder := &ArithmeticCoder{frequencyTable, highTable, lowTable, readSequence, 0, upperLimit, 0, upperLimit, 0, 0, quarters, 0, outputBits}
35+
arithmeticCoder := &ArithmeticCoder{frequencyTable, highTable, lowTable,
36+
readSequence, 0, upperLimit, 0, upperLimit,
37+
0, 0, quarters, 0, outputBits}
3638
arithmeticCoder.quarterize(upperLimit)
3739
//The last argument is for the arithmetic decoder, whenever we are not decoding, it's nil
3840
readBinaryFile(arithmeticCoder, inputFile, operation, true, nil)
3941
readBinaryFile(arithmeticCoder, inputFile, operation, false, nil)
4042
fmt.Print("Ouputting to... ", outputFile)
4143
//Decomepression, read compressed file, deconstruct the symbols based off of it
4244
} else if operation == "d" {
43-
45+
/* inputBits []bool
46+
highTable []uint32
47+
lowTable []uint32
48+
symbolInterval uint32
49+
step uint32
50+
low uint32
51+
high uint32
52+
output []byte
53+
}*/
54+
inputBits := make([]bool, 0)
55+
highTable := make([]uint32, 0)
56+
lowTable := make([]uint32, 0)
57+
var symbolInterval uint32 = 0
58+
var step uint32 = 0
59+
output := make([]byte, 0)
60+
arithmeticDecoder := &ArithmeticDecoder{inputBits, highTable, lowTable,
61+
symbolInterval, step, 0, upperLimit, output}
62+
readBinaryFile(nil, inputFile, operation, false, arithmeticDecoder)
4463
}
4564
}

0 commit comments

Comments
 (0)