-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarization.java
More file actions
127 lines (111 loc) · 4 KB
/
Copy pathBinarization.java
File metadata and controls
127 lines (111 loc) · 4 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
package cn.edu.ustc.aaron.common;
import java.util.*;
import Jama.*;
public class Binarization {
private byte[][] binArr;
private int[] totalLength;
private List<LinkedList<Integer>> diagList;
private boolean codeCbCr;
public Binarization (List<LinkedList<Integer>> diagList, boolean codeCbCr) {
this.codeCbCr = codeCbCr;
this.diagList = diagList;
totalLength = new int[3];
calcTotalLength();
binArr = new byte[3][];
binArr[0] = new byte[totalLength[0]];
if (codeCbCr) {
binArr[1] = new byte[totalLength[1]];
binArr[2] = new byte[totalLength[2]];
}
}
public Binarization (byte[][] binArr, boolean codeCbCr) {
this.codeCbCr = codeCbCr;
this.binArr = binArr;
this.diagList = new ArrayList<>();
for (int color = 0; color < 3; color++) {
diagList.add(new LinkedList<Integer>());
}
}
private void calcTotalLength() {
calcTotalLength(0);
if (codeCbCr) {
calcTotalLength(1);
calcTotalLength(2);
}
}
private void calcTotalLength(int color) {
totalLength[color] = 0;
Iterator diagListIt = diagList.get(color).iterator();
while(diagListIt.hasNext()) {
int elem = (int) diagListIt.next();
totalLength[color] += Math.abs(elem); // unary binarization
}
totalLength[color] += 2 * diagList.get(color).size(); // plus one sign bit and one segmentation bit
System.out.println("Binarization totalLength[" + color + "]: " + totalLength[color]);
}
private void binarizeDiag(int color) {
int binPosElemFront = 0;
int binPosElemEnd = 0;
byte[] binArray = binArr[color];
Iterator diagListIt = diagList.get(color).iterator();
while(diagListIt.hasNext()) {
int elem = (int) diagListIt.next();
if (elem < 0) { // a sign bit at the front
binArray[binPosElemFront] = (byte)1;
}
else {
binArray[binPosElemFront] = (byte)0;
}
binPosElemEnd = binPosElemFront + Math.abs(elem);
for (int binPos = binPosElemFront + 1; binPos <= binPosElemEnd; binPos++) {
binArray[binPos] = (byte)1;
}
binArray[binPosElemEnd + 1] = (byte)0; // a segmentation bit at the end
binPosElemFront = binPosElemEnd + 2;
}
}
public void binarizeDiag() {
binarizeDiag(0);
if (codeCbCr) {
binarizeDiag(1);
binarizeDiag(2);
}
}
private void invBinarizeDiag(int color) {
int binPosElemFront = 0;
byte[] binArray = binArr[color];
while (binPosElemFront < binArray.length) {
int diffPos = 1; // skip the sign bit
while (binArray[binPosElemFront+diffPos] == (byte)1) { // calc the abs value
diffPos++;
}
if (binArray[binPosElemFront] == (byte)1) { // decode the sign bit
diagList.get(color).offer((-1) * (diffPos - 1));
}
else if (binArray[binPosElemFront] == (byte)0) {
diagList.get(color).offer(diffPos - 1);
}
else {
System.out.println("Unsuported sign bit in decoding arithmetic coding.");
}
binPosElemFront += diffPos + 1; // skip the segmentation bit
}
}
public List<LinkedList<Integer>> invBinarizeDiag() {
invBinarizeDiag(0);
if (codeCbCr) {
invBinarizeDiag(1);
invBinarizeDiag(2);
}
return diagList;
}
public byte[][] getbinArr () {
return binArr;
}
public byte[] getbinArr (int color) {
return binArr[color];
}
public List<LinkedList<Integer>> getDiag () {
return diagList;
}
}