-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryIO.go
122 lines (110 loc) · 3.56 KB
/
binaryIO.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"io"
"os"
)
func readBinaryFile(arithmeticCoder *ArithmeticCoder, filepath string, operation string, modelCreation bool, arithmeticDecoder *ArithmeticDecoder, outputFile string) {
file, err := os.Open(filepath)
defer file.Close()
fileInfo, err := file.Stat()
errCheck(err)
fileSize := fileInfo.Size()
var bufferSize int64
//YOLO
bufferSize = fileSize
var bufferOverflow int64 = 0
//Data where we put the read bytes into
data := make([]byte, bufferSize)
for {
//Loop through the file, retrieve the bytes as integers
//:cap(data) = capacity of array, how many elements it can take before it has to resize
//Init slice
data = data[:cap(data)]
//Byte in the file
readByte, err := file.Read(data)
if err != nil {
if err == io.EOF {
break
}
fmt.Println(err)
return
}
data = data[:readByte]
//for _,aByte := range data {
//Add the new 8 booleans to the end of the bits array
//bits = append(bits,byteToBitSlice(&aByte)...)
//}
if operation == "c" {
if modelCreation {
arithmeticCoder.frequencyTableGenerator(data)
fmt.Println("Uncompressed size: ", fileSize)
} else {
arithmeticCoder.intervalCalculation(data)
}
} else if operation == "d" {
arithmeticDecoder.readFreqTable(data)
outputBytes := arithmeticDecoder.output
writeBinaryFile(outputFile, &outputBytes, 0)
}
bufferOverflow += bufferSize
//So we're aware of indexes if the file is larger
}
/*After we are done encoding the values byte by byte, we look at the rest:
- if low < firstQuarter : output 01 and E3_COUNTER times bit 1
- else : output 10 and E3_COUNTER times bit 0
*/
if !modelCreation && arithmeticCoder != nil {
writeEncoded(arithmeticCoder, outputFile)
//fmt.Println("The rest:")
}
}
func writeBinaryFile(fileName string, bytesToWrite *[]byte, bufferOverflow int64) {
if bufferOverflow == 0 {
_, err := os.Create(fileName)
errCheck(err)
}
file, err := os.OpenFile(fileName, os.O_RDWR, 0644)
defer file.Close()
errCheck(err)
_, err = file.WriteAt(*bytesToWrite, bufferOverflow)
errCheck(err)
stat, err := file.Stat()
size := stat.Size()
errCheck(err)
fmt.Println("Written file: ", size)
os.Exit(0)
}
func writeEncoded(arithmeticCoder *ArithmeticCoder, fileName string) {
if arithmeticCoder.low < arithmeticCoder.quarters[0] {
arithmeticCoder.outputBits = append(arithmeticCoder.outputBits, false, true)
for i := 0; uint32(i) < arithmeticCoder.e3Counter; i++ {
arithmeticCoder.outputBits = append(arithmeticCoder.outputBits, true)
}
} else {
arithmeticCoder.outputBits = append(arithmeticCoder.outputBits, true, false)
for i := 0; uint32(i) < arithmeticCoder.e3Counter; i++ {
arithmeticCoder.outputBits = append(arithmeticCoder.outputBits, false)
}
}
fmt.Println("")
//Write the 32uint[256] high table into file
//If the value in high table is 0,
// we can just write 4 0 bytes into the table, this saves us some time when doing compression on a small amount of unique symbols
outputBytes := make([]byte, 0)
//TODO: decide on number of written bytes based on the highest value
for i := 0; i < 256; i++ {
currentElement := arithmeticCoder.highTable[i]
if currentElement != 0 {
tempSlice := byteToBitSlice(currentElement, 32)
outputBytes = append(outputBytes, bitSliceToByte(&tempSlice, 4)...)
} else {
outputBytes = append(outputBytes, 0, 0, 0, 0)
}
}
for i := 0; i < len(arithmeticCoder.outputBits); i += 8 {
tempSlice := arithmeticCoder.outputBits[i : i+8]
outputBytes = append(outputBytes, bitSliceToByte(&tempSlice, 1)[0])
}
writeBinaryFile(fileName, &outputBytes, 0)
}