-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.kt
99 lines (72 loc) · 2.47 KB
/
Main.kt
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
import arithmeticEncoder.ArithmeticDecoder
import arithmeticEncoder.ArithmeticEncoder
import arithmeticEncoder.BitInputStream
import arithmeticEncoder.BitOutputStream
import java.io.*
import kotlin.system.measureTimeMillis
fun main(args: Array<String>) {
val time = measureTimeMillis {
val op = args[0][1]
when(op) {
'c' -> {
println("Comprimindo...")
if (args.size != 3) throw IllegalArgumentException("Arguments: [-c k | -d] file")
val maxContextOrder = args[1].toInt()
val filename = args[2]
compress(filename, maxContextOrder)
}
'd' -> {
println("Descomprimindo...")
if (args.size != 2) throw IllegalArgumentException("Arguments: [-c k | -d] file")
val filename = args[1]
decompress(filename)
}
else -> throw IllegalArgumentException("Invalid operation. Arguments: [-c k | -d] file")
}
}
println("t: $time ms")
}
fun compress(filename: String, maxContextOrder: Int) {
val input = BufferedInputStream(FileInputStream(File(filename)))
val output = BitOutputStream(BufferedOutputStream(FileOutputStream(File("$filename.ppm"))))
output.write8BitInt(maxContextOrder)
val encoder = ArithmeticEncoder(output)
val ppmEncoder = PpmEncoder(encoder, maxContextOrder)
while(true) {
val symbol = input.read()
if(symbol == -1) break // EOF
ppmEncoder.encode(symbol)
}
ppmEncoder.encode(256) // EOF
encoder.finish()
input.close()
output.close()
}
fun decompress(filename: String) {
if(!filename.endsWith(".ppm"))
throw IllegalArgumentException("Not a .ppm file")
val input = BitInputStream(FileInputStream(File(filename)))
val output = BufferedOutputStream(FileOutputStream(filename.removeSuffix(".ppm") + '2'))
val maxContextOrder = input.read8BitInt()
val decoder = ArithmeticDecoder(input)
val ppmDecoder = PpmDecoder(decoder, maxContextOrder)
while(true) {
val symbol = ppmDecoder.decode()
if(symbol == 256) break
output.write(symbol)
}
output.close()
input.close()
}
fun BitOutputStream.write8BitInt(int: Int) {
for(i in 0 until 8) {
this.write((int shr (7-i)) and 1)
}
}
fun BitInputStream.read8BitInt(): Int {
var int = 0
for(i in 0 until 8) {
int = (int shl 1) or this.read()
}
return int
}