提交 30b34dc2 authored 作者: Thomas Mueller Graf's avatar Thomas Mueller Graf

BitStream improvements

上级 5d4fcfe8
...@@ -203,8 +203,8 @@ public class BinaryArithmeticStream { ...@@ -203,8 +203,8 @@ public class BinaryArithmeticStream {
Node n = tree; Node n = tree;
for (int i = bitCount; i >= 0; i--) { for (int i = bitCount; i >= 0; i--) {
boolean goRight = ((code >> i) & 1) == 1; boolean goRight = ((code >> i) & 1) == 1;
int prob = MAX_PROBABILITY * int prob = (int) ((long) MAX_PROBABILITY *
n.right.frequency / n.frequency; n.right.frequency / n.frequency);
out.writeBit(goRight, prob); out.writeBit(goRight, prob);
n = goRight ? n.right : n.left; n = goRight ? n.right : n.left;
} }
...@@ -219,8 +219,8 @@ public class BinaryArithmeticStream { ...@@ -219,8 +219,8 @@ public class BinaryArithmeticStream {
public int read(In in) throws IOException { public int read(In in) throws IOException {
Node n = tree; Node n = tree;
while (n.left != null) { while (n.left != null) {
int prob = MAX_PROBABILITY * int prob = (int) ((long) MAX_PROBABILITY *
n.right.frequency / n.frequency; n.right.frequency / n.frequency);
boolean goRight = in.readBit(prob); boolean goRight = in.readBit(prob);
n = goRight ? n.right : n.left; n = goRight ? n.right : n.left;
} }
......
...@@ -245,6 +245,17 @@ public class BitStream { ...@@ -245,6 +245,17 @@ public class BitStream {
return n.value; return n.value;
} }
/**
* Get the number of bits of the Huffman code for this value.
*
* @param value the value
* @return the number of bits
*/
public int getBitCount(int value) {
int code = codes[value];
return 30 - Integer.numberOfLeadingZeros(code);
}
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论