-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmeticDecompress.java
More file actions
156 lines (131 loc) · 4.21 KB
/
Copy pathArithmeticDecompress.java
File metadata and controls
156 lines (131 loc) · 4.21 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package nayuki.arithcode;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.*;
public class ArithmeticDecompress {
private byte[][] binArr;
private List<LinkedList<Byte>> binList;
private boolean codeCbCr;
public ArithmeticDecompress (String inputFileName, boolean codeCbCr) throws IOException {
this.codeCbCr = codeCbCr;
binList = new ArrayList<>();
for (int color = 0; color < 3; color++) {
binList.add(new LinkedList<Byte>());
}
arithmeticDecompressProcess(binList.get(0), inputFileName+"Y.bin");
if (codeCbCr) {
arithmeticDecompressProcess(binList.get(1), inputFileName+"Cb.bin");
arithmeticDecompressProcess(binList.get(2), inputFileName+"Cr.bin");
}
listToArr();
}
public byte[][] getbinArr () {
return binArr;
}
public byte[] getbinArr (int color) {
return binArr[color];
}
private void allocMemory () {
binArr = new byte[3][];
binArr[0] = new byte[binList.get(0).size()];
if (codeCbCr) {
binArr[1] = new byte[binList.get(1).size()];
binArr[2] = new byte[binList.get(2).size()];
}
}
private void listToArr (int color) {
Iterator binLinkedListIt = binList.get(color).iterator();
byte[] binArray = binArr[color];
int binArrPos = 0;
while (binLinkedListIt.hasNext()) {
binArray[binArrPos++] = (byte)binLinkedListIt.next();
}
}
private void listToArr () {
allocMemory ();
listToArr(0);
if (codeCbCr) {
listToArr(1);
listToArr(2);
}
}
private void arithmeticDecompressProcess (LinkedList<Byte> b, String inputFileName) throws IOException {
// Otherwise, decompress
File inputFile = new File(inputFileName);
// File outputFile = new File(outputFileName);
BitInputStream in = new BitInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
// OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
// Writer out = new BufferedWriter(new FileWriter(outputFile));
try {
FrequencyTable freq = readFrequencies(in);
System.out.println(freq.toString());
decompress(freq, in, b);
} finally {
// out.close();
in.close();
}
}
public static void main (String[] args) throws IOException {
// Show what command line arguments to use
if (args.length == 0) {
System.err.println("Usage: java ArithmeticDecompress InputFile OutputFile");
System.exit(1);
return;
}
// Otherwise, decompress
File inputFile = new File(args[0]);
File outputFile = new File(args[1]);
BitInputStream in = new BitInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
// OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
Writer out = new BufferedWriter(new FileWriter(outputFile));
try {
FrequencyTable freq = readFrequencies(in);
// System.out.println(freq.toString());
decompress(freq, in, out);
} finally {
out.close();
in.close();
}
}
static FrequencyTable readFrequencies(BitInputStream in) throws IOException {
int[] freqs = new int[3];
for (int i = 0; i < 2; i++)
freqs[i] = readInt(in, 32);
freqs[2] = 1; // EOF symbol
return new SimpleFrequencyTable(freqs);
}
static void decompress(FrequencyTable freq, BitInputStream in, Writer out) throws IOException {
ArithmeticDecoder dec = new ArithmeticDecoder(in);
while (true) {
int symbol = dec.read(freq);
if (symbol == 2) // EOF symbol
break;
out.write(symbol);
}
}
static void decompress(FrequencyTable freq, BitInputStream in, LinkedList<Byte> b) throws IOException {
ArithmeticDecoder dec = new ArithmeticDecoder(in);
// int count = 0;
while (true) {
int symbol = dec.read(freq);
if (symbol == 2) // EOF symbol
break;
// System.out.print("\r"+(++count));
b.offer((byte)symbol);
}
// System.out.print("\n");
}
private static int readInt(BitInputStream in, int numBits) throws IOException {
if (numBits < 0 || numBits > 32)
throw new IllegalArgumentException();
int result = 0;
for (int i = 0; i < numBits; i++)
result |= in.readNoEof() << i; // Little endian
return result;
}
}