*
* @param received data and error-correction codewords
* @param twoS number of error-correction codewords available
- * @throws ReedSolomonException if decoding fails for any reaosn
+ * @param dataMatrix if true, then uses a calculation that matches the Data Matrix
+ * standard rather than the one used in QR Code
+ * @throws ReedSolomonException if decoding fails for any reason
*/
- public void decode(int[] received, int twoS) throws ReedSolomonException {
+ public void decode(int[] received, int twoS, boolean dataMatrix) throws ReedSolomonException {
GF256Poly poly = new GF256Poly(field, received);
int[] syndromeCoefficients = new int[twoS];
boolean noError = true;
for (int i = 0; i < twoS; i++) {
- int eval = poly.evaluateAt(field.exp(i));
+ // This difference in syndrome calculation appears to be correct, but then causes issues below
+ int eval = poly.evaluateAt(field.exp(dataMatrix ? i + 1 : i));
syndromeCoefficients[syndromeCoefficients.length - 1 - i] = eval;
if (eval != 0) {
noError = false;
return;
}
GF256Poly syndrome = new GF256Poly(field, syndromeCoefficients);
+ if (dataMatrix) {
+ // TODO Not clear this is correct for DataMatrix, but it gives almost-correct behavior;
+ // works except when number of errors is the maximum allowable.
+ syndrome = syndrome.multiply(field.buildMonomial(1, 1));
+ }
GF256Poly[] sigmaOmega =
runEuclideanAlgorithm(field.buildMonomial(twoS, 1), syndrome, twoS);
- int[] errorLocations = findErrorLocations(sigmaOmega[0]);
- int[] errorMagnitudes = findErrorMagnitudes(sigmaOmega[1], errorLocations);
+ GF256Poly sigma = sigmaOmega[0];
+ GF256Poly omega = sigmaOmega[1];
+ int[] errorLocations = findErrorLocations(sigma);
+ int[] errorMagnitudes = findErrorMagnitudes(omega, errorLocations);
for (int i = 0; i < errorLocations.length; i++) {
int position = received.length - 1 - field.log(errorLocations[i]);
received[position] = GF256.addOrSubtract(received[position], errorMagnitudes[i]);
}\r
int numECCodewords = codewordBytes.length - numDataCodewords;\r
try {\r
- rsDecoder.decode(codewordsInts, numECCodewords);\r
+ rsDecoder.decode(codewordsInts, numECCodewords, true);\r
} catch (ReedSolomonException rse) {\r
throw new ReaderException(rse.toString());\r
}\r
}\r
int numECCodewords = codewordBytes.length - numDataCodewords;\r
try {\r
- rsDecoder.decode(codewordsInts, numECCodewords);\r
+ rsDecoder.decode(codewordsInts, numECCodewords, false);\r
} catch (ReedSolomonException rse) {\r
throw new ReaderException(rse.toString());\r
}\r
--- /dev/null
+/*
+ * Copyright 2008 ZXing authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.common.reedsolomon;
+
+import junit.framework.TestCase;
+
+import java.util.BitSet;
+import java.util.Random;
+
+/**
+ * @author srowen@google.com (Sean Owen)
+ */
+public final class ReedSolomonDecoderDataMatrixTestCase extends TestCase {
+
+ private static final int[] DM_CODE_TEST = { 142, 164, 186 };
+ private static final int[] DM_CODE_TEST_WITH_EC = { 142, 164, 186, 114, 25, 5, 88, 102 };
+ private static final int DM_CODE_CORRECTABLE = (DM_CODE_TEST_WITH_EC.length - DM_CODE_TEST.length) / 2;
+
+ private final ReedSolomonDecoder dmRSDecoder = new ReedSolomonDecoder(GF256.DATA_MATRIX_FIELD);
+
+ public void testNoError() throws ReedSolomonException {
+ int[] received = new int[DM_CODE_TEST_WITH_EC.length];
+ System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
+ // no errors
+ checkQRRSDecode(received);
+ }
+
+ public void testOneError() throws ReedSolomonException {
+ int[] received = new int[DM_CODE_TEST_WITH_EC.length];
+ Random random = new Random(0xDEADBEEFL);
+ for (int i = 0; i < received.length; i++) {
+ System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
+ received[i] = random.nextInt(256);
+ checkQRRSDecode(received);
+ }
+ }
+
+ public void testMaxErrors() throws ReedSolomonException {
+ int[] received = new int[DM_CODE_TEST_WITH_EC.length];
+ Random random = new Random(0xDEADBEEFL);
+ for (int i = 0; i < DM_CODE_TEST.length; i++) { // # iterations is kind of arbitrary
+ System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
+ // TODO we aren't testing max errors really since there is a known bug here
+ corrupt(received, DM_CODE_CORRECTABLE - 1, random);
+ checkQRRSDecode(received);
+ }
+ }
+
+ public void testTooManyErrors() {
+ int[] received = new int[DM_CODE_TEST_WITH_EC.length];
+ System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
+ Random random = new Random(0xDEADBEEFL);
+ corrupt(received, DM_CODE_CORRECTABLE + 1, random);
+ try {
+ checkQRRSDecode(received);
+ fail("Should not have decoded");
+ } catch (ReedSolomonException rse) {
+ // good
+ }
+ }
+
+ private void checkQRRSDecode(int[] received) throws ReedSolomonException {
+ dmRSDecoder.decode(received, 2 * DM_CODE_CORRECTABLE, true);
+ for (int i = 0; i < DM_CODE_TEST.length; i++) {
+ assertEquals(received[i], DM_CODE_TEST[i]);
+ }
+ }
+
+ private static void corrupt(int[] received, int howMany, Random random) {
+ BitSet corrupted = new BitSet(received.length);
+ for (int j = 0; j < howMany; j++) {
+ int location = random.nextInt(received.length);
+ if (corrupted.get(location)) {
+ j--;
+ } else {
+ corrupted.set(location);
+ int newByte = random.nextInt(256);
+ received[location] = newByte;
+ }
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2008 ZXing authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.common.reedsolomon;
+
+import junit.framework.TestCase;
+
+import java.util.BitSet;
+import java.util.Random;
+
+/**
+ * @author srowen@google.com (Sean Owen)
+ */
+public final class ReedSolomonDecoderQRCodeTestCase extends TestCase {
+
+ /** See ISO 18004, Appendix I, from which this example is taken. */
+ private static final int[] QR_CODE_TEST =
+ { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC,
+ 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xA5 };
+ private static final int[] QR_CODE_TEST_WITH_EC =
+ { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC,
+ 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xA5, 0x24,
+ 0xD4, 0xC1, 0xED, 0x36, 0xC7, 0x87, 0x2C, 0x55 };
+ private static final int QR_CODE_CORRECTABLE = (QR_CODE_TEST_WITH_EC.length - QR_CODE_TEST.length) / 2;
+
+ private final ReedSolomonDecoder qrRSDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);
+
+ public void testNoError() throws ReedSolomonException {
+ int[] received = new int[QR_CODE_TEST_WITH_EC.length];
+ System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
+ // no errors
+ 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 {
+ int[] received = new int[QR_CODE_TEST_WITH_EC.length];
+ Random random = new Random(0xDEADBEEFL);
+ for (int i = 0; i < QR_CODE_TEST.length; i++) { // # iterations is kind of arbitrary
+ System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
+ corrupt(received, QR_CODE_CORRECTABLE, random);
+ checkQRRSDecode(received);
+ }
+ }
+
+ public void testTooManyErrors() {
+ int[] received = new int[QR_CODE_TEST_WITH_EC.length];
+ System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
+ Random random = new Random(0xDEADBEEFL);
+ corrupt(received, QR_CODE_CORRECTABLE + 1, random);
+ try {
+ checkQRRSDecode(received);
+ fail("Should not have decoded");
+ } catch (ReedSolomonException rse) {
+ // good
+ }
+ }
+
+ private void checkQRRSDecode(int[] received) throws ReedSolomonException {
+ qrRSDecoder.decode(received, 2*QR_CODE_CORRECTABLE, false);
+ for (int i = 0; i < QR_CODE_TEST.length; i++) {
+ assertEquals(received[i], QR_CODE_TEST[i]);
+ }
+ }
+
+ private static void corrupt(int[] received, int howMany, Random random) {
+ BitSet corrupted = new BitSet(received.length);
+ for (int j = 0; j < howMany; j++) {
+ int location = random.nextInt(received.length);
+ if (corrupted.get(location)) {
+ j--;
+ } else {
+ corrupted.set(location);
+ int newByte = random.nextInt(256);
+ received[location] = newByte;
+ }
+ }
+ }
+}
\ No newline at end of file
+++ /dev/null
-/*
- * Copyright 2008 ZXing authors
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.zxing.common.reedsolomon;
-
-import junit.framework.TestCase;
-
-import java.util.BitSet;
-import java.util.Random;
-
-/**
- * @author srowen@google.com (Sean Owen)
- */
-public final class ReedSolomonDecoderTestCase extends TestCase {
-
- /** See ISO 18004, Appendix I, from which this example is taken. */
- private static final int[] QR_CODE_TEST =
- { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC,
- 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xA5 };
- private static final int[] QR_CODE_TEST_WITH_EC =
- { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC,
- 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xA5, 0x24,
- 0xD4, 0xC1, 0xED, 0x36, 0xC7, 0x87, 0x2C, 0x55 };
- private static final int QR_CODE_CORRECTABLE = (QR_CODE_TEST_WITH_EC.length - QR_CODE_TEST.length) / 2;
-
- private final ReedSolomonDecoder qrRSDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);
-
- public void testNoError() throws ReedSolomonException {
- int[] received = new int[QR_CODE_TEST_WITH_EC.length];
- System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
- // no errors
- 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 {
- int[] received = new int[QR_CODE_TEST_WITH_EC.length];
- Random random = new Random(0xDEADBEEFL);
- for (int i = 0; i < QR_CODE_TEST.length; i++) { // # iterations is kind of arbitrary
- System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
- corrupt(received, QR_CODE_CORRECTABLE, random);
- checkQRRSDecode(received);
- }
- }
-
- public void testTooManyErrors() {
- int[] received = new int[QR_CODE_TEST_WITH_EC.length];
- System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
- Random random = new Random(0xDEADBEEFL);
- corrupt(received, QR_CODE_CORRECTABLE + 1, random);
- try {
- checkQRRSDecode(received);
- fail("Should not have decoded");
- } catch (ReedSolomonException rse) {
- // good
- }
- }
-
- private void checkQRRSDecode(int[] received) throws ReedSolomonException {
- qrRSDecoder.decode(received, 2*QR_CODE_CORRECTABLE);
- for (int i = 0; i < QR_CODE_TEST.length; i++) {
- assertEquals(received[i], QR_CODE_TEST[i]);
- }
- }
-
- private static void corrupt(int[] received, int howMany, Random random) {
- BitSet corrupted = new BitSet(received.length);
- for (int j = 0; j < howMany; j++) {
- int location = random.nextInt(received.length);
- if (corrupted.get(location)) {
- j--;
- } else {
- corrupted.set(location);
- int newByte = random.nextInt(256);
- received[location] = newByte;
- }
- }
- }
-}
\ No newline at end of file