mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Optimized 0- and 1-error case of Reed Solomon decoding a bit
git-svn-id: https://zxing.googlecode.com/svn/trunk@309 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
b88df41dca
commit
3a1e3dc156
|
@ -16,8 +16,6 @@
|
||||||
|
|
||||||
package com.google.zxing.common.reedsolomon;
|
package com.google.zxing.common.reedsolomon;
|
||||||
|
|
||||||
import java.util.Vector;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Implements Reed-Solomon decoding, as the name implies.</p>
|
* <p>Implements Reed-Solomon decoding, as the name implies.</p>
|
||||||
*
|
*
|
||||||
|
@ -59,19 +57,25 @@ public final class ReedSolomonDecoder {
|
||||||
public void decode(int[] received, int twoS) throws ReedSolomonException {
|
public void decode(int[] received, int twoS) throws ReedSolomonException {
|
||||||
GF256Poly poly = new GF256Poly(field, received);
|
GF256Poly poly = new GF256Poly(field, received);
|
||||||
int[] syndromeCoefficients = new int[twoS];
|
int[] syndromeCoefficients = new int[twoS];
|
||||||
|
boolean noError = true;
|
||||||
for (int i = 0; i < twoS; i++) {
|
for (int i = 0; i < twoS; i++) {
|
||||||
syndromeCoefficients[syndromeCoefficients.length - 1 - i] = poly.evaluateAt(field.exp(i));
|
int eval = poly.evaluateAt(field.exp(i));
|
||||||
|
syndromeCoefficients[syndromeCoefficients.length - 1 - i] = eval;
|
||||||
|
if (eval != 0) {
|
||||||
|
noError = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (noError) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
GF256Poly syndrome = new GF256Poly(field, syndromeCoefficients);
|
GF256Poly syndrome = new GF256Poly(field, syndromeCoefficients);
|
||||||
if (!syndrome.isZero()) { // Error
|
GF256Poly[] sigmaOmega =
|
||||||
GF256Poly[] sigmaOmega =
|
runEuclideanAlgorithm(field.buildMonomial(twoS, 1), syndrome, twoS);
|
||||||
runEuclideanAlgorithm(field.buildMonomial(twoS, 1), syndrome, twoS);
|
int[] errorLocations = findErrorLocations(sigmaOmega[0]);
|
||||||
int[] errorLocations = findErrorLocations(sigmaOmega[0]);
|
int[] errorMagnitudes = findErrorMagnitudes(sigmaOmega[1], errorLocations);
|
||||||
int[] errorMagnitudes = findErrorMagnitudes(sigmaOmega[1], errorLocations);
|
for (int i = 0; i < errorLocations.length; i++) {
|
||||||
for (int i = 0; i < errorLocations.length; i++) {
|
int position = received.length - 1 - field.log(errorLocations[i]);
|
||||||
int position = received.length - 1 - field.log(errorLocations[i]);
|
received[position] = GF256.addOrSubtract(received[position], errorMagnitudes[i]);
|
||||||
received[position] = GF256.addOrSubtract(received[position], errorMagnitudes[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,31 +135,34 @@ public final class ReedSolomonDecoder {
|
||||||
return new GF256Poly[]{sigma, omega};
|
return new GF256Poly[]{sigma, omega};
|
||||||
}
|
}
|
||||||
|
|
||||||
private int[] findErrorLocations(GF256Poly errorLocator)
|
private int[] findErrorLocations(GF256Poly errorLocator) throws ReedSolomonException {
|
||||||
throws ReedSolomonException {
|
|
||||||
// This is a direct application of Chien's search
|
// This is a direct application of Chien's search
|
||||||
Vector errorLocations = new Vector(3);
|
int numErrors = errorLocator.getDegree();
|
||||||
for (int i = 1; i < 256; i++) {
|
if (numErrors == 1) { // shortcut
|
||||||
|
return new int[] { errorLocator.getCoefficient(1) };
|
||||||
|
}
|
||||||
|
int[] result = new int[numErrors];
|
||||||
|
int e = 0;
|
||||||
|
for (int i = 1; i < 256 && e < numErrors; i++) {
|
||||||
if (errorLocator.evaluateAt(i) == 0) {
|
if (errorLocator.evaluateAt(i) == 0) {
|
||||||
errorLocations.addElement(new Integer(field.inverse(i)));
|
result[e] = field.inverse(i);
|
||||||
|
e++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (errorLocations.size() != errorLocator.getDegree()) {
|
if (e != numErrors) {
|
||||||
throw new ReedSolomonException("Error locator degree does not match number of roots");
|
throw new ReedSolomonException("Error locator degree does not match number of roots");
|
||||||
}
|
}
|
||||||
int[] result = new int[errorLocations.size()]; // Can't use toArray() here
|
|
||||||
for (int i = 0; i < result.length; i++) {
|
|
||||||
result[i] = ((Integer) errorLocations.elementAt(i)).intValue();
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int[] findErrorMagnitudes(GF256Poly errorEvaluator,
|
private int[] findErrorMagnitudes(GF256Poly errorEvaluator, int[] errorLocations) {
|
||||||
int[] errorLocations) {
|
|
||||||
// This is directly applying Forney's Formula
|
// This is directly applying Forney's Formula
|
||||||
int s = errorLocations.length;
|
int s = errorLocations.length;
|
||||||
|
if (s == 1) { // shortcut
|
||||||
|
return new int[] { errorEvaluator.getCoefficient(0) };
|
||||||
|
}
|
||||||
int[] result = new int[s];
|
int[] result = new int[s];
|
||||||
for (int i = 0; i < errorLocations.length; i++) {
|
for (int i = 0; i < s; i++) {
|
||||||
int xiInverse = field.inverse(errorLocations[i]);
|
int xiInverse = field.inverse(errorLocations[i]);
|
||||||
int denominator = 1;
|
int denominator = 1;
|
||||||
for (int j = 0; j < s; j++) {
|
for (int j = 0; j < s; j++) {
|
||||||
|
|
|
@ -45,6 +45,16 @@ public final class ReedSolomonDecoderTestCase extends TestCase {
|
||||||
checkQRRSDecode(received);
|
checkQRRSDecode(received);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testOneError() throws ReedSolomonException {
|
||||||
|
int[] received = new int[QR_CODE_TEST_WITH_EC.length];
|
||||||
|
Random random = new Random(0xDEADBEEFL);
|
||||||
|
for (int i = 0; i < received.length; i++) {
|
||||||
|
System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
|
||||||
|
received[i] = random.nextInt(256);
|
||||||
|
checkQRRSDecode(received);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void testMaxErrors() throws ReedSolomonException {
|
public void testMaxErrors() throws ReedSolomonException {
|
||||||
int[] received = new int[QR_CODE_TEST_WITH_EC.length];
|
int[] received = new int[QR_CODE_TEST_WITH_EC.length];
|
||||||
Random random = new Random(0xDEADBEEFL);
|
Random random = new Random(0xDEADBEEFL);
|
||||||
|
|
Loading…
Reference in a new issue