-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmeticCompress.java
More file actions
135 lines (109 loc) · 3.96 KB
/
Copy pathArithmeticCompress.java
File metadata and controls
135 lines (109 loc) · 3.96 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
package nayuki.arithcode;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
public class ArithmeticCompress {
public ArithmeticCompress (byte[][] b, String outputFileName, boolean codeCbCr) throws IOException {
arithmeticCompressProcess(b[0], outputFileName+"Y.bin");
if (codeCbCr) {
arithmeticCompressProcess(b[1], outputFileName+"Cb.bin");
arithmeticCompressProcess(b[2], outputFileName+"Cr.bin");
}
}
private void arithmeticCompressProcess (byte[] b, String outputFileName) throws IOException {
File outputFile = new File(outputFileName);
// byte[] b = new byte[]{0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0 ,0 ,1, 1, 1, 1, 1, 1, 1, 1,0};
// Read input file once to compute symbol frequencies
FrequencyTable freq = getFrequencies(b);
// freq.increment(2); // EOF symbol gets a frequency of 1
// Read input file again, compress with arithmetic coding, and write output file
InputStream in = new ByteArrayInputStream(b);
// InputStream in = new BufferedInputStream(new FileInputStream(inputFile));
BitOutputStream out = new BitOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
try {
writeFrequencies(out, freq);
System.out.println(freq.toString());
compress(freq, in, out); // the AC is written after freq in outputBitstream
} 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 ArithmeticCompress InputFile OutputFile");
// System.exit(1);
// return;
// }
// Otherwise, compress
// File inputFile = new File(args[0]);
File outputFile = new File(args[0]);
byte[] b = new byte[]{0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0 ,0 ,1, 1, 1, 1, 1, 1, 1, 1,0};
// Read input file once to compute symbol frequencies
FrequencyTable freq = getFrequencies(b);
// freq.increment(2); // EOF symbol gets a frequency of 1
// Read input file again, compress with arithmetic coding, and write output file
InputStream in = new ByteArrayInputStream(b);
// InputStream in = new BufferedInputStream(new FileInputStream(inputFile));
BitOutputStream out = new BitOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
try {
writeFrequencies(out, freq);
System.out.println(freq.toString());
compress(freq, in, out); // the AC is written after freq in outputBitstream
} finally {
out.close();
// in.close();
}
}
private static FrequencyTable getFrequencies(byte[] b) {
FrequencyTable freq = new SimpleFrequencyTable(new int[3]);
// InputStream input = new BufferedInputStream(new FileInputStream(file));
for (byte x : b) {
freq.increment(x & 0xFF);
}
freq.increment(2);
// try {
// while (true) {
// int b = input.read();
// if (b == -1)
// break;
// freq.increment(b);
// }
// } finally {
// input.close();
// }
return freq;
}
static void writeFrequencies(BitOutputStream out, FrequencyTable freq) throws IOException {
for (int i = 0; i < 2; i++)
{
writeInt(out, 32, freq.get(i));
}
}
static void compress(FrequencyTable freq, InputStream in, BitOutputStream out) throws IOException {
ArithmeticEncoder enc = new ArithmeticEncoder(out);
// int count = 0;
while (true) {
int b = in.read();
if (b == -1)
break;
// System.out.print("\r"+(++count));
enc.write(freq, b);
}
// System.out.print("\n");
enc.write(freq, 2); // EOF
enc.finish();
}
private static void writeInt(BitOutputStream out, int numBits, int value) throws IOException {
if (numBits < 0 || numBits > 32)
throw new IllegalArgumentException();
for (int i = 0; i < numBits; i++)
out.write((byte)(value >>> i & 1)); // Little endian
}
}